Link to home
Start Free TrialLog in
Avatar of agbalaji
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+Product1*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
Avatar of Babycorn-Starfish
Babycorn-Starfish
Flag of United States of America image

Hi there,

will your string always have text seperated by the symbols + * or /?

BCS
Avatar of agbalaji
agbalaji

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
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This example might be useful to you?
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfSystemStringClassSplitTopic.htm
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.RegularExpressions

....


    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+Product1*Product2/Product3")
        If matchx.Success Then
            MsgBox(matchx.Groups(1).Value)
            MsgBox(matchx.Groups(2).Value)
            MsgBox(matchx.Groups(3).Value)
            MsgBox(matchx.Groups(4).Value)
        End If
    End Sub