3

I have a

string CCstring = "CC01=50 CC02=300 CC03=500 CC04=40";

I want to store the individual values in seperate strings like:

for(int i = 0; i<=4; i++)
{
   string suffix = i.ToString().PadLeft(2, '0');
   string CCindividual = CCindividual + i;
   CCindividual = //THIS IS WHERE I WOULD LIKE TO GET MY INDIVIDUAL VALUES i.e 50,300,500,40;
    Console.WriteLn("CC" + i + " =" + CCIndividual);//Testing
}

Which string manipulation should I use Regex or Substring. How would the code snippet look like?

5
  • 4
    Old chinese proverb: When face with problem, programmer may decide to use Regex. Programmer now have 2 problem. Commented Nov 2, 2011 at 14:28
  • @Jamiec: Didn't know that Jamie Zawinski was chinese :-p (regex.info/blog/2006-09-15/247) Commented Nov 2, 2011 at 14:53
  • @Joachim - funnily enough I kinda knew that wasnt really a chinese proverb. It's just funnier framed as one. Commented Nov 2, 2011 at 14:57
  • 1
    only true if you don't know how and when to use them... Commented Nov 2, 2011 at 15:50
  • @Jamiec: I agree and disagree with you! Commented Nov 2, 2011 at 19:05

6 Answers 6

3

One line:

string[] CCindividual = Regex.Split(CCstring, "CC[0-9]+=").Where(x => x != "").
    Select(x => x.Trim()).ToArray<String>();

Not sure this is the more efficient way though.

Sign up to request clarification or add additional context in comments.

7 Comments

Thank you. The snippet: string[] CCindividual = Regex.Split(tempComms, "CC[1-9]+="); worked perfectly. I also have other generated elements like CCundefined which are created by JS on unused inputs. Regex elimated that too. Other solutions described here were making it too complicated then its actually is. I know Regex has a beautiful way to simplify complex string operation. Just could not make one. Thanks again.
The edited version works too. Can you explain what is different in the edited version from the original version and why was it made?
@SameerShah: I added .Where(x => x != "") to remove empty values that you can obtain after doing the Regex.Split, and Select(x => x.Trim()) to remove any trailing spaces (without it, you will obtain 50_, 300_, 500_ and 40, _ representing a space here).
I got it. I also would like to remove any alphabets For Eg: string tempComms = "CC01=500 CC02=50 CC03=400 CC04=undefined CC06=Blah CC07=4Blah"; string[] CCIndividualEdited = Regex.Split(tempComms, "CC[0-9]+=").Where(x => x != "").Where(x => x != "^[a-zA-Z]+$").Select(x => x.Trim()).ToArray<String>(); How should I do that.
You mean like this: string[] Comm = Regex.Split(tempComms, "CC[1-9]+=").Where(x => x != "").Where(x => x = Regex.Match( "[0-9]+")).Select(x => x.Trim()).ToArray<String>();
|
2

Can't you use split to first split on spaces and next on '='? It's easier than regex or substring imho.

Comments

2

Neither. You can use string.Split to get an array:

string CCstring = "CC01=50 CC02=300 CC03=500 CC04=40";
string[] strings = CCstring.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

After that, you are able to do the same for the = using string.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries);.

1 Comment

You then have to remove "CCXX=".
1

Unless you need this code to run very very efficiently. You should be worried about what is readable for you and your team (sometimes thats substring, split etc and sometimes thats regex). Only you can really decide.

Comments

1

just use String.Split

        string CCstring = "CC01=50 CC02=300 CC03=500 CC04=40";
        var result = CCstring.Split(' ')
            .Select(s => s.Split('='))
            .ToDictionary(kv => kv[0], kv => Convert.ToInt64(kv[1]));

Comments

0

Looking at your CCstring it will be definitely faster to walk through the string characters just once. Sure it doesn't worth that until you have tons of such strings.

So, yep, it's easier to use just string.Split once for spaces, and once for each fragmet to split by '='.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.