Trying to split up a string separated with ; VB.NET - 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.

Related

vb.net split last part of the string

net 2.0 and need to split the last / mark of the string. Currently I have a code that says Dim test As String = "Software\Microsoft\Windows\Welcome" and need a code that will split that Software\Microsoft\Windows\Welcome into two separate section at the end
So I'll have Software\Microsoft\Windows and Welcome as new strings.
I can only find things that will split the beginning from the rest like
Dim whole As String = "Software/Microsoft/Windows/Run"
Dim firstpart As String = whole.Substring(0, whole.IndexOf("/"))
Dim lastpart As String = whole.Substring(whole.IndexOf("/") + 1)`
Use String.LastIndexOf()
Dim whole As String = "Software/Microsoft/Windows/Run"
Dim firstpart As String = whole.Substring(0, whole.LastIndexOf("/"))
Dim lastpart As String = whole.Substring(whole.LastIndexOf("/") + 1)
Try splitting with '\' As your delimeter and store it as a string array. Then just grab the last element and it should be "Welcome".

VB.NET string manipulation

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.

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.

Left of a character in a string in vb.net

say if I have a string 010451-09F2
How to I get left of - from the above string in vb.net
I want 010451
The left function doesn't allow me to specify seperator character.
Thanks
Given:
Dim strOrig = "010451-09F2"
You can do any of the following:
Dim leftString = strOrig.Substring(0, strOrig.IndexOf("-"))
Or:
Dim leftString = strOrig.Split("-"c)(0) ' Take the first index in the array
Or:
Dim leftString = Left(strOrig, InStr(strOrig, "-"))
' Could also be: Mid(strOrig, 0, InStr(strOrig, "-"))
Dim str As String = "010451-09F2"
Dim leftPart As String = str.Split("-")(0)
Split gives you the left and right parts in a string array. Accessing the first element (index 0) gives you the left part.
Sorry not sure on the vb syntax, but the c# is
string mystring ="010451-09F2";
string whatIwant = mystring.Split('-')[0];
Get the location of the dash first (or do it inline), and use that value for the left. This is old school VBA, but it'll be something like this:
Left(YourStringWithTheDash, InStr(YourStringWithTheDash)-1)
dim s as String = "010451-09F2"
Console.WriteLine(s.Substring(0, s.IndexOf("-")))
Console.WriteLine(s.Split("-")(0))
Use something like this:
Mid("010451-09F2",1,InStr("-"))
Dim sValue As String = "010451-09F2"
Debug.WriteLine(sValue.Substring(0, sValue.IndexOf("-"c)))
This helped
Dim str1 as string = me#test.com
Dim str As String
str = Strings.Left(str1, str1.LastIndexOf("#"))

String substring function

How can i get the string within a parenthesis with a custom function?
e.x. the string "GREECE (+30)" should return "+30" only
There are some different ways.
Plain string methods:
Dim left As Integer = str.IndexOf('(')
Dim right As Integer= str.IndexOf(')')
Dim content As String = str.Substring(left + 1, right - left - 1)
Regular expression:
Dim content As String = Regex.Match(str, "\((.+?)\)").Groups[1].Value
For the general problem, I'd suggest using Regex. However, if you are sure about the format of the input string (only one set of parens, open paren before close paren), this would work:
int startIndex = s.IndexOf('(') + 1;
string result = s.Substring(startIndex, s.LastIndexOf(')') - startIndex);
With regular expressions.
Dim result as String = System.Text.RegularExpressions.Regex.Match("GREECE (+30)", "\((?<Result>[^\)]*)\)").Groups["Result"].Value;
Code is not tested, but I expect only compilation issues.
You could look a regular expressions, or otherwise play with the IndexOf() function
In Python, using the string index method and slicing:
>>> s = "GREECE(+30)"
>>> s[s.index('(')+1:s.index(')')]
'+30'