VB.NET string manipulation - vb.net

Looking for the simplest way to extract values from a string. For example consider the following:
Dim args As String = "/firstname:Bob /lastname:Jones"
To simplify, I need to be able to popup a box that says "Firstname = Bob" or "Lastname = Jones"

Have you tried using the Split method on a string. It should look something like this:
Dim arr() as string
arr=args.Split("/")
Dim i as integer
For i=0 to arr.GetLength(0)
arr(i)=arr(i).Trim.Replace(":", "=")
Next

I would use the Split function to create an array of words, then read them in order:
char[] sep = new char[2];
sep[0] = '/';
sep[1] = ':';
string values = "/firstname:Bob /lastname:Jones";
string[] sites = values.Split(sep);
foreach (string s in sites) {
Console.WriteLine(s);
}
This post is also useful!
http://www.techrepublic.com/article/easily-parse-string-values-with-net/6030362

Using Regex this pattern can help:
(?<identifier>[a-z]+)(?<value>[a-z]+)
See how it works.
You can iterate on all groups and can extract identifier and value.

Related

How to separate data with a comma

For example, I'm having a two set of vars with data type of string:
users = "Admin, Staff"
pass = "202cb9, caf1a"
These are vars with only normal string data type. The two vars above is generated, so I could only get these kind of data. The question is:
How can I separate those data by the commas (like Admin -> 202cb9, Staff -> caf1a) and then store them into an array.
users_array(0) = "Admin"
users_array(1) = "Staff"
pass_array(0) = "202cb9"
pass_array(1) = "caf1a"
Thank you.
You can use users.Split(New Char() {","c}) as in this link.
http://www.dotnetperls.com/split-vbnet
I have two options to solve this problem:
Dim users_array() As String = users.Split(New String() {", "}, StringSplitOptions.RemoveEmptyEntries)
and:
Dim pass_array() As String = Split(users, ", ")
IMO, it is better to use ,<space> as a separator string instead of just ,, to avoid getting <space>staff at index 1.
Here the first solution works for both C# and VB.Net and second one is specific to VB.Net.
User String.Split to break strings apart on a specific character. It returns an array.

Remove the character alone from the string

My code retrieves the data from various resources .
And output will be like below
UNY4/4/2010
hds04/5/2010
saths04/22/2013
But I want the output like this
4/4/2010
4/5/2010
04/22/2013
Is there any way to do this ?
You need to use a regular expression that finds all uppercase and lowercase characters and replaces them with a blank, like this:
Dim rgx As New Regex("[a-zA-Z]")
str = rgx.Replace(str, String.Empty)
An alternate solution is to look for the first numeric digit, then discard all text before that.
Function GetDate(data As String) As Date
Dim indexFirstNum As Integer = data.IndexOfAny("0123456789".ToCharArray())
Dim datePortion As String = data.Substring(indexFirstNum)
Return Date.Parse(datePortion)
End Function

Trying to split up a string separated with ; VB.NET

I'm writing a musical transposition application (in simple VB Console) and I want the program to split up a string of different notes separated with ;'s, looking like this:
'C blues scale
Dim strNotesString As String = "C; Eb; F; F#; G; Bb"
Can anyone recommend a way I can achieve this? I would put each separate note into an array.
Dim strTmpNotes() As String
strTmpNotes(0) = "C"
strTmpNotes(1) = "Eb"
'And so on
Thanks in advance
Nick
Maybe something like this:
Dim strTmpNotes() As String = strNotesString.Replace(" ", "").Split(";"c)
You could also use regular expressions:
Dim strTmpNotes() As String = New Regex(";\s*").Split(strNotesString)
I prefer the latter.
Use String.Split:
Dim strNotesString As String = "C; Eb; F; F#; G; Bb"
Dim strTmpNotes() As String = strNotesString.Split("; ")
Checkout the String.Split method.
Dim strNotesString As String = "C; Eb; F; F#; G; Bb"
Dim notes() As String = strNotesString.Split("; ")
Very simple solution, as .NET has this capability built in.
strTmpNotes = strNotesString.Split(';')
Then run a Trim on each of the individual strings in the resulting array using a for loop.

split string, VB.net?

I have a file txt file that holds 3 values each seperated by a space how can i assign each value to its own variable and use that for other things?
as example the numbers might be displayed in the text file as:
-1100.02 -1958.19 0.0
Translating Marco’s C# code to VB:
Dim s As String = File.ReadAllText(filename)
Dim nums As String() = s.Split(" "c)
To get numbers, you need to parse the strings separately. You can use Linq to do this:
Dim numbers As Double() = From num In nums Select Double.Parse(num)
In C#:
string s = File.ReadAllText(filename);
string[] nums = s.Split(' ');
So you can access nums[index] where index should be between 0 and 2.
Note that you MUST check if everything went ok...
If you need you can also try:
foreach (string num in nums)
{
double d = double.Parse(num);
// Here you can do what you want with d
}
Try this:
Dim line as String = "-1100.02 -1958.19 0.0"
Dim values() as Double = Array.ConvertAll(line.Split(New Char() { " "c }, StringSplitOptions.RemoveEmotyEntries), AddressOf Convert.ToDouble)
This will result in values being filled with numbers from the input string (assuming a set of valid numbers on each line).
dim strSplitted() as string = Line.split(" "c)
' strSplitted(0), strSplitted(1) and strSplitted(2) will hold the values.
Line is the line in the file ofcourse :-)
update: code updated according to comments.

How to split in VB.NET

I am using VB.NET code.
I have got the below string.
http://localhost:3282/ISS/Training/SearchTrainerData.aspx
Now I want to split the above string with "/" as well. I want to store the value SearchTrainerData.aspx in a variable.
In my case
Dim str as String
str = "SearchTrainerData.aspx"
What would be the code which will split the above string and further store it in a variable?
Try to use the function String.Split.
Your "string" is obviously a URL which means you should use the System.Uri class.
Dim url As Uri = New Uri("http://localhost:3282/ISS/Training/SearchTrainerData.aspx")
Dim segments As String() = url.Segments
Dim str As String = segments(segments.Length - 1)
This will also allow you to get all kinds of other interesting information about your "string" without resorting to manual (and error-prone) parsing.
The Split function takes characters, which VB.NET represents by appending a 'c' to the end of a one-character string:
Dim sentence = "http://localhost:3282/ISS/Training/SearchTrainerData.aspx"
Dim words = sentence.Split("/"c)
Dim lastWord = words(words.length - 1)
I guess what you are actually looking for is the System.Uri Class. Which makes all string splitting that you are looking for obsolete.
MSDN Documentation for System.Uri
Uri url = new Uri ("http://...");
String[] parts = url.Segments;
Use split(). You call it on the string instance, passing in a char array of the delimiters, and it returns to you an array of strings. Grab the last element to get your "SearchTrainerData.aspx."