Reading an array following a 2 level string.split - vb.net

Newbie question. I can't figure this out.
I have string "48,1;49,2;50,0" which I have split using :
Dim splitAtSemiColon() As String = strPatientMed.Split(";"c)
Dim result() = splitAtSemiColon.Select(Function(x) x.Split(","c)).ToArray()
In the Visual studio interface, I can see that this results in:
Result(0) contains (0) = "48" and (1) = "1"
Result(1) contains (0) = "49" and (1) = "2"
Result(2) contains (0) = "50" and (1) = "0"
But how do I loop to read this array using code i.e. how do I read the (0) and (1) portion of each result item?
Thanks all!

Sub Main()
Dim strPatientmed = "48,1;49,2;50,0"
Dim splitAtSemiColon() As String = strPatientMed.Split(";"c)
Dim result() = splitAtSemiColon.Select(Function(x) x.Split(","c)).ToArray()
'Result is an Array of arrays, each inner array has only 2 elements (first & last)
For Each element In result
'For each <array> in result
'result(0) -> <element> -> [48|1]
'<element>.First = 48 | <element>.Last = 1
Console.WriteLine($"Before comma: {element.First} | After comma: {element.Last}")
Next
End Sub
Result
Before comma: 48 | After comma: 1
Before comma: 49 | After comma: 2
Before comma: 50 | After comma: 0

Related

Substring Method in VB.Net

I have Textboxes Lines:
{ LstScan = 1,100, DrwR2 = 0000000043 }
{ LstScan = 2,200, DrwR2 = 0000000041 }
{ LstScan = 3,300, DrwR2 = 0000000037 }
I should display:
1,100
2,200
3,300
this is a code that I can't bring to a working stage.
Dim data As String = TextBox1.Lines(0)
' Part 1: get the index of a separator with IndexOf.
Dim separator As String = "{ LstScan ="
Dim separatorIndex = data.IndexOf(separator)
' Part 2: see if separator exists.
' ... Get the following part with Substring.
If separatorIndex >= 0 Then
Dim value As String = data.Substring(separatorIndex + separator.Length)
TextBox2.AppendText(vbCrLf & value)
End If
Display as follows:
1,100, DrwR2 = 0000000043 }
This should work:
Function ParseLine(input As String) As String
Const startKey As String = "LstScan = "
Const stopKey As String = ", "
Dim startIndex As String = input.IndexOf(startKey)
Dim length As String = input.IndexOf(stopKey) - startIndex
Return input.SubString(startIndex, length)
End Function
TextBox2.Text = String.Join(vbCrLf, TextBox1.Lines.Select(AddressOf ParseLine))
If I wanted, I could turn that entire thing into a single (messy) line... but this is more readable. If I'm not confident every line in the textbox will match that format, I can also insert a Where() just before the Select().
Your problem is you're using the version of substring that takes from the start index to the end of the string:
"hello world".Substring(3) 'take from 4th character to end of string
lo world
Use the version of substring that takes another number for the length to cut:
"hello world".Substring(3, 5) 'take 5 chars starting from 4th char
lo wo
If your string will vary in length that needs extracting you'll have to run another search (for example, searching for the first occurrence of , after the start character, and subtracting the start index from the newly found index)
Actually, I'd probably use Split for this, because it's clean and easy to read:
Dim data As String = TextBox1.Lines(0)
Dim arr = data.Split()
Dim thing = arr(3)
thing now contains 1,100, and you can use TrimEnd(","c) to remove the final comma
thing = thing.TrimEnd(","c)
You can reduce it to a one-liner:
TextBox1.Lines(0).Split()(3).TrimEnd(","c)

Substring a Word from a Multiline Textboxes

I want to do the following thing: if I have a Textbox1.Lines:
2
5
15
21
45
and I want to for example get the number left 15 and get the number right 15.
Output: Textbox2.Lines 15 21
5 15
How should I do this? example for the number 2 want substring 2 5
Module Module1
Public Function FirstWords(input As String,
count As Integer) As String
Dim words = count
For i As Integer = 0 To input.Length - 1
' Decrement word count when we reach a space.
If input(i) = " " Then
words -= 1
End If
' When no words remaining, return a substring to this point.
If words = 0 Then
Return input.Substring(0, i)
End If
Next
Return ""
End Function
how should i do this to work?
You can separate them into an array of strings with the Split function, like in this example:
Dim input as String = "2 5 15 21 45"
Dim numbers as String() = input.Split(" ")
For Each s as String In numbers
Console.WriteLine(s)
Next
You just have to figure out how to extract the numbers you want from the lot. Have fun!

Convert a String to its integer type assigned value

Using Below codes in VB.net, I have some variables (HUCROW1,HUCROW2,HUCROW3) which are integer type and a specific value is assigned to each of them.
Then in a loop I try to make HUCROW equal to above variables. HUCROW is string type and I concatenate it with the loop number for example by using "HUCROW" & 1.
HUCROW1 = 66
HUCROW2 = 84
HUCROW3 = 102
HUCROW4 = 120
For j = 1 To 3
Dim HUCROW As String = "HUCROW" & (j)
If ScheduleSht.Cells(HUCROW, 9).Value.Ticks <> Nothing Then
HUCdates(i) = ScheduleSht.Cells(HUCROW, 9).value.Ticks
HUCdates(i) = ScheduleSht.Cells(HUCROW, 9).value.Ticks
Else
HUCdates(i) = 0
End If
Next
My aim is to get HUCROW1 value which is 66, but it doesn't work and give "type mismatch error" on ScheduleSht.Cells(HUCROW, 9).value.Ticks as it doesn't convert the string HUCROW to its assigned number.
Any help on how to convert the string type "HUCROW1" to the assigned integer value to HUCROW1 would be appreciated.
I think it's easier to use an array or a dictionary to store and retrive the values.
Array sample (subtract j because an array's index starts from zero)
Dim HUCROWS As Integer() = {66, 84, 102, 120}
For j = 1 To 3
Console.WriteLine(HUCROWS(j - 1))
Next
Dictionary sample
Dim HUCROWS As New Dictionary(Of String, Integer) From {
{"HUCROW1", 66},
{"HUCROW2", 84},
{"HUCROW3", 102},
{"HUCROW4", 120}
}
For j = 1 To 3
Dim HUCROW As String = "HUCROW" & j
Console.WriteLine(HUCROWS(HUCROW))
Next
Result of both codes
66
84
102

How in Visual Basics change characters in a string for example (1 to 0) and (0 to 1)?

How in Visual Basics change characters in a string for example (1 to 0)
and (0 to 1) without the problem of changing them first to all to 0 and then all to 1, i want to get a result for example like that "00110010101101001010" to "11001101010010110101" (to flip it)
Use an intermediate value.
Change all 0 -> 2
Change all 1 -> 0
Change all 2 -> 1
How about this:
Dim text = "00110010101101001010"
Dim flipped = New String(text.Select(Function(c) If(c = "0"c, "1"c, "0"c)).ToArray())
That gives me:
11001101010010110101
Another option is to convert the string to a character array iterate over each character individually; then build a new string from the modified array (or overwrite the original):
Dim data As String = "00110010101101001010"
Dim arr() As Char = data.ToCharArray
For i As Integer = 0 To arr.Length - 1
arr(i) = If(arr(i) = "1", "0", "1")
Next
Dim data2 As New String(arr)
Debug.Print(data)
Debug.Print(data2)

Reading multiple integers on a line to a listbox

I am trying to read all of the integers in a list box line.
Dim scores As System.Text.RegularExpressions.Match = System.Text.RegularExpressions.Regex.Match(line, "\d+")
I have saved the scores in a format like this
Name 00 00 00
Could I have the regular expression for reading 3 integers with a space in between each number? The file the integers are stored in is a notepad file.
Approach without regex:
Const SEPARATOR As String = " "c
Dim line As String 'Here reading line from the file
Dim numbers As New List(Of Int32)()
Dim values As String() = line.Split(SEPARATOR) 'Split values to array
For Each value As String in values
Dim tempnumber As Int32
If Int32.TryParse(value, tempnumber) = True Then
'Accept only numbers
numbers.Add(tempnumber)
End If
Next
Use Matches instead of Match and store the results of the Regex into a MatchCollection
Sub Main()
Dim scores As String = "00 13 00"
Dim score As System.Text.RegularExpressions.MatchCollection = System.Text.RegularExpressions.Regex.Matches(scores, "\d+")
For i As Integer = 0 To score.Count - 1
Console.WriteLine(score.Item(i))
Next
Console.ReadLine()
End Sub
Results: