将例如和夫人视为一个词c#
我有一个字符串,其中包含单词“e.g.”和“Mrs.”。我需要用句号作为分隔符分成两句话。但是,如果我使用句点字符,e和g在一个数组中被分成不同的索引,而不是一个包含两个句子的数组将例如和夫人视为一个词c#,c#,C#,我有一个字符串,其中包含单词“e.g.”和“Mrs.”。我需要用句号作为分隔符分成两句话。但是,如果我使用句点字符,e和g在一个数组中被分成不同的索引,而不是一个包含两个句子的数组 string wholeSentence = @"Each paragraph may have a number of sentences, depending on the topic. I can now write topics on sports e.g. basketball, football
string wholeSentence = @"Each paragraph may have a number of sentences, depending on the topic. I can now write topics on sports e.g. basketball, football, baseball and submit it to Mrs. Smith.";
string[] collection = wholeSentence.Split('.');
foreach(string sentence in collection)
{
Console.WriteLine(sentence);
}
Console.ReadLine();
输出
Each paragraph may have a number of sentences, depending on the topic
I can now write topics on sports e
g
basketball, football, baseball and submit it to Mrs
Smith
我可以知道如何更正吗?当您想检查
例如和夫人时,您可以简单地将例如临时替换为其他内容,例如e*g*
和Mrs.
替换为Mrs*
。然后,一旦拆分发生,您可以检查句子是否包含替换字符,然后您可以再次将它们替换回以前的术语。如下所示:
string wholeSentence = @"Each paragraph may have a number of sentences, depending on the topic. I can now write topics on sports e.g. basketball, football, baseball and submit it to Mrs. Smith.";
string[] collection = wholeSentence.Replace("e.g.", "e*g*").Replace("Mrs.", "Mrs*").Split('.');
foreach (string sentence in collection)
{
if (sentence.Contains("e*g*") || sentence.Contains("Mrs*"))
{
Console.WriteLine(sentence.Replace("*", "."));
continue;
}
Console.WriteLine(sentence);
}
您已在解决方案中硬编码替换eg和替换Mrs。对于其他前缀,如Mr和自然语言,如AM或P.M.,它怎么可能是动态的?问题是,它不会。正是为了这句话中的那两个词。你认为用户会如何检测这些单词,因为一个句子中可能有许多单词带有缩写,你无法列出。而且你不能说AM PM的自然语言,我们都知道,这些缩写分别是ante merīdiem
和post meridiem
的缩写,英语中有数千个甚至数百万个缩写。