替换部分句子 [英] Replacing part of sentence
本文介绍了替换部分句子的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一句话
我的名字是詹姆斯,我19岁。
如何更换和部分直到完全停止(。)并完全停止(。)
预期输出应该是这样的。
我的名字是詹姆斯。
提前谢谢
Suppose i have a sentence
my name is james and i am 19 years old.
how can i replace after "and" part til full stop(.) with full stop(.)
the expected output should be something like that.
my name is james.
thank you in advance
推荐答案
使用正则表达式:
Use a Regex:
public static Regex regex = new Regex(
@"\sand\s.+\.",
RegexOptions.IgnoreCase
| RegexOptions.Multiline
| RegexOptions.CultureInvariant
| RegexOptions.IgnorePatternWhitespace
| RegexOptions.Compiled
);
string result = regex.Replace(inputText,".");
您可以使用 Substring()来这样做
You can use Substring() to do that
string sentence = "my name is james and i am 19 years old.";
string result = sentence.Substring(0, sentence.IndexOf(" and "))+".";
希望,它有帮助:)
更新:
我刚刚意识到,最好使用正则表达式来处理这些要求,如@OriginalGriff所示。
Hope, it helps :)
Update:
I just realized, it's better to use regular expression to handle these kind of requirements as shown by @OriginalGriff.
这篇关于替换部分句子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文