我想修剪一个字符串并删除某个单词之后出现的所有单词。
例如- 如果字符串包含“非常”文本
string mySentence=" Today is very nice day! ";
if (mysentence.Contains(very))
{
//remove everything that starts with 'very' until rest of the line..
}
结果应该是:
今天是
首先,您使用所需的单词进行拆分
string[] splits = mysentence.Split("very");
由于您已经确定“very”在字符串中,这将为您提供两个字符串。您想要第一个(“非常”之前的拆分)。您需要从那个修剪多余的空间,以便:
string result = splits[0].Trim();
尝试这个
string mySentence = " Today is very nice day! ";
if (mySentence.Contains("very"))
{
mySentence = mySentence.Remove(mySentence.IndexOf("very")).Trim();
}