Splitting string by two conditions in vb.net 2014 - vb.net

I'm trying to split a string into a list of strings with this vb.net code, while adding it to a dictionary of String:
objevt.wbStr.Add(b, objevt.metalbelowwidth.Item(b).Split({",","-"}).ToList)
But I get an error saying "Value of '1-dimentitional arrary of string' cannot be converted to 'Char' "
If I try to implement splitting with just the comma, it works fine. So the following code works.
objevt.wbStr.Add(b, objevt.metalbelowwidth.Item(b).Split(",").ToList)
But I really want to split the string with two conditions. Any help is appreciated. Thanks!

One of the overloads of the Split Method that takes a String array also needs to have a StringSplitOption parameter also set.
objevt.wbStr.Add(b, objevt.metalbelowwidth.Item(b).Split(New String() {",", "-"}, StringSplitOptions.None).ToList)
Sub Main()
Dim myString As String = "H,c,J-Hello-World"
Dim myList As List(Of String) = New List(Of String)
myList = myString.Split(New String() {"-", ","}, StringSplitOptions.None).ToList
For Each s As String In myList
Console.WriteLine(s)
Next
Console.ReadLine()
End Sub

You need to use the overloaded Split method with an array of char as the separators, like so:
objevt.metalbelowwidth.Item(b).Split(New [Char]() {","c, "-"c })
Demo here

Related

Comparing String to List of String in visual basic

Example: How do I compare one string out of a list against my desired string? Please help!
Dim myString As String
Dim myList As List(Of String)
Let's say myString Returns Bob and myList Returns (Bob, Mary, Sally, Joe)
I Need to do the following:
If(myString = myList) Then
//Do some code
End If
In order to compare a string out of a list of strings to your desired string, you must know where in the list the string is that you want to compare with.
Dim myString As String
Dim myList as List(Of String)
'this will compare your string to the first string in the list of strings
If myString = myList(0) Then
'do something
End If
If you would like to see if any of the strings in the list are equal to your string, do this
For i = 0 to myList.Count - 1
If myString = myList(i) Then
'do something
End If
Next
If you want to test whether the list contains "Bob" you can use the Contains Method:
Dim myList As New List(Of String)
myList.Add("Bob")
If(myList.Contains("Bob")) Then
Console.WriteLine("Yes, it is in the list")
End If

VB.Net two times split

I'm am developing a app, and I have a question.
How can I do two times split?
What I mean is: |abcd,abcd|abcd,abcd
Now I want to split | and then I have two string, those two string I want
to split. How can I split those two strings?
With regards,
Martin de Groot
You can achieve what you want by splitting just once. See the following code.
Sub Main()
Dim test = "|abcd,xyz|abcde,lmno|foo,123"
Dim result = test.Split("|,".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
For Each item In result
Console.WriteLine(item)
Next
End Sub
All you have to do is place all your delimiters in the first argument in an array (or generate one as I have done), and then ensure you have no empty entries and you're good to go. The result will be one array with all the strings you want.
Example
Dim s As String = "|abcd,xyz|abcde,lmno|foo,123"
Dim p() As String = s.Split(New Char() {"|"c}, StringSplitOptions.RemoveEmptyEntries)
Dim sp() As String
For Each foo As String In p
sp = foo.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
Next

String.Split Method optimize way in vb.net

I have a string xxx1-0.1/1 yyy,ccc1,1. I used split and substring. All i want to get is the 0.1/1 only. Is there any optimize way to do this?
Thank you in advance!
Use more than one string split chars. then if the position and format is always the same then this will work.
Dim s As String = "xxx1-0.1/1 yyy,ccc1,1"
Dim ans = s.Split(New Char() {"-"c, " "c})(1)
MessageBox.Show(ans)
Lets make it a function:
Private Function getMySpecialValue(input As String) As String
Return input.Split(New Char() {"-"c, " "c})(1)
End Function

VB.NET - Reading a text file containing tab-separated integers/doubles and storing them in arrays

I have a text file containing tab-seperated integers and doubles, e.g.:
5[TAB]0.3[TAB]2.9[TAB]61[TAB]110
8[TAB]1.1[TAB]5.2[TAB]13[TAB]45
1[TAB]0.8[TAB]1.4[TAB]28[TAB]33
...
What I need is a convenient way to access each line as a double array in VB.NET - so that the first array would be [5.0 0.3 2.9 61.0 110.0], the second array would be [8.0 1.1 5.2 13.0 45.0], and so on...
How can this be accomplished using the StreamReader?
If it's ok to use a list of lists instead of a list of arrays, you can just do this:
Private Function LoadFile(ByVal filePath As String) As List(Of List(Of Double))
Dim records As New List(Of List(Of Double))()
For Each line As String In File.ReadAllLines(filePath)
Dim values As New List(Of Double)()
For Each field As String In line.Split(New String() {ControlChars.Tab}, StringSplitOptions.None)
values.Add(Double.Parse(field))
Next
records.Add(values)
Next
Return records
End Function
Or, if it must be a list of arrays, you could do this:
Private Function LoadFileToArrays(ByVal filePath As String) As List(Of Double())
Dim records As New List(Of Double())()
For Each line As String In File.ReadAllLines(filePath)
Dim values As New List(Of Double)()
For Each field As String In line.Split(New String() {ControlChars.Tab}, StringSplitOptions.None)
values.Add(Double.Parse(field))
Next
records.Add(values.ToArray())
Next
Return records
End Function
If you need an array of arrays, you could just return records.ToArray(), instead, in that last example. I did not add any code to handle invalid or empty field values, because it wasn't clear, in your question, how you would want to handle those. So, you'll want to add code to handle that appropriately, otherwise, this code will throw an exception in such cases.

How to split multiline string?

When I try to split a string into a string list where each element represents a line of the initial string, I get the "square" character, which I think is a linefeed or something, at the start of each line, except the first line. How can I avoid that? My code is as follows:
Dim strList as List(Of String)
If Clipboard.ContainsText Then
strList = Clipboard.GetText.Split(Environment.NewLine).ToList
End If
I find that a pretty reliable way to read the lines of a string is to use a StringReader:
Dim strList As New List(Of String)
Using reader As New StringReader(Clipboard.GetText())
While reader.Peek() <> -1
strList.Add(reader.ReadLine())
End While
End Using
Maybe that's weird; I don't know. It's nice, though, because it frees you from dealing with the different ways of representing line breaks between different systems (or between different files on the same system).
Taking this one step further, it seems you could do yourself a favor and wrap this functionality in a reusable extension method:
Public Module StringExtensions
<Extension()> _
Public Function ReadAllLines(ByVal source As String) As IList(Of String)
If source Is Nothing Then
Return Nothing
End If
Dim lines As New List(Of String)
Using reader As New StringReader(source)
While reader.Peek() <> -1
lines.Add(reader.ReadLine())
End While
End Using
Return lines.AsReadOnly()
End Function
End Module
Then your code to read the lines from the clipboard would just look like this:
Dim clipboardContents As IList(Of String) = Clipboard.GetText().ReadAllLines()
There is a carriage return and line feed on each of your lines. character 10 and character 13 combine them into a string and split and you will get what you need.