[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

c# special format

Posted on 2015-02-03
2
Medium Priority
?
80 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 64

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
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.
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

650 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