?
Solved

c# special format

Posted on 2015-02-03
2
Medium Priority
?
72 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
[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
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 2000 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

DFW AZURE MEETUP TONIGHT FRI 6PM

We will be discussing what Azure Stack is, how does it fit into the suit of offerings that Azure has currently, and where can it fit into your organizations technology stack. We will also be discussing limitations of the platform while covering various applicable scenarios.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 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