例如"apple"
应删除而"dog"
不应。
例如:"Dog ate my apple,so i am sad wow."
结果将是:"Dog ate my ,so i am sad."
char[] skyrikliai = { ' ', '.', ',', '!', '?', ':', ';', '(', ')', '\t' };
string[] parts = line.Split(skyrikliai, StringSplitOptions.RemoveEmptyEntries);
foreach (string word in parts)
答案 0 :(得分:1)
不能是最聪明的方式,但有效:
using System.Linq;
string line = "Dog ate my apple,so i am sad wow.";
char[] skyrikliai = { ' ', '.', ',', '!', '?', ':', ';', '(', ')', '\t' };
string[] parts = line.Split(skyrikliai, StringSplitOptions.RemoveEmptyEntries);
foreach (string word in parts)
{
char[] letters = word.ToCharArray();
var DisintctLetters = letters.Distinct().ToArray();
if (letters.Length != DisintctLetters.Length)
{
line = line.Replace(word, "");
}
}
Console.WriteLine(line);
我认为您的帖子中有拼写错误,Apple
应该删除,并且应该保留Dog,如果是的话:
输出结果为:Dog ate my ,so i am sad .
答案 1 :(得分:0)
我假设您要删除任何以指定特殊字符之一结尾的给定单词。
因此,您可以使用正则表达式:(using System.Text.RegularExpressions;
)
string str = "Dog ate my apple, so I am sad wow.";
var reg = new Regex(@"\w+[.|,|!|?|:|;|(|)|\t]");
var matches = reg.Matches(str);
// access the current match with 'i'
// keep track of how many characters were removed with 'j'
for (int i = 0, j = 0; i < matches.Count; i++)
{
str = str.Remove(matches[i].Index - j - 1, matches[i].Length);
j += matches[i].Length;
}
输出为:Dog ate my, so I am sad.