如何从字符串中提取子字符串? [英] How to I can extract substrings from a string ?
本文介绍了如何从字符串中提取子字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个输入字符串,想从中提取几个子字符串。我的输入字符串具有以下格式:
字符串 inputStr = substr1< strlabel1> substr2< strlabel2> substr3< strlabel3> ...
所以, substr包含每个字符和标点符号。!:?/} {] [\}(和空格
除了<或> charachers
和strlabel包含任何特征(w *)
例如:
string inputStr = 此< zm>是< vb>< aa>简单示例< jh>用于< ppr>您的< zm>
结果子字符串必须如下:
string [] substrs = { this, , , 简单示例 , for , };
string [] strlabels = { zm , vbb , aa , jh , ppr , zm };
如何从inputstring中提取每个substr和strlabel?
解决方案
< blockquote>使用正则表达式:
[^<>] +(?=(\<。*?\>)|
)
应该这样做。
这可以很简单:List< string> Words = new 列表< string>();
列表< string> Tags = new 列表< string>();
var splitItems = inputStr.Split( new char [] {' <',' < span class =code-string>>'},StringSplitOptions.RemoveEmptyEntries);
for ( int i = 0 ; i < (splitItems.Length); i + = 2 )
{
Words.Add(splitItems [i]);
Tags.Add(splitItems [i + 1 ]);
}
Hi,
I have an input string and want to extract several substrings from it. My input string has this format:
String inputStr="substr1<strlabel1> substr2<strlabel2> substr3<strlabel3> … "
so,the substr contains each character and punctuation marks such .!:?/}{][\)( and space
except < or > charachers
and strlabel contains any characher(w*)
For example :
string inputStr="this<zm> is<vbb> an<aa> simple example<jh> for<ppr> your<zm>"
the result substrings must be as follows:
string[] substrs={"this," is"," an"," simple example"," for"," your"};
string[] strlabels={" zm","vbb","aa","jh","ppr","zm""};
How to I can extract each substr and strlabel from inputstring?
解决方案
Use a regex:
[^<>]+(?=(\<.*?\>)|
)Should do it.
This can be very simple:List<string> Words = new List<string>(); List<string> Tags = new List<string>(); var splitItems = inputStr.Split(new char[] {'<', '>'}, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < (splitItems.Length); i+= 2) { Words.Add(splitItems[i]); Tags.Add(splitItems[i + 1]); }
这篇关于如何从字符串中提取子字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文