String.Split Method optimize way in vb.net - 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

Related

VB.net regex exclude certain characters from string

I am using regex.ismatch to check a string doesn't contain any one of a list of characters such as £&+(/?!;:* And also a quotation mark " not sure how to place that...
But can't get to to work...
If Regex.ismatch(Line, "^[^##£&+()*']"). Then
Msgbox("error")
End If
But doesn't work for me?
Any suggestions
You could do this pretty easily without Regex by simply doing something like this:
Public Shared Function HasSpecialChars(ByVal str As String) As Boolean
Const specialChars As String = "!##$%^&*()"
Dim indexOf As Integer = str.IndexOfAny(specialChars.ToCharArray())
Return indexOf <> -1
End Function

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

Splitting string by two conditions in vb.net 2014

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

Collection to delimited string

when I do
String.Join(";", lst.Items)
I get a string of object descriptors instead of item of values.
But when I iterate the collection, I end up with a delimiter at the front or back and need to a Substring call afterwards.
Dim res As String = "" 'or use stringbuilder
For Each s As String In lst.Items
s &= ";" & s
Next
res = res.Substring(1)
This applies to other cases as well where you want to turn a shared property within a collection into a delimited string. Is there a nice way to do this?
Can I do this with LINQ and would it be faster?
You'll have to convert the items to strings then:
String.Join(";", lst.Items.Select(Function(item) item.ToString()));
How about
Dim res As String = String.Join(";", lst.Items.OfType(Of String))
This does work:
Dim col As New Collection
col.Add("One")
col.Add("Two")
col.Add("Three")
Dim res = String.Join(";", col.OfType(Of String))
See also this question

How to filter anything but numbers from a string

I want to filter out other characters from a string as well as split the remaining numbers with periods.
This is my string: major.number=9minor.number=10revision.number=0build.number=804
and this is the expected output: 9.10.0.804
Any suggestions?
As to my comment, if your text is going to be constant you can use String.Split to remove the text and String.Join to add your deliminators. Quick example using your string.
Sub Main()
Dim value As String = "major.number=9minor.number=10revision.number=0build.number=804"
Dim seperator() As String = {"major.number=", "minor.number=", "revision.number=", "build.number="}
Console.WriteLine(String.Join(".", value.Split(seperator, StringSplitOptions.RemoveEmptyEntries)))
Console.ReadLine()
End Sub
If your string does not always follow a specific pattern, you could use Regex.Replace:
Sub Main()
Dim value as String = "major.number=9minor.number=10revision.number=0build.number=804"
Dim version as String = Regex.Replace(value, "\D*(\d+)\D*", "$1.") ' Run the regex
version = version.Substring(0, version.Length - 1) ' Trim the last dot
End
Note you should Imports System.Text.RegularExpressions.