1

I have a string of the format

[00:26:19] Completed 80000 out of 500000 steps  (16%)

from which I want to get the 16 part.

Should I search for ( and then get the % and get the portion in between, or would it be wiser to set up a regex query?

1
  • If I'm in your place I would go for regex Commented Feb 10, 2011 at 2:10

4 Answers 4

6

RegEx is probably going to be the trend, but I don't see a good reason for it, personally.

That being said, this should work:

String s = "[00:26:19] Completed 80000 out of 500000 steps  (16%)";
Int32 start = s.LastIndexOf('(') + 1;
Console.WriteLine(s.Substring(start,s.LastIndexOf('%')-start));

And you can Convert.ToInt32() if you feel it necessary.

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

3 Comments

+1. If the string will always be in this format then this will always work. Why go for a solution that's slower and more complicated?
I agree with you on this case. If the format of the string is fixed and not likely to change, doing the simple parsing you mentioned is probably the best way since you avoid the overhead of instantiate the regex engine.
For the reasons above, I believe good'ol'fashioned parsing is best. Assuming it doesn't change and is in a "standard format", string manipulation just seems a better fit.
5

I would use a regular expression like this:

([^%]+)%\)$

This expression would allow non-numeric data to be captured - if you are certain that the text within the parenthesis and just to the left of the percentage will always be a number you can simplify the expression to this:

(\d+)%\)$

2 Comments

Considering that we're talking about percentages, I would use [0-9] instead.
@Voo - It's best to not assume too much - the OP may need to contend with bad data.
2

Another Fast way is...

        string s = "[00:26:19] Completed 80000 out of 500000 steps  (16%)";
        string res = s.Split("(%".ToCharArray())[1];

this assumes we will only see '(' and '%' once in the string

3 Comments

I think that you probably meant ")%" as the first parameter to String.Split.
@Jason No, it's written as I meant it to be written... I am splitting the string on the following chars: ( and %.... which results as Element0="[00:26:19] Completed 80000 out of 500000 steps", Element1="16", Element2=")"
My mistake. Sorry. I like your solution.
1

It depends on how variable you expect the input string (the "haystack") to be, and how variable you expect your target pattern (the "needle") to be. Regexes are extremely useful for describing a whole class of needles in a largely unknown haystack, but they're not the right tool for input that's in a very static format.

If you know your string will always be something like:

"[A:B:C] Completed D out of E steps (F%)"

where 1) A-F are the only variable portions, and 2) A-F are always numeric, then all you need is a little string manipulation:

int GetPercentage(string str)
{  
    return int.Parse(
        str.Substring(
            str.IndexOf('(') + 1, 
            str.IndexOf('%') - str.IndexOf('(')
        )
    );
}

The key question here is: "Are the presence of ( and % sufficient to indicate the substring I'm trying to capture?" That is, will they only occur in that one position? If the rest of the haystack might contain ( or % somewhere, I'd use regex:

@"(?<=\()\d+(?=%\)))$"

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.