使用C#查找字符串中的文本

如何在字符串中找到给定的文本?之后,我想在此与其他之间创建一个新字符串。例如,如果字符串是:

This is an example string and my data is here

我想创建一个字符串,其中“ my”和“ is”之间应该是什么?这是很伪的,但希望它是有道理的。

回答:

使用此方法:

public static string getBetween(string strSource, string strStart, string strEnd)

{

if (strSource.Contains(strStart) && strSource.Contains(strEnd))

{

int Start, End;

Start = strSource.IndexOf(strStart, 0) + strStart.Length;

End = strSource.IndexOf(strEnd, Start);

return strSource.Substring(Start, End - Start);

}

return "";

}

如何使用它:

string source = "This is an example string and my data is here";

string data = getBetween(source, "my", "is");

以上是 使用C#查找字符串中的文本 的全部内容, 来源链接: utcz.com/qa/423448.html

回到顶部