简体   繁体   中英

How to determine the count for substring in dynamic text?

ok so I have this string.

Next: Warrant Officer Grade 2
21,202 / 33,000
21,202 / 33,000

It is a substring I have obtained from a long thing of text. Now "Warrent Officer Grade 2" as well as the numbers can be dynamic (I am loading these variables from a webpage) If there is a number in the ones or hundreds it does not append 0's so the exp for less than a thousand would look like "478" instead of 00,478.

Now my question is how to I obtain just "Warrant Officer Grade 2"? I already have this

int indexOfNext = myString.IndexOf("Next") + 6;  
//Below is what I need to obtain  
int count = 0;  
string newString = myString.Substring(indexOfNext, count);

But how would I help determine the count for getting the rank? or would It just be simple to create an array with all the ranks and the length of each rank?

You can use regular expression to search for the name and level for example

var regExp = new Regex(@"^Next: (?<name>[^\d]+) Grade (?<level>[\d]+)");

this will match all lines starting with Next: followed with the name, Grade word and level. The captured values of name and level groups can be retrieved later

var match = regExp.Match(input);
var level = match.Groups["level"].Value; 

or modify it the way you want it.

Regex playground: Derek Slager's A Better .NET Regular Expression Tester

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM