Solved

c# special format

Posted on 2015-02-03
2
49 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 62

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Please explain "Multi-Tenant Services" 5 60
Error on Add method 1 36
C# Json POSt to Rest API 4 31
free scanner TWAIN can be operated with a Web application 9 34
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

937 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

10 Experts available now in Live!

Get 1:1 Help Now