?
Solved

500 Points Unanswered C# question+String parsing

Posted on 2005-03-10
6
Medium Priority
?
216 Views
Last Modified: 2010-04-23
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
Comment
Question by:agbalaji
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 11

Expert Comment

by:Babycorn-Starfish
ID: 13511670
Hi there,

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

BCS
0
 

Author Comment

by:agbalaji
ID: 13511706
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 2000 total points
ID: 13511805
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!

 
LVL 14

Expert Comment

by:amyhxu
ID: 13511811
This example might be useful to you?
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.1033/cpref/html/frlrfSystemStringClassSplitTopic.htm
0
 

Author Comment

by:agbalaji
ID: 13511852
Fantastic and quick..thanks a zillion Idlemind and amyhxu..the link does not work on my PC...thanks for the quick reply though
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13511972
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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question