agbalaji
asked on
500 Points Unanswered C# question+String parsing
Hi Peeps,
Here is the deal:
I have a string with a value as follows:
StringText="Metric+Product 1*Product2 /Product3"
Now,I have to parse that string and store the letters and operators CONTIGUOSLY in an array. In other sense, I have to have a string array for eg say: A[20]
A[0] should store Metric
A[1] should store +
A[2] should store Product1
A[3] should store *
A[4] should store Product2
A[5] should store /
A[6] should store Product3
I need a C# and VB.NET code. Please answer this ASAP
Here is the deal:
I have a string with a value as follows:
StringText="Metric+Product
Now,I have to parse that string and store the letters and operators CONTIGUOSLY in an array. In other sense, I have to have a string array for eg say: A[20]
A[0] should store Metric
A[1] should store +
A[2] should store Product1
A[3] should store *
A[4] should store Product2
A[5] should store /
A[6] should store Product3
I need a C# and VB.NET code. Please answer this ASAP
ASKER
Yup,
But I dont know where they occur in the string, there may be a situation where a string is in the following way---->" Metric+Product1*Product2 " or sometimes
"Metric*Product1-Product2* Product3". The order of occurence of operators is unknown
Thanks
BJ
But I dont know where they occur in the string, there may be a situation where a string is in the following way---->" Metric+Product1*Product2 " or sometimes
"Metric*Product1-Product2*
Thanks
BJ
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
This example might be useful to you?
ms-help://MS.VSCC.2003/MS. MSDNQTR.20 03FEB.1033 /cpref/htm l/frlrfSys temStringC lassSplitT opic.htm
ms-help://MS.VSCC.2003/MS.
ASKER
Fantastic and quick..thanks a zillion Idlemind and amyhxu..the link does not work on my PC...thanks for the quick reply though
Just a diff solution... although Idle has already solved this for you. (This assumes splitting characters come in the order + * and /
Imports System.Text.RegularExpress ions
....
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strParse As String = ""
strParse &= "(?<1>[^\+]*)\+"
strParse &= "(?<2>[^\*]*)\*"
strParse &= "(?<3>[^\/]*)\/"
strParse &= "(?<4>.*)"
Dim regParse As New Regex(strParse)
Dim matchx As Match = regParse.Match("Metric+Pro duct1*Prod uct2/Produ ct3")
If matchx.Success Then
MsgBox(matchx.Groups(1).Va lue)
MsgBox(matchx.Groups(2).Va lue)
MsgBox(matchx.Groups(3).Va lue)
MsgBox(matchx.Groups(4).Va lue)
End If
End Sub
Imports System.Text.RegularExpress
....
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strParse As String = ""
strParse &= "(?<1>[^\+]*)\+"
strParse &= "(?<2>[^\*]*)\*"
strParse &= "(?<3>[^\/]*)\/"
strParse &= "(?<4>.*)"
Dim regParse As New Regex(strParse)
Dim matchx As Match = regParse.Match("Metric+Pro
If matchx.Success Then
MsgBox(matchx.Groups(1).Va
MsgBox(matchx.Groups(2).Va
MsgBox(matchx.Groups(3).Va
MsgBox(matchx.Groups(4).Va
End If
End Sub
will your string always have text seperated by the symbols + * or /?
BCS