c# - Getting substring for string -
i have string of format
[00:26:19] completed 80000 out of 500000 steps (16%)
from want 16
part.
should search ( , % , portion in between, or wiser set regex query?
it depends on how variable expect input string (the "haystack") be, , how variable expect target pattern (the "needle") be. regexes extremely useful describing whole class of needles in largely unknown haystack, they're not right tool input that's in static format.
if know string like:
"[a:b:c] completed d out of e steps (f%)"
where 1) a-f variable portions, , 2) a-f numeric, need 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 presence of (
, %
sufficient indicate substring i'm trying capture?" is, occur in 1 position? if rest of haystack might contain (
or %
somewhere, i'd use regex:
@"(?<=\()\d+(?=%\)))$"
Comments
Post a Comment