stringparsing
-
Hy everyone! I want to do some stringparsing, but well I seem to having done to much programming in C# and C/C++. So I guess I start mixing things. So what I want to do is the following: I do have an input in an Excel Cell which looks like "xxx-yyyy" or maybe even blanks between them like "xxx - yyy" etc. xxx and yyy represent integers. So now I do want to split this string to get xxx and yyy in seperate integer variables. Well I guess it is quit simple, but at the moment I seem to be mixing things. I know how to do this in C# etc (parsing the array of char) but well... the info I collected from the sheet is stored in a variant variable, because the user should be allowed to either enter "xxx" or "xxx-yyy". Thanks for your help. Stephan.
-
Hy everyone! I want to do some stringparsing, but well I seem to having done to much programming in C# and C/C++. So I guess I start mixing things. So what I want to do is the following: I do have an input in an Excel Cell which looks like "xxx-yyyy" or maybe even blanks between them like "xxx - yyy" etc. xxx and yyy represent integers. So now I do want to split this string to get xxx and yyy in seperate integer variables. Well I guess it is quit simple, but at the moment I seem to be mixing things. I know how to do this in C# etc (parsing the array of char) but well... the info I collected from the sheet is stored in a variant variable, because the user should be allowed to either enter "xxx" or "xxx-yyy". Thanks for your help. Stephan.
I just found out myself. I have to use the Split(string) operation but is there any way to split the string without having blanks to seperate it? If anyone of you knows how to split "123-234" into 123 and 234 (meaning the two integers) then please let me know. At the moment I am able to split "123 - 234" into "123" "-" and "234" but well, if there are no blanks inbetween my programm will crash. And I really would like to avoid this :) Thanks. Stephan.
-
Hy everyone! I want to do some stringparsing, but well I seem to having done to much programming in C# and C/C++. So I guess I start mixing things. So what I want to do is the following: I do have an input in an Excel Cell which looks like "xxx-yyyy" or maybe even blanks between them like "xxx - yyy" etc. xxx and yyy represent integers. So now I do want to split this string to get xxx and yyy in seperate integer variables. Well I guess it is quit simple, but at the moment I seem to be mixing things. I know how to do this in C# etc (parsing the array of char) but well... the info I collected from the sheet is stored in a variant variable, because the user should be allowed to either enter "xxx" or "xxx-yyy". Thanks for your help. Stephan.
now that i ponder on it a bit, when i use the split function in c# i simply do the following - myString.split('-')[0] //gets xxx myString.split('-')[1] //gets yyy I wonder if you couldn't do the same for vb.net i.e. myString.split('-')(0) //gets xxx myString.split('-')(1) //gets yyy
-
now that i ponder on it a bit, when i use the split function in c# i simply do the following - myString.split('-')[0] //gets xxx myString.split('-')[1] //gets yyy I wonder if you couldn't do the same for vb.net i.e. myString.split('-')(0) //gets xxx myString.split('-')(1) //gets yyy
You can but VB.net likes you to specify that the splitter character is of type char. Also the single quote is the comment character in VB.Net so you have to use double quotes. myString.split("-"c)(0) 'gets xxx myString.split("-"c)(1) 'gets yyy If you have knowledge, let others light their candles at it. Margaret Fuller (1810 - 1850)
-
I just found out myself. I have to use the Split(string) operation but is there any way to split the string without having blanks to seperate it? If anyone of you knows how to split "123-234" into 123 and 234 (meaning the two integers) then please let me know. At the moment I am able to split "123 - 234" into "123" "-" and "234" but well, if there are no blanks inbetween my programm will crash. And I really would like to avoid this :) Thanks. Stephan.
Why use split? Just use SubString: Dim a As Integer Dim b As Integer Dim s As String = "123 - 456" Dim i As Integer = s.IndexOf("-") If i < 0 Then 'must be in xxx form a = Integer.Parse(s) Else 'in xxx-yyyy form, so parse a = Integer.Parse(s.Substring(0, i)) b = Integer.Parse(s.Substring(i + 1, s.Length - i - 1)) End If This will work if spaces or no spaces. If you need the strings rather than the integers, just use stringa = s.Substring(0, i).Trim. The trim will remove leading or trailing spaces.
-
Why use split? Just use SubString: Dim a As Integer Dim b As Integer Dim s As String = "123 - 456" Dim i As Integer = s.IndexOf("-") If i < 0 Then 'must be in xxx form a = Integer.Parse(s) Else 'in xxx-yyyy form, so parse a = Integer.Parse(s.Substring(0, i)) b = Integer.Parse(s.Substring(i + 1, s.Length - i - 1)) End If This will work if spaces or no spaces. If you need the strings rather than the integers, just use stringa = s.Substring(0, i).Trim. The trim will remove leading or trailing spaces.
Scott Serl wrote: a = Integer.Parse(s.Substring(0, i)) Well, seams as if VBA for Excel does not support "Integer.Parse" Or does it? At the moment I am on Office 2000 but might soon be updating to Office 2003. I had a look and the help says there is only a normal parse operation. The examples says parsing a telephonenumber and seperating it into two Cells (similar to my example). Maybe I could use this one ... Or was your example supposed for .NET? Because actually I do not have this installed here and at the moment I do not have access to it. So I have to get along with what Office is offering 'til I will be able to use .NET Stephan.
-
Scott Serl wrote: a = Integer.Parse(s.Substring(0, i)) Well, seams as if VBA for Excel does not support "Integer.Parse" Or does it? At the moment I am on Office 2000 but might soon be updating to Office 2003. I had a look and the help says there is only a normal parse operation. The examples says parsing a telephonenumber and seperating it into two Cells (similar to my example). Maybe I could use this one ... Or was your example supposed for .NET? Because actually I do not have this installed here and at the moment I do not have access to it. So I have to get along with what Office is offering 'til I will be able to use .NET Stephan.
Sorry, In Excel, it would be something like: value = "123 - 456" Pos = Instr(value,"-") If Pos > 0 Then 'I think position starts at 1 in Excel a = Val(Left(value, pos - 1)) b = Val(Right(value, Len(value) - Pos)) Else a = val(value) End If I can't check at this computer because I don't have anything with old vb on it here.