How do I search in a string from ";" to ";"?
Dim strInput as String = "text1;text2;text3"
Solution should look like this:
strOutput1 = "text1"
strOutput2 = "text2"
strOutput3 = "text3"
The lenght of the single "parts" is not fix, strInput can also be like "12345;name;Christoph;"
I just want to get the parts in a own string.
Does anybody knows how to?
Quickest way is probably using SPLIT to populate an array then set the individual variables to each element in the array. The only issue that might arise is if you have ;'s inside the strings, but that would pretty much prevent anything not based on length.
Starting with your string:
Dim strInput as String = "text1;text2;text3"
Then do a split:
Dim inputArray() As String = Split(strInput, ";")
This will split your string up using ; as the delineator. So you will end up with this:
inputArray(0) = "text1"
inputArray(1) = "text2"
inputArray(2) = "text3"
More on split()...
https://msdn.microsoft.com/en-us/library/6x627e5f%28v=vs.90%29.aspx?f=255&MSPPError=-2147217396
Using .Split you can separate the original string into an array of strings like this
Dim strInput As String = "text1;text2;text3"
Dim tempStringList() As String = strInput.Split(";"c)
So now
tempStringList(0) contains "text1"
tempStringList(1) contains "text2"
and so on.
The beauty of .split is that it doesnt care how many sections there are to separate.
Related
I have two comma separated string. this String actually connected with database.
This the example of string what I actually want to use.
e.g.
Dim value As String = "One,Two,Three"
Dim mtr As String = ",,,"
I use the following code
Dim elements() As String = value.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
Dim Q_MTR() As String = mtr.Split(New Char() {","c}, StringSplitOptions.RemoveEmptyEntries)
For i As Integer = 0 To elements.Length - 1
MsgBox(elements(i) & " Mtr="& Q_MTR(i))
Next
I want to show the Output Like:
"One Mtr= "
"Two Mtr= "
"Three Mtr= "
But I got Error
"Index was outside the bounds of the array."
Can anyone please tell me how can I solve this prblm? in VB.net.
I think using Trim with StringSplitOptions.RemoveEmptyEntries doesn't work because " " isn't considered an empty entry. I need to do a normal split, then trim each item, then filter out the empty strings. So I change StringSplitOptions.RemoveEmptyEntries to StringSplitOptions.None.
I use this code and it's work :)
Dim Q_MTR() As String = mtr.Split(New Char() {","c}, StringSplitOptions.None)
I have a file name like below:
sub_fa__hotchkis_type1a__180310__PUO4x4__180813
I want to separate it with double underscores "__" and using this code:
Dim MdlNameArr() As String = Path.GetFileNameWithoutExtension(strProjMdlName).Split(New Char() {"__"}, StringSplitOptions.RemoveEmptyEntries)
myTool.Label9.Text = MdlNameArr(1).ToString
I expect the result will be "hotchkis_type1a" but it returns "fa".
It doesnt recognize single underscore "_".
Is there any method to use it properly?
You need to split on a string rather than just a character, so if we look at the available overloads for String.Split, we find the nearest one to that is String.Split(string(), options) which takes an array of strings as the separators and requires the inclusion of StringSplitOptions like this:
Dim s = "sub_fa__hotchkis_type1a__180310__PUO4x4__180813"
Dim separators() As String = {"__"}
Dim parts = s.Split(separators, StringSplitOptions.None)
If parts.Length >= 2 Then
Console.WriteLine(parts(1))
Else
Console.WriteLine("Not enough parts found.")
End If
Outputs:
hotchkis_type1a
i just wanna ask:
i have a label40.text
with a content of {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}
and i also have have a label39.text that will change its output everytime a certain changes happens.
My question is
How can i embed this simulation through a code?
If Label39.text = "a" then the content of label40.text "a" will be remove and the list of alphabets will be remain alphabetically.
I want that also to be happen anytime my label39.text will change its value "RANDOMLY"
Example if label39.text = "a,b,c,d,x,z" then
label40.text = "e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y"
this is my code so far
Dim patterns As String
patterns = Label39.Text
Dim tobefollow As String
tobefollow = Label40.Text
Dim matches As MatchCollection = Regex.Matches(patterns, tobefollow)
If Regex.IsMatch(patterns, tobefollow) Then
'this where i will put my code to make my example
End If
First of all, note that you are populating the patterns and tobefollow variables wrongly (you were doing it right in the other question); it should be:
patterns = Label40.Text
tobefollow = Label39.Text
Also bear in mind that what you want can easily be accomplished without relying on Regex; for example via:
If (Label40.Text.ToLower().Contains(Label39.Text.ToLower())) Then
'this where i will put my code to make my example
End If
Regarding what you want this time, you can rely on .Replace: .Replace("text to be deleted", "") will remove this letter; but you have also to account for the commas. Code to be put inside the condition:
Dim origString As String = Label40.Text
Label40.Text = Label40.Text.ToLower().Replace(Label39.Text.ToLower() & ",", "")
If (origString = Label40.Text) Then
'It means that it does not have any comma, that is, refers to the last letter
Label40.Text = Label40.Text.ToLower().Replace("," & Label39.Text.ToLower(), "")
End If
An alternate answer would to use the String.Split and the String.Join Methods to breakdown your strings into individual characters then remove them from the List and join them back together. Here is a Function that does that:
Private Function RemoveLetters(str1 As String, str2 As String) As String
Dim sep() As String = {","}
Dim list1 As List(Of String) = str1.Split(sep, StringSplitOptions.None).ToList
Dim list2 As List(Of String) = str2.Split(sep, StringSplitOptions.None).ToList
For Each s As String In list2
list1.Remove(s)
Next
Return String.Join(",", list1)
End Function
you would use it like this:
Label40.Text = RemoveLetters(Label40.Text, Label39.Text)
I have to following string:
Dim text As String = "userĖ˛upload"
I want to change the Unicode character #0332 to underscore. I have the following code for this:
Dim test As New Text.StringBuilder
test.Append(text.Replace("#0332", "_"))
Dim normalizedUrl As String = test.ToString()
However it does not work, my "test" string has the same value as "text" variable. Anyone has an idea what can be wrong?
You can;
text.Replace(ChrW(&H332), "_")
Right - to start with, I'm entering unfamiliar areas with this - so please be kind!
I have a script that looks a little something like this:
Private Function checkString(ByVal strIn As String) As String
Dim astrWords As String() = New String() {"bak", "log", "dfd"}
Dim strOut As String = ""
Dim strWord As String
For Each strWord In astrWords
If strIn.ToLower.IndexOf(strWord.ToLower, 0) >= 0 Then
strOut = strWord.ToLower
Exit For
End If
Next
Return strOut
End Function
It's function is to check the input string and see if any of those 'astrWords' are in there and then return the value.
So I wrote a bit of code to dynamically create those words that goes something like this:
Dim extensionArray As String = ""
Dim count As Integer = 0
For Each item In lstExtentions.Items
If count = 0 Then
extensionArray = extensionArray & """." & item & """"
Else
extensionArray = extensionArray & ", ""." & item & """"
End If
count = count + 1
Next
My.Settings.extensionArray = extensionArray
My.Settings.Save()
Obviously - it's creating that same array using list items. The output of that code is exactly the same as if I hard coded it - but when I change the first bit of code to:
Dim astrWords As String() = New String() {My.Settings.extensionArray}
instead of:
Dim astrWords As String() = New String() {"bak", "log", "dfd"}
It starts looking for the whole statement instead of looping through each individual one?
I think it has something to do with having brackets on the end of the word string - but I'm lost!
Any help appreciated :)
When you use the string from the settings in the literal array, it's just as if you used a single strings containing the delimited strings:
Dim astrWords As String() = New String() {"""bak"", ""log"", ""dfd"""}
What you probably want to do is to put a comma separated string like "bak,log,dfd" in the settings, then you can split it to get it as an array:
Dim astrWords As String() = My.Settings.extensionArray.Split(","C)
You need to set extensionArray up as a string array instead of simply a string.
Note that
Dim something as String
... defines a single string, but
Dim somethingElse as String()
... defines a whole array of strings.
I think with your code, you need something like:
Dim extensionArray As String() = new String(lstExtensions.Items)
Dim count As Integer = 0
For Each item In lstExtentions.Items
extensionArray(count) = item
count = count + 1
Next
My.Settings.extensionArray = extensionArray
My.Settings.Save()
Then at the start of checkString you need something like
Private Function checkString(ByVal strIn As String) As String
Dim astrWords As String() = My.Settings.extensionArray
...
There also might be an even easier way to turn lstExtentions.Items into an Array if Items has a 'ToArray()' method, but I'm not sure what Type you are using there...
What you've done is created a single string containing all 3 words. You need to create an array of strings.
New String() {"bak", "log", "dfd"}
means create a new array of strings containing the 3 strings values "bak", "log" and "dfd".
New String() {My.Settings.extensionArray}
means create a new array of strings containing just one value which is the contents of extensionArray. (Which you have set to ""bak", "log", "dfd""). Note this is one string, not an array of strings. You can't just create 1 string with commas in it, you need to create an array of strings.
If you want to create your array dynamically, you need to define it like this:
Dim astrWords As String() = New String(3)
This creates an array with 3 empty spaces.
You can then assign a string to each space by doing this:
astrWords(0) = "bak"
astrWords(1) = "log"
astrWords(2) = "dfd"
You could do that bit in a for loop:
Dim count As Integer = 0
For Each item In lstExtentions.Items
astrWords(count) = item
count = count + 1
Next
Alternatively, you could look at using a generic collection. That way you could use the Add() method to add multiple strings to it
I think you want your extensionArray to be of type String() and not String. When you are trying to initialize the new array, the initializer doesn't know to parse out multiple values. It just sees your single string.