简体   繁体   English

如何确定动态文本中子字符串的计数?

[英]How to determine the count for substring in dynamic text?

ok so I have this string. 好的,所以我有这个字符串。

Next: Warrant Officer Grade 2 下一个:2级准尉官
21,202 / 33,000 21,202 / 33,000
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. 现在“Warrent Officer Grade 2”以及数字可以是动态的(我从网页加载这些变量)如果有一个数字或数百个它不附加0的所以exp不到一千就会看起来比如“478”而不是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. 这将匹配以Next:开头的所有行Next:后跟名称, Grade word和level。 The captured values of name and level groups can be retrieved later 稍后可以检索捕获的namelevel组的值

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 正则表达式游乐场: Derek Slager的更好的.NET正则表达式测试程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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