• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 217
  • Last Modified:

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
0
agbalaji
Asked:
agbalaji
1 Solution
 
Babycorn-StarfishCommented:
Hi there,

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

BCS
0
 
agbalajiAuthor Commented:
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
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Something like...

Public Class Form2
    Inherits System.Windows.Forms.Form

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ops As New ArrayList
        ops.Add("+")
        ops.Add("-")
        ops.Add("*")
        ops.Add("/")

        Dim StringText As String = "Metric+Product1*Product2/Product3"

        Dim A As New ArrayList

        Dim startPos As Integer
        Dim curChar As String
        Dim i As Integer
        Dim length As Integer
        startPos = 0
        For i = 0 To StringText.Length - 1
            curChar = StringText.Substring(i, 1)
            If ops.IndexOf(curChar) <> -1 Then
                If i <> startPos Then
                    A.Add(StringText.Substring(startPos, i - startPos))
                End If
                A.Add(curChar)
                startPos = i + 1
            End If
        Next
        If startPos <> StringText.Length Then
            A.Add(StringText.Substring(startPos))
        End If

        Dim part As String
        For Each part In A
            Debug.WriteLine(part)
        Next
    End Sub
End Class
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
amyhxuCommented:
This example might be useful to you?
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfSystemStringClassSplitTopic.htm
0
 
agbalajiAuthor Commented:
Fantastic and quick..thanks a zillion Idlemind and amyhxu..the link does not work on my PC...thanks for the quick reply though
0
 
S-TwilleyCommented:
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
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now