このindexOf()
関数は、指定された部分文字列が最初に出現したインデックスを返します。
(私のインデックスは少しずれているかもしれませんが)次のようなことをお勧めします:
var searchme = "this is a testxxx of my data yxxx and there are many of these xxx parts yxxx";
var startindex= searchme.indexOf("xxx");
var endindex = searchme.indexOf("yxxx") + 3; //added 3 to find the index of the last 'x' instead of the index of the 'y' character
var stringpiece = searchme.substring(startindex, endindex - startindex);
そして、あなたはそれを繰り返すことができますstartindex != -1
私が言ったように、インデックスはわずかにずれている可能性があり、どこかに +1 または -1 を追加する必要があるかもしれませんが、これでうまくいくでしょう (と思います)。
これは、単語の代わりに文字を数える小さなサンプル プログラムです。ただし、プロセッサ機能を変更する必要があるだけです。
var a = "this is a testxxx of my data yxxx and there are many of these xxx parts yxxx";
a = ProcessString(a, CountChars);
string CountChars(string a)
{
return a.Length.ToString();
}
string ProcessString(string a, Func<string, string> processor)
{
int idx_start, idx_end = -4;
while ((idx_start = a.IndexOf("xxx", idx_end + 4)) >= 0)
{
idx_end = a.IndexOf("yxxx", idx_start + 3);
if (idx_end < 0)
break;
var string_in_between = a.Substring(idx_start + 3, idx_end - idx_start - 3);
var newString = processor(string_in_between);
a = a.Substring(0, idx_start + 3) + newString + a.Substring(idx_end, a.Length - idx_end);
idx_end -= string_in_between.Length - newString.Length;
}
return a;
}