I'd like to know how can i find out if a substring is contained in another string?
In VB.net I used to write something like:
Dim s as string = textbox1.text
if s.contains("http://youtube.com/watch?v=")
//instructions
end if
Basically, this is want I want to do but it doesn't work the same way in RealBasic, so how can I translate this?
You can do the same thing by using:
Dim s as string = textbox1.text
if s.instr("http://youtube.com/watch?v=") > 0 then
//instructions
end
Related
I have a string that contains digits inside. For example, "adf20j83n,m3jh2k9". Is there a direct way to count the number of digits inside the string. As in my example, it should give me "7" as an output.
Also, I have tried RegExp but it's not working in VBScript in QTP.
Btw, I'm not looking for loops and stuff like that. Just a direct way, or a suggestion to make this RegExp work in QTP.
You'll probably need to create the COM object via its ProgId:
Dim re
Set re = CreateObject("VBScript.RegExp")
re.Pattern = "\d"
re.Global = True
MsgBox "Digits: " & re.Execute("adf20j83n,m3jh2k9").Count
Output:
Digits: 7
I know it's techically what you said but it's a good way nonetheless
Try using this function:
Public Sub CountNumeric(ByVal input As String)
Dim numericCount As Integer = 0
For Each c As Char In input
If Char.IsDigit(c) Then numericCount += 1
Next
MessageBox.Show(String.Format("Number of numerics : {0}", numericCount)
End Sub
EDIT:
You could also try this:
Dim charColl As MatchCollection = Regex.Matches(input , "^\d+$")
Console.WriteLine(charColl.Count.ToString())
I am trying to convert letters to numbers.
I have a sub which ensures only numbers are put into the textbox.
My questions is will the following code work. I have a textbox(for numbers) and combobbox(for letters)
Dim sha As String
Dim stringposition As Long
Dim EngNumber As Long
sha = "abcdefghifjklmnopqrstuvwxyz"
stringposition = InStr(1, sha, Mid(1, cmbEngletter.Text, 1))
MsgBox "stringposition"
EngNumber = (txtManuNo.Text * 10) + stringposition
My only question above would be will the multiplication work with a .text. I believe it won't because it is a string. Please advise then on how to deal with a situation.
You can use CLng() to convert a string to a Long variable
CLng() will throw an error though if it doesn't like the contents of the string (for example if it contains a non-numeric character), so only use it when you are certain your string will only contain numbers
More forgiving is it to use Val() to convert a string into a numeric variable (a Double by default)
I also suggest you look into the following functions:
Asc() : returns the ASCII value of a character
Chr$() : coverts an ASCII value into a character
Left$() : returns the first characters of a string
CStr() : convert a number into a string
I think in your code you mean to show the contents of your variable stringposition instead of the word "stringposition", so you should remove the ""
I do wonder though what you are trying to accomplish with your code, but applying the above to your code gives:
Dim sha As String
Dim stringposition As Long
Dim EngNumber As Long
sha = "abcdefghifjklmnopqrstuvwxyz"
stringposition = InStr(1, sha, Left$(cmbEngletter.Text, 1))
MsgBox CStr(stringposition)
EngNumber = (Val(txtManuNo.Text) * 10) + stringposition
I used Val() because I am not certain your txtManuNo will contain only numbers
To ensure an user can only enter numbers you can use the following code:
Private Sub txtManuNo_KeyPress(KeyAscii As Integer)
Select Case KeyAscii
Case vbKeyBack
'allowe backspace
Case vbKey0 To vbKey9
'allow numbers
Case Else
'refuse any other input
KeyAscii = 0
End Select
End Sub
An user can still input non-numeric charcters with other methods though, like copy-paste via mouse actions, but it is a quick and easy first filter
I'm extremely unfamiliar with ancient VB, and I'm trying to figure out the proper commands to concatenate an array (I am assuming it is in array form) into a string with comma separated values.
The values are being provided by a multiselect box, which is being assigned to the areas variable, which is grabbed from the areas select box.
dim name
dim from
dim company
dim phone
dim zip
dim message
dim areas
name = Request.Form("name")
from = Request.Form("from")
company = Request.Form("company")
phone = Request.Form("phone")
zip = Request.Form("zip")
areas = Request.Form("areas")
message = Request.Form("message")
I want to take areas, and implode it into a string.
What's the command in very old VB to do this?
The Join function does also exist in VB6, the syntax is like this:
myString = Join(myArray, ",")
EDIT: Note that the array goes before the delimiter. The delimiter is optional, it'll be a space if left empty.
you want to use the Join function String.Join(",",array)
If you are using VB6, try this:
Join(New String() {name, from, company}, ", ")
Join Function (Visual Basic) # MSDN
EDIT: I know it's a link to VB.NET, but it's the old VB6 function call, it should be compatible with VB6, syntax wise.
I am trying to replace the double quotes in a string with a single quote, got the following code but get error message saying "Object Required strLocation"
Sub UpdateAdvancedDecisions(strLocation)
Dim d
Dim strLLength
strLLength = Len(strLocation) - 1
For d = 0 To strLLength
alert strLocation
strValue = strLocation.Substring(2,3)
If strLocation.substring(d,d+1)=" " " Then
strLLength = strLLength.substring(0, d) + "'" + strLLength.substring(d + 1,strLLength.length)
Next
End Sub
As Helen said, you want to use Replace, but her example assigned the result to your weird strLLength variable. Try this instead:
strLocation = Replace(strLocation, """", "'")
This one line does the job you asked about and avoids all the code currently in your given subroutine.
Other things that are problems in the code you posted:
a variable holding a number like the length of a string would not have a "str" prefix, so strLLength is misleading
strings in VBScript are indexed from 1 through length, not 0 through length-1
there is no "alert" keyword in VBScript
you assign a value to strValue, then never use it again
you need to use Mid to get a substring, there is no "substring" string method in VBScript
c = Mid(strLocation, d, 1) ' gets one character at position d
The more I look at this, the more clear it is that its some JavaScript that you're trying to run as VBScript but are not translating at all correctly.
Use a reference for VBScript like one of the following:
MSDN Library: VBScript Language Reference
W3Schools VBScript Tutorial
I'm using vbscript, how do I find the hyphen character "-" in a string and place characters that follow in a new string?
Basically my computer names end with a room number
PC1-3 (room 3) , PC1-4 (room 4) etc
I'm using
Dim roomArray(2)
roomArray = Array("-1", "-2", "-3")
Dim item
For each item in roomArray
If instr(strComputerName, item)
..do something
End If
Next
but I'm getting false positives due to room -33 containing "-3" etc so if I locate "-" in the string and place the characters that follow into a string I can check against the full string rather than using instr.
Thanks for any help.
This will get all text behind the last hyphen found:
Function GetRoomNumber(strComputerName)
Dim hyphenIndex
hyphenIndex = InStrRev(strComputerName, "-")
If hyphenIndex > 0 Then
GetRoomNumber = Mid(strComputerName, hyphenIndex+1)
End If
End Function
Usage in your example will be:
Dim roomArray(2)
roomArray = Array("-1", "-2", "-3")
Dim item, index
For index = LBound(roomArray) To UBound(roomArray)
item = roomArray(index)
If ("-" & GetRoomNumber(strComputerName)) = item Then
..do something
End If
Next
Or the short version would be (without defining the function with data validation):
...
If Mid(strComputerName, InStrRev(strComputerName, "-") ) = item Then
...
You need to check if strComputerName ends with dash-roomNumber.
if Right(strComputerName,Len(item))=item Then...
You kan use right and instr to retrieve this
dim s
s = "PC1-3 (room 3)"
msgbox right(s, len(s) - instr(s,"-"))