Solved

c# special format

Posted on 2015-02-03
2
55 Views
Last Modified: 2015-02-14
my string x will come from xml file and I do not know how many items will be there. may 10, may 100.
And now I want to extract as below. How can I do that in c#?


string x = [KDAV(100),KDX(200),CSGW(300)]

extract to

string model = "KDAV,KDX,CSGW";
string qty = "100,200,300";
0
Comment
Question by:ITsolutionWizard
2 Comments
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 40587821
This works:

       
static void Main(string[] args)
        {
            string x = "[KDAV(100),KDX(200),CSGW(300)]";

            x = x.Replace("[", string.Empty);
            x = x.Replace("]", string.Empty);
            x = x.Replace(")", string.Empty);
            string[] y = x.Split(new char[] { ',' });

            string model = string.Empty;
            string qty = string.Empty;

            for (int i = 0; i < y.Length; i++)
            {
                string[] z = y[i].Split(new char[] { '(' });
                model += z[0] + ",";
                qty += z[1] + ",";
            }

            model = model.Remove(model.Length - 1, 1);
            qty = qty.Remove(qty.Length - 1, 1);
        }

Open in new window

0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40587845
Hi ITsolutionWizard;

This salution assumes that the model can be from 1 - 4 characters and the quantity can have a value from 0 - 999. If that is not correct let me know and I will modify the regular expression

// Test Data
string x = "[KDAV(100),KDX(200),CSGW(300)]";
// Remove the [ and ] around the string
x = x.Trim(new char[] {'[',']'});
// Parse out the model and qty for each value using Regular Expression
var matches = Regex.Matches(x, @"([a-zA-Z]{1,4})\((\d{1,3})\)");

// Fill the result strings with the values
string model = "";
string qty = "";
foreach( Match m in matches )
{
    model += m.Groups[1] + ",";;
    qty += m.Groups[2] + ",";
}

// Remove the last comma in the string
model = model.Trim(new char[] {','});
qty = qty.Trim(new char[] {','});

Open in new window

0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

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

Suggested Solutions

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project

808 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