Solved

c# special format

Posted on 2015-02-03
2
46 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 19

Expert Comment

by:Daniel Van Der Werken
Comment Utility
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 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
crm development 2 35
How do I bind the results to a grid 3 22
Limiting string to two deciamls 18 30
ConsoleSql 1 12
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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now