Equivalent of Visual Basic Split() in Attachmate Extra! Basic? - arrays

I'm working with an Attachmate Extra Basic Macro and need to Split() a string. I've tried several variations with Extra! Basic and it seems there is no Split() option. The below code works in Visual Basic; how would I do the same thing in Extra! Basic?
Dim str As String
Dim strArr() As String
Dim count As Integer
str = "vb.net split test"
strArr = str.Split(" ")
For count = 0 To strArr.Length - 1
MsgBox(strArr(count))
Next

Related

Splitting a String using 2 Characters

I'm currently splitting on periods in my code, but found a few issues when I ran it. I was wondering how I could split on ". " instead of just "."
Current Code:
Dim words As String() = item.Split(New Char() {"."c})
Dream Code:
Dim words As String() = item.Split(New Char() {". "c})
It doesn't allow me to add that extra space in after the period, is there a workaround?
To expand on that, see how Replace is used first? First you turn the 2 characters into one that is going to be uniquely identifiable, and then you can split it effectively.
Dim words As String() = item.Replace(". ", "|").Split(New Char() {"|"c})
And you can probably simplify it even more like this:
Dim words As String() = item.Replace(". ", "|").Split("|"c)
That assumes you are using Option Strict On - if not, you can simplify it more like this as the string will be converted to a char automatically:
Dim words As String() = item.Replace(". ", "|").Split("|")
You should use the String.Split Method (String(), StringSplitOptions) overload of String.Split:
Dim s = "1. 2. 3. 4.5.6"
Dim a = s.Split({". "}, StringSplitOptions.None)
Console.Write(String.Join(vbCrLf, a))
outputs:
1
2
3
4.5.6
(Depending on the version of Visual Studio, you might need something like New String() {". "} instead of {". "}.)

Splitting A String On The Second Space

I am very new to Vb.net, and have found out how to split strings and put the remnants of the split string in an array like so...
Dim str As String
Dim strArr() As String
Dim count As Integer
str = "vb.net split test"
strArr = str.Split(" ")
For count = 0 To strArr.Length - 1
MsgBox(strArr(count))
Next
Output:
vb.net
split
test
Now that I've got that figured out, I was wondering if there was a way to split on the SECOND space giving the output...
vb.net split
test
Thanks for your time!
You can do something like this
Dim input = "This is a Long String"
Dim splitted = input.Split(" "c)
Dim result = New List(Of String)()
For x As Integer = 0 To splitted.Length - 1 Step 2
result.Add(String.Join(" ", splitted.Skip(x).Take(2)))
Next

Finding Array index using VB6.0

I m trying to find out array index using visual basic. I tried some code with VB.Net and getting correct output. Below is the code I am using,
Dim FindThisString as String="EFGH"
Dim MyArray() As String={"ABCD","EFGH","IJKLM"}
For Each Str As String In MyArray
If Str.Contains(FindThisString) Then
MsgBox(Str.IndexOf(FindThisString))
End If
Next
Now I want to try the same method with VB 6.0. I am using Instr function but it is giving me string index in entire string and I am looking for array index i.e. index of string "EFGH" in MyArray.
Here is the VB6 code I'm trying:
Dim MyString as String
Dim str as Variant
MyString="ABCD/EFGH/IJKLM"
Dim MyArray() as String
MyArray = split(MyString,"/")
Dim inIndex as Integer
For Each Str In MyArray
inIndex= Instr(str,"EFGH")
MsgBox inIndex
Next
You would basically use the same algorithm:
Loop through the array (you'll need to use a Variant as the loop variable for VB Classic For Each),
verify if the array entry contains the substring in question (you need to use InStr here, since VB Classic does not have String.Contains),
return the index (which you already determined with InStr).
The implementation is left as an exercise.
Function IndexOf(ByRef arr() As String, ByVal str As String) As Integer
Dim joinedStr As String
Dim strIndex As Integer
joinedStr = "|" & Join(arr, "|")
strIndex = InStr(1, joinedStr, str)
If strIndex = 0 Then
IndexOf = -1
Exit Function
End If
joinedStr = Mid(joinedStr, 1, strIndex - 1)
IndexOf = UBound(Split(joinedStr, "|")) - 1
End Function

How can I iterate through an array of strings using VB?

Here's my code so far:
Dim i As Integer
Dim stringArray() as String
stringArray = split("Hello|there", "|")
For i = 0 To stringArray.Length()
' Logic goes here
Next
VB6 doesn't seem to like me using stringAray.Length() and gives me a compile error message like Invalid Qualifier, but what's the right way to iterate through each element of a string array?
ubound() returns the upper bounds;
Dim i As Long
Dim stringArray(1) as String
stringArray(0) = "hello"
stringArray(1) = "world"
For i = 0 To ubound(stringArray)
msgbox(stringArray(i))
Next
Dim i As Integer
Dim stringArray() as String
stringArray = split("Hello|there", "|")
For i = 0 To ubound(stringArray)
' Logic goes here
Next
This is the coding for above questions.
I have re do and also include the output using Visual Basic 6.0
it is pretty simple and easy to understand.
However,I wanted to try for more iteration.
click here for the coding and the output

VB.NET: Split string at everything except normal letters

I'm trying to split a string in VB.NET at everything except normal letters.
I tried to write a function using Char.IsLetter(...) but it didn't work too well for some reason (I put a comment where it crashed):
Private Function splitAtNonLetter(ByVal SplitString As String) As String()
Dim NonCharSplitArray As String() = {}
Dim ProcessValueTemp As String = String.Empty
For Each Letter As Char In SplitString
If Char.IsLetter(Letter) Then
ProcessValueTemp += Letter.ToString
Else
NonCharSplitArray(NonCharSplitArray.Length) = ProcessValueTemp
ProcessValueTemp = String.Empty
End If
Next
If ProcessValueTemp.Length > 0 Then
' Crashes in the next line: Index out of range exception...
NonCharSplitArray(NonCharSplitArray.Length) = ProcessValueTemp
End If
Return NonCharSplitArray
End Function
(I tried to use Regular Expressions but I've never used them before so I didn't really manage to get it to work either)
Is there a way to do it with RegExps or do you have to write a new function and how would it work?
Regex.Split is probably the way to go, using a negative character group.
For example:
Dim bits = Regex.Split(text, "[^a-zA-z]")
Or to cope with non-ASCII letters as well:
Dim bits = Regex.Split(text, "[^\p{L}]")

Resources