How to split a String using multiple delimeters - vb.net

I'm using a very particular program that doesn't have many pre loaded functions on it. I would like to split a Date and time into individual variables. 6 variables to be exact
I get the date and time like this 5/28/2014 15:34:40 so I have 3 characters "/" " " and ":" separating the 6 variables I want, but I can only get one to work at a time. I have tried many other ways but the x.Split() is the only syntax that this program seems to like (PARCView).
Dim x as String = "5/28/2014 15:34:40"
Dim y as String() = x.Split(New Char() {":"c})
So how can I add all three at once or use consecutive steps to store each number as a unique variable?

You actually have the Char Array that you need, though it only has one delineator... just fill it with the other delineators and it will split it for you.
Dim y As String() = x.Split(New Char() {":"c, "/"c, " "c})
example:

Another option is to parse it as a datetime object which will contain all the parts as properties:
Dim teststr = "5/28/2014 15:34:40"
Dim dt = DateTime.Parse(teststr)

Related

using IndexOf in Mid function

Perhaps this is a simple solution for most, but I can't get this to work like it should according to syntax.
I have this line of text "Part Number123456Price$50.00"
I want to pull the part number out of it, so I use this function...
str = Mid(str, str.IndexOf("Part Number") + 12, str.IndexOf("Price"))
My results are str = "123456Price$50.0" every time. I know the part number can vary in length so I need a solid solution of pulling this out.
It can be confusing to mix the legacy VB string methods (such as Mid) with the .Net string methods (like IndexOf). The VB methods use 1 as the index of the first character while the .Net methods use 0.
The following code will extract the part number from a string
Dim str As String = "Part Number123456Price$50.00"
Dim iPart As Integer = str.IndexOf("Part Number") + 11
Dim iPrice As Integer = str.IndexOf("Price")
str = str.Substring(iPart, iPrice - iPart).Trim
The Mid() function of Visual Basic is documented as having three arguments: (1) a string, (2) the beginning location in the string, and (3) the number of characters to copy.
So if your string is "Part Number123456Price$50.00" and you want to pull the part number as a series of digits, the "123456" part of the string, using the Mid() function then you need to find the beginning of the part number digit string and to then know the number of digits.
If your string is in the variable str then you can find the offset by something like str.IndexOf("Number") + len("Number") which will provide the offset to after the string "Number".
Next you need to find the number of digits so you would do something like str.IndexOf("Price") to find where the text "Price" begins and then subtract from that offset the offset of where the digits begin.
The result of all of this is you need a bit of code something like the following. I have not tested this source as I am not a VB programmer so it may need a tweak and you might want to put some checks on data validity as well.
Dim TextNumber as String = "Number"
Dim TextPrice as String = "Price"
iOffset = str.IndexOf(TextNumber) + len(TextNumber)
str = Mid(str, iOffset, str.IndexOf(TextPrice) - iOffset)
Alternatively, if Price is always the format $00.00, this will also work.
Dim str as String = "Part Number123456Price$50.00"
str = str.Remove(str.IndexOf("Price"))

Using two array in single for each loop

In my application i have string like this 1,2,3&&4,5,6. Now i want check each and every element in single for each loop. Is it possible or not? If its possible how can i acheive this?.
Am trying using split method. But if i am using split method i want more than loop.
Like
dim sa as string=1,2,3&&4,5,6
for each x as string in sa.split("&&")
for each y as string in x.split(",")
''' Here My Process
next
next
How can over come this?. how can change to single loop?. It is possible or not?.
String.Split has an overload that accepts an array of string delimiters:
Dim input As String = "1,2,3&&4,5,6"
For Each element As String In input.Split({",", "&&"}, StringSplitOptions.None)
'do your processing on (1,2,3,4,5,6)
Next
As I understand, you want to use only one for each instead of using for each in for each.
You can first split by "&&" and then join with ",":
dim sa as string=1,2,3&&4,5,6
dim stringArray = String.Join(",", sa.split("&&")).split(",")
for each x as string in stringArray
end for
You could split using a regular expression as a delimiter:
Imports System.Text.RegularExpressions 'goes at the top of the module
For Each x As String In Regex.Split(sa, "\,|&&")
Where the regular expression means "comma or two ampersands". Note that you need to 'escape' the comma using a backslash; this is because commas do something special in regular expressions.
Don't forget to enclose your string in quotes:
dim sa as string="1,2,3&&4,5,6"
One option would be to split on the "," and the "&" via the Split method and ignore empty entries, as such:
Dim sa As String = "1,2,3&&4,5,6"
Dim split As String() = sa.Split(New Char() {",", "&"}, StringSplitOptions.RemoveEmptyEntries)
For Each value In split
Debug.WriteLine(value)
Next

Replacing nth occurrence of string

This should be fairly simple but I'm having one of those days. Can anyone advise me as to how to replace the first and third occurrence of a character within a string? I have looked at replace but that cannot work as the string could be of different lengths. All I want to do is replace the first and third occurrence.
There is an overload of the IndexOf method which takes a start position as a parameter. Using a loop you'll be able to find the position of the first and third occurences. Then you could use a combination of the Remove and Insert methods to do the replacements.
You could also use a StringBuilder to do the replacements. The StringBuilder has a Replace method for which you can specify a start index and a number of characters affected.
aspiringCoder,
Perhaps something like this might be useful to you (in line with what Meta-Knight was talking about <+1>)
Dim str As String = "this is a test this is a test this is a test"
Dim first As Integer
Dim third As Integer
Dim base As Integer = 0
Dim i As Integer
While str.length > 0
If i = 0 Then
first = str.IndexOf("test")
else if i = 2 Then
third = base + str.IndexOf("test")
end if
base = base + str.IndexOf("test")
str = str.Remove(0, str.IndexOf("test") + "test".length -1 )
i++
End While
It might have a one-off error somewhere...but this should at least get you started.

Need Assistance on getting LINK ID using Regular Expressions?

I got this url for example "http://www.yellowpages.com/manhattan-beach-ca/mip/marriott-manhattan-beach-4933923?lid=185795402"
I want to get the last digit numbers and the rest could be anything.
I need a format like this "http://www.yellowpages.com/anything.... lid=randomdigitnumbers" or as long as i get those numbers.
My knowledge is very poor in this regex thing so please guys help me.
the following did not work
Dim r As New System.Text.RegularExpressions.Regex("http://www.yellowpages.com/.*lid=d*", RegexOptions.IgnoreCase)
Dim m As Match = r.Match(txt)
If (m.Success) Then
Dim int1 = m.Groups(1)
MsgBox("(" + int1.ToString() + ")" + "")
End If
thank you in advance
Using Regular Expressions for this is a bit of overkill, IMO.
You could accomplish the same thing using string functions:
Dim url As String = "http://www.yellowpages.com/manhattan-beach-ca/mip/marriott-manhattan-beach-4933923?lid=185795402"
Dim queryString As String = url.SubString(url.IndexOf("?"), url.Length - url.IndexOF("?"))
Dim nameValuePairs As String() = queryString.Split("=")
Dim lid As String = nameValuePairs(1)
This is off the top of my head, so you may need to tweak it a bit. The basic concept is to the portion of the URL after the ? (the query string), and then split it on the = sign, taking the second element of the resulting array (the value).
Also, if the query string has more than one name value pair, they'll be separated by &, so you'll need to split on the ampersand (&) first, then the equal signs.
Just find lid= and get everything after that:
Dim url As String = "http://www.yellowpages.com/manhattan-beach-ca/mip/marriott-manhattan-beach-4933923?lid=185795402"
Dim lidIndex As Integer = url.IndexOf("lid=") + "lid=".Length
Dim lid As Integer = url.Substring(lidIndex)

VB: Need help splitting a string into 3 variables

I need to split one variable into 3 variables. For example I have a variable called Data and it contains AAA00000001. I need to have them be split between the "AAA", "0000000", and "1". I looked at the Split() function but didn't get a clear example for this situation. The reason I need to do this is because I want to store these 3 variables into fields for a MySQL database.
Are the three subvariables always of the same length?
If so, you can use Substrings:
Dim substring1 As String = Data.Substring(0, 3)
Dim substring2 As String = Data.Substring(3, 7)
Dim substring3 As String = Data.Substring(10, 1)
Assuming the string is ALWAYS the EXACT same length and need to be split at the SAME place, you can use Substring().
dim s as String = "AAA00000001"
dim s1 as String = s.Substring(0, 3)
dim s2 as String = s.Substring(3, 7)
dim s3 as String = s.Substring(10)
If they're not always the same length, you're probably going to need to use Regular Expressions.
Split will break your string apart based on a character, or group of. It's not appropriate here, that is unless you're always splitting on 0000000, which I doubt you are.
If you know that the first 3 characters will always be your first group, second 7 your next, and last character, your final group, you could do something like this.
This uses the Substring function, e.g.
Dim yourString as String = "AAA00000001"
Dim c1 As String = yourString.Substring(0, 3)
Dim c2 As String = yourString.Substring(3, 7)
Dim c3 As String = yourString.Substring(10, 1)