これは私が使用する簡単な拡張方法です。
public static string Between(this string src, string findfrom, string findto)
{
int start = src.IndexOf(findfrom);
int to = src.IndexOf(findto, start + findfrom.Length);
if (start < 0 || to < 0) return "";
string s = src.Substring(
start + findfrom.Length,
to - start - findfrom.Length);
return s;
}
これで使用できます
string valueToFind = sourceString.Between("car=", "</value>")
これを試すこともできます:
public static string Between(this string src, string findfrom,
params string[] findto)
{
int start = src.IndexOf(findfrom);
if (start < 0) return "";
foreach (string sto in findto)
{
int to = src.IndexOf(sto, start + findfrom.Length);
if (to >= 0) return
src.Substring(
start + findfrom.Length,
to - start - findfrom.Length);
}
return "";
}
これにより、複数の終了トークンを与えることができます (順序が重要です)。
string valueToFind = sourceString.Between("car=", ";", "</value>")