Split by space vb.net - vb.net

How to split to 2 different string by space split?
I have a string with name and link.
I need to split it to 2 different strings.
[LVL 8] Logan_Aasd http://www.google.com/1
[LVL 8] Jack_Jackosn http://www.google.com/2
[LVL 8] Mask_Off http://www.google.com/3
[LVL 8] Dream_Alive http://www.google.com/4
And I need to split them this way to a different richtextbox:
[LVL 8] Logan_Aasd
[LVL 8] Jack_Jackosn
[LVL 8] Mask_Off
[LVL 8] Dream_Alive
http://www.google.com/1
http://www.google.com/2
http://www.google.com/3
http://www.google.com/4
I've tried to split it by the spaces and then use For Each. The code isn't right but I think it's close to what I need.
Dim text As String = RichTextBox2.Text
Dim x() As String
x = text.Split(" "c)
For Each s As String In x
RichTextBox3.Text = s
Next

You can use something like this:
Dim modifiedLines = New List(Of String)()
For index As Integer = 0 To lines.Length - 1
Dim url = lines(index).Substring(lines(index).LastIndexOf(" "c) + 1)
modifiedLines.Insert(index, lines(index).Replace(url, String.Empty))
modifiedLines.Add(url)
Next
This assumes that you've split up your content into a String array named lines.
Basically what this does is:
Create a List to store the new lines
Loop over each existing line
Get the URL by splitting on the last instance of a space on the currently iterated line, then getting the substring from that space to the end
Insert the existing line, replacing the URL with an empty String at the current index
Add the URL to the end of the collection
There is a bit of concern in that if your URL appears twice in the string then it would be removed when it goes to call the Insert command, but based on your example I don't think that is likely to happen.
Live Example: https://dotnetfiddle.net/ejlj9y

Related

Change a string and insert characters in between

I have this string: 71892378917238978
I want to do this: 71892-37891723-8978
I was trying this, but how i can do it multiple times?
String.Insert(6, "-")
We could start with this idea. Create an array of the substrings that you want to exist between the dash symbol. In your expected string you have 71892-37891723-8978, so the first block is composed of 5 char and the second block is 8 char.
Using the string.Substring method we could extract these substrings parts and store them in an array. Finally we could use string.Join to rebuild the string with the expected separator
Dim s As String = "71892378917238978"
' Define the substring blocks
Dim blocks As Integer() = New Integer() {5,8}
' Define the array that will contain the substrings
Dim substrings(blocks.Length) As String
' Get the blocks
For x As Integer = 0 To blocks.Length - 1
substrings(x) = s.Substring(0,blocks(x))
s = s.Substring(blocks(x))
Next
' Join together with the last part not included in the substrings
Dim result = String.Join("-", substrings) & s
Console.WriteLine(result)
You could generalize this code putting everything in a method where you could pass the string, the blocks and the separator.

Parsing through an Array For Next loop Visual Basic

I am stuck here. Spent hours trying many different approaches but nothing is working
I have an array that holds text that looks like this
4456|4450|17
4466|4430|18
4446|4420|19
4436|4410|20
The separator is a pica ("|").
What I am trying to do is run through the array and extract the first two columns in separate strings to compare the values, look for the max, and min.
I am trying to end up with a string like this
4456,4466,4446,4436
Here is the solution:
Dim source As String = prices
Dim stringSeparators() As String = {vbCrLf}
Dim result() As String
result = source.Split(stringSeparators,
StringSplitOptions.RemoveEmptyEntries)
Dim fString As String = String.Join(Of String)(", ", result.Cast(Of String).Select(Of String)(Function(x) x.Split("|")(0)))
MsgBox(fString)
Let's take your example below...
4456|4450|17
4466|4430|18
4446|4420|19
4436|4410|20
prices = [the array shown above]
For Each i As String In prices
high = (i.Split("|"))(0)
highs = highs & highs1 & ","
MsgBox(highs)
Next
The reason you are getting 4,4,5,6,,4,4,5,0,,1,7 is because for each string you are splitting on the | and then taking the first character adding a comma to it.
If you want to get the first column or index whatever you want to call it before the | you need to loop through each string in that array and select out the values...
'this is my test array...
Dim arr As New ArrayList From {"4456|4450|17", "4466|4430|18", "4446|4420|19", "4436|4410|20"}
Now we can use a String.Join function, cast the array for each item as a string and finally select the first item on the split. This will get every item before the | and put them in a string separated with a comma.
Dim fString As String = String.Join(Of String)(", ", arr.Cast(Of String).Select(Of String)(Function(x) x.Split("|")(0)))
If you want the second section select the 1st index as arrays start at 0...
Dim sString As String = String.Join(Of String)(", ", arr.Cast(Of String).Select(Of String)(Function(x) x.Split("|")(1)))
Here is my screenshot of the outputs...

How do I split one number from another to populate a field

I have a VendorID in the following format "24849_1". I'm trying to split the "1" off by itself and populate a field with it. The following code is what I'm using but all I'm managing to do is drop the "1" off completely. Anyone have any suggestions?
Dim strCustom1 As String
Dim strVendorID As String
strCustom1 = pWorkdoc.Fields("VendorID").Text
If strCustom1 <> "" Then
strCustom1 = Split(strCustom1, "_") (0)
xmlDoc.getElementsByTagName("RemitTo").Item(0).Text = strCustom1
End If
You're calling Split but have you actually bothered to find out what it does? It returns an array containing the split parts. You are then getting the part at index 0 in that array. If you actually want the part at index 1, i.e. the second part, then you should use index 1 instead of index 0.
The Split will give you an array, and you are taking the first element only; what you can do is:
strCustom1 = pWorkdoc.Fields("VendorID").Text // let it be 24849_1
If strCustom1 <> "" Then
Dim resultArray = Split(strCustom1, "_")
//resultArray[0] will be 24849
//resultArray[1] will be 1
//assign them as you need
End IF
The Split function returns a String array, but you have only taken the first element (the second element which contains the "1" was discard).
Keep the split array in a variable, so that you can access both elements.
Dim strCustom1 As String = "24849_1"
Dim split As String()
split = strCustom1.split("_")
Console.WriteLine("Part1={0} Part2={1}", split(0), split(1))

Using VB 2013, I'm having trouble sorting an array of strings on substrings within each string

I think it's pretty simple, but for some reason it's not coming to me...
This is my code so far
Dim fileReader As New StreamReader(FOfileToProcess)
Dim sLine As String = ""
Dim fileText As New ArrayList()
Do Until fileReader.EndOfStream()
sLine = fileReader.ReadLine()
If Not sLine Is Nothing Then fileText.Add(sLine)
Loop
fileReader.Close()
fileText.Sort(20, 2, Nothing) 'My intention is to sort by the values at zero-based 20 for 2 characters
fileText.Sort(63, 9, Nothing) '... and likewise at zero-based 63 for 9 characters
Dim saText() As String = DirectCast(fileText.ToArray(GetType(String)), String())
File.WriteAllLines(FOfileToProcess + "_sorted", saText)
It works, but the '_sorted' file written out is not sorted correctly. Is it just a matter of correcting my sort fields? The original sort is a 2-field sort with the primary being column 63 and the secondary being column 20, but since there's only 1 set of parms, I figured to go for the secondary first and end with the primary.
Suggestions?
TIA!
First, don't use an ArrayList as it does not allow you to specify the type of your items. Instead, use a List(Of T).
Dim fileText As New List(Of String)
Second, the indexes of the Sort method are related to the line number, not to a position within the strings (ArrayList does not know that you are inserting strings).
Dim fileTextSorted = fileText _
.OrderBy(Function(s) s.Substring(20, 2)) _
.ThenBy(Function(s) s.Substring(63, 29))
If the file contains lines shorter than the required positions of the substrings or empty lines (e.g. one empty line at the file end), then using the VB Mid function is to be preferred over the Substring which might throw exceptions. Alternatively, you can discard such lines by first applying a Where condition: .Where(Function(s) s.Length >= 72) _ or don't add such lines to the list when reading the file.
ArrayList.Sort(Int32, Int32, IComparer) doesn't sort your strings by substrings. You can just specifiy the range you want to sort, so string 21 to 23 and 64 to 73.
You want to sort by substrings in each string. Can i provide you a more readable approach using LINQ and ReadLines/WriteAllLines?
Dim lines = From line In System.IO.File.ReadLines(FOfileToProcess)
Where line.Length >= 73
Let col1 = line.Substring(20, 2)
Let col2 = line.Substring(63, 9)
Order By col1 Ascending, col2 Ascending
Select line
System.IO.File.WriteAllLines(FOfileToProcess + "_sorted", lines)

Split words but preserve special words

I am using VS2012, vb.net.
I am wanting to load a string into an array. The string is a series of words that are separated by a space (" ").
Currently I am using the following code:
Dim stringTempCurrentGameTypes() As String = Split(stringTypeList, " ")
This works perfectly. However, I have an exception to some of the data that gets loaded into the array. Sometimes the data in the string (the string that is separated by spaces) has two words that I want to load into the array as one item, and not two items.
Here is an example of a string that I am talking about:
tourney ffa team ctf clan arena test
The exception is the two words 'clan arena'.
Currently, if I just use the split command, I get an array with the following elements:
item(0) = tourney
item(1) = ffa
item(2) = team
item(3) = ctf
item(4) = clan
item(5) = arena
item(6) = test
I am after the following:
item(0) = tourney
item(1) = ffa
item(2) = team
item(3) = ctf
item(4) = clan arena
item(5) = test
How can I detect if the item being added to the array is the words 'clan arena', and add this as one entry, rather than as two entries? Also, the words 'clan arena' may change, so rather than hard coding the words 'clan arena', I need to do it via a string variable.
There are, of course, several ways to do this.
One way is to replace the whitespace of all your special items in the input string with a temporary character, split the input string, and then change the temporary character back to the original whitespace.
Example:
Dim raw = "tourney ffa team ctf clan arena test"
Dim special_words = new String() {"clan arena"}
Dim tmp_char = "$"
For Each word in special_words
raw = raw.Replace(word, word.Replace(" ", tmp_char))
Next
Dim result = raw.Split(new Char() {" "c})
For i = 0 To result.Count -1
result(i) = result(i).Replace(tmp_char, " ")
Next
As temporary character, you could use a unprintable character like Chr(31) (unit seperator) or anything you know that would not be in your input string.
This approach is quite simple and preserves the order of your items.