helloyesyes 发表于 2013-2-1 09:51:40

C# 找出一个子串在另一字符串中出现的所有位置

如果需要在一个字符串中找出一个特定串所有出现的位置,可以采用下面代码:
<div style="padding-right: 5.4pt; padding-left: 5.4pt; background: #e6e6e6; padding-bottom: 4px; width: 95%; padding-top: 4px;">http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gifpublicint[]GetSubStrCountInStr(Stringstr,Stringsubstr,intStartPos)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifintfoundPos=-1;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifintcount=0;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifList<int>foundItems=newList<int>();
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifdo
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.giffoundPos=str.IndexOf(substr,StartPos);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifif(foundPos>-1)
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gifhttp://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif...{
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifStartPos=foundPos+1;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifcount++;
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.giffoundItems.Add(foundPos);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif}while(foundPos>-1&&StartPos<str.Length);
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gifreturn((int[])foundItems.ToArray());
http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif}
页: [1]
查看完整版本: C# 找出一个子串在另一字符串中出现的所有位置