Real Studio: How do I get the index of a substring? - realbasic

How can I get the index of a substring in from a string using Real Studio?
For example, I want to get the index of World in the example bellow
Dim str As String = "Hello World"

Look at the Instr() method in http://docs.realsoftware.com/index.php/Instr
Dim pos As Integer
pos = str.InStr("World")

Related

hidden space in excel

I tried almost all the methods (CLEAN,TRIM,SUBSTITUTE) trying to remove the character hiding in the beginning and the end of a text. In my case, I downloaded the bill of material report from oracle ERP and found that the item codes are a victim of hidden characters.
After so many findings, I was able to trace which character is hidden and found out that it's a question mark'?' (via VBA code in another thread) both at the front and the end. You can take this item code‭: ‭11301-21‬
If you paste the above into your excel and see its length =LEN(), you can understand my problem much better.
I need a good solution for this problem. Therefore please help!
Thank you very much in advance.
Thanks to Gary's Student, because his answer inspired me.
Also, I used this answer for this code.
This function will clean every single char of your data, so it should work for you. You need 2 functions: 1 to clean the Unicode chars, and other one to clean your item codes_
Public Function CLEAN_ITEM_CODE(ByRef ThisCell As Range) As String
If ThisCell.Count > 1 Or ThisCell.Count < 1 Then
CLEAN_ITEM_CODE = "Only single cells allowed"
Exit Function
End If
Dim ZZ As Byte
For ZZ = 1 To Len(ThisCell.Value) Step 1
CLEAN_ITEM_CODE = CLEAN_ITEM_CODE & GetStrippedText(Mid(ThisCell.Value, ZZ, 1))
Next ZZ
End Function
Private Function GetStrippedText(txt As String) As String
If txt = "–" Then
GetStrippedText = "–"
Else
Dim regEx As Object
Set regEx = CreateObject("vbscript.regexp")
regEx.Pattern = "[^\u0000-\u007F]"
GetStrippedText = regEx.Replace(txt, "")
End If
End Function
And this is what i get using it as formula in Excel. Note the difference in the Len of strings:
Hope this helps
You have characters that look like a space character, but are not. They are UniCode 8236 & 8237.
Just replace them with a space character (ASCII 32).
EDIT#1:
Based on the string in your post, the following VBA macro will replace UniCode characters 8236 amd 8237 with simple space characters:
Sub Kleanup()
Dim N1 As Long, N2 As Long
Dim Bad1 As String, Bad2 As String
N1 = 8237
Bad1 = ChrW(N1)
N2 = 8236
Bad2 = ChrW(N2)
Cells.Replace what:=Bad1, replacement:=" ", lookat:=xlPart
Cells.Replace what:=Bad2, replacement:=" ", lookat:=xlPart
End Sub

Is there a direct funtion to count numbers inside a string?

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())

Vb6 .text property for textbox required

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

Realbasic substring contained in string

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

How is this binary string being used?

Im studying some ruby code and I see this "varx" variable being used with [0x140,4] <- What does that means? I couldn't find any ruby doc showing that. The varx is something like a binary string.
varx[0x140, 4] = [0x4a8acb38 - 0x1c].pack('V')
It is replacing 4 "things" in varx starting at index 0x140 in varx with the data returned from the RHS side.
Example:
s="my name is prince";
s[11,6] = "John";
s
=> "my name is John"

Resources