Splitting A String On The Second Space - arrays

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

Related

How to extract a part of array by referring start and end index just like what we do in Matlab

For example, if we have an array A.
In matlab, we just use A[a:b] to get a sub array easily,where a,b are start point and end point respectively.
Is there similar way to do it in VBA?
Thanks
Working with arrays is incredibly fast so this will probably give no discernable benefit - although I can understand how it may appeal from a coding sense than looping to fill a smaller array
Given you are working with a single element array you could:
Introduce a "marker" string inside the large array.
Join the large array with a delimiter into a single string.
Split the large array by the "marker" string, then separate the reduced string into a smaller array with the delimiter.
The code below dumps the numbers 1 to 100 into an array, and then splits it as above to pull out the first 10 records.
Sub test()
Dim bigArr
Dim subArr
Dim strSep As String
Dim strDelim As String
Dim strNew As String
Dim rowBegin As Long
Dim rowEnd As Long
strDelim = ","
strSep = "||"
'fill array with 1 to 100
bigArr = Application.Transpose(Application.Evaluate("row(1:100)"))
rowBegin = 1
rowEnd = 10
bigArr(rowEnd + 1) = strSep
'make a single string
strNew = Join(bigArr, strDelim)
'split the string at the marker
vArr = Split(strNew, strSep)
ReDim subArr(rowBegin To rowEnd)
'split the smaller string with the desired records
subArr = Split(Left$(vArr(0), Len(vArr(0)) - 1), strDelim)
End Sub

Regex expression and match function in text file

I have problem with my matching function actually I have to count number of lines with specific string and return line number ,so I have one dimensional array of string that contain the unique strings of text file {33,7,77,3 23,6} and text file with the same strings in array I have read lines of text file to array , but with duplicate of these strings ,when I use regex.match it works not bad expect when I check if line 2 contain 3 the function return True it's consider 3 in 23 as 3 , and the above explanation is just example of what I need any help please
Module Module1
Sub Main()
Dim txt As String = File.ReadAllText("e:\ii.txt")
' Use regular expressions to replace characters
' that are not letters or numbers with spaces.
Dim reg_exp As New Regex("[^a-zA-Z0-9]")
txt = reg_exp.Replace(txt, " ")
' Split the text into words.
'Dim words() As String = txt.Split( _
' New Char() {" "c}, _
' StringSplitOptions.RemoveEmptyEntries)
Dim words = txt.Split(New String() {" ", Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
' Use LINQ to get the unique words.
Dim word_query = _
(From word As String In words _
Order By word _
Select word).Distinct()
Dim stra() As String = word_query.ToArray()
For i = 0 To stra.Length - 1
Console.WriteLine(" " & stra(i))
Next
Dim lines() As String = IO.File.ReadAllLines("e:\ii.txt")
For i = 0 To lines.Length - 1
Console.WriteLine(lines(1))
Dim linecount = 0
Dim regex As New Regex(stra(i), RegexOptions.ExplicitCapture)
Dim match As Match = regex.Match(lines(1))
If match.Success Then
linecount += 1
Console.WriteLine("linecount= " & linecount)
Else
Console.WriteLine("false")
End If
Next
End Sub
End Module
You many not have to split the text into words. Is your word list very long? From what I understand you want the following:
1.Read a text file and return the line number for a given word or phrase.
Is the word or phrase complex? If not, why not use a the Contains extension method?
For example:
Dim myString = "Hello World"
If myString.Contains("World") Then
'Add line number to line count.
End if
If you are using this as an opportunity to learn regular expressions, I highly recommend "Mastering Regular Expressions" by Jeffrey Friedl. When I first begun I invested in a program RegexBuddy, which is worth the money. But now there are so many online regex testers now, that could be an alternative for something free.
Enhance your regex with anchors. These will ascertain that the whole test string matches instead of a substring. The following code also assembles all match patterns of interest into a single regex pattern which will be used against each line of the target file:
Dim strall As String
strall = ""
For i = 0 To stra.Length - 1
If i > o Then
strall = strall & "|"
End If
strall = strall & stra(i)
Console.WriteLine(" " & stra(i))
Next
strall = "^(" & strall & ")$"
Dim regexall As New Regex(strall, RegexOptions.ExplicitCapture)
'...
Dim linecount = 0
Dim match As Match = regexall.Match(lines(i)) '... was 'lines(1)', probably a typo
If match.Success Then
'...
this code is working with me thanks for all
Public Function countlines(ByVal st As String) As Integer
Dim count As Integer
Dim linecount As Integer = 0
Dim substrings() As String = Regex.Split(st, " ")
Dim stt() As String = {23, 7, 3}
For i = 0 To stt.Length - 1
'For j = 0 To substrings.Length - 1
'Console.WriteLine(substrings(0))
'For i = 0 To substrings.Length - 1
'Console.Write(substrings(i))
Dim matchQuery = From word In substrings Where word.ToLowerInvariant() = stt(i).ToLowerInvariant() Select word
' ' Count the matches.
count = matchQuery.Count()
Console.WriteLine("count=" & count)
If count > 0 Then
linecount += 1
Else
Console.WriteLine(" linecount=" & linecount)
End If
Next
Console.WriteLine("linecount= " & linecount)
Return linecount
End Function
Sub Main()
Dim lines() As String = IO.File.ReadAllLines("e:\ii.txt")
For Each line As String In lines
countlines(line)
Next
End Sub

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

Visual Basic: How to get entire section before/after a certain character?

I have a string. For example stringone/string2
How do I get the section before the "/" and also after the "/"
Dim Word As String = "stringone/string2"
Dim wordArr As String() = Word.Split("/")
Dim stringBefore As String = wordArr(???)
Dim stringBefore As String = wordArr(???)
What is the next step
Split() returns an Array. The first element is at Index 0 (zero), the second element is at Index 1 (one), etc...
You should check to make sure the returned array is your expected size (at least), otherwise you'll get an error attempting to access an index slot that doesn't exist.
Dim Word As String = "stringone/string2"
Dim wordArr As String() = Word.Split("/")
If wordArr.Length = 2 Then
Dim stringBefore As String = wordArr(0)
Dim stringAfter As String = wordArr(1)
Debug.Print("stringBefore = " & stringBefore)
Debug.Print("stringAfter = " & stringAfter)
End If
*By the way, the code you posted is not VB6, it's VB.Net.

Resources