C# 从文本中删除单词,但仅删除包含所有不同字母的单词

C# 从文本中删除单词,但仅删除包含所有不同字母的单词,c#,C#,例如,应删除“苹果”,而不应删除“狗” 例如:“狗吃了我的苹果,所以我很伤心哇。” 结果是:“狗吃了我的肉,所以我很伤心。” 这不是最聪明的方法,但却有效: using System.Linq; string line = "Dog ate my apple,so i am sad wow."; char[] skyrikliai = { ' ', '.', ',', '!', '?', ':', ';', '(', ')', '\t' }; string[]

例如,应删除
“苹果”
,而不应删除
“狗”

例如:
“狗吃了我的苹果,所以我很伤心哇。”

结果是:
“狗吃了我的肉,所以我很伤心。”


这不是最聪明的方法,但却有效:

 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应该被保留,如果是这样的话:


输出将是:
狗吃了我的,所以我很难过。

我假设您要删除以一个命名特殊字符结尾的任何给定单词

因此,您可以使用正则表达式:(
使用System.Text.RegularExpressions;

string str=“狗吃了我的苹果,所以我很伤心哇。”;
var reg=新正则表达式(@“\w+[.|,|!|?|:| |(|)|\t]”);
变量匹配=规则匹配(str);
//使用“i”访问当前匹配项
//跟踪使用“j”删除的字符数
for(int i=0,j=0;i

输出将是:
狗吃了我的,所以我很难过。

我很困惑。你说“苹果”不会被删除,“狗”会被删除,但结果不包含“苹果”,而是包含狗。你应该澄清你的问题,并向我们展示你尝试过的内容。我认为这个问题的措辞可能更好:“我如何从包含该单词内重复字母的字符串中删除所有单词?”,但我不是100%确定。请尝试正则表达式,例如
string result=Regex.Replace(source,“[A-Za-z']+”,match=>match.Value.GroupBy(c=>char.toupper不变量(c)).Any(chunk=>chunk.Count()>1)?“”:match.Value)foreach(部分字符串zodis){var pirmas=zodis.ToCharArray();for(int i=0;ichar[] skyrikliai = { ' ', '.', ',', '!', '?', ':', ';', '(', ')', '\t' };

 string[] parts = line.Split(skyrikliai, StringSplitOptions.RemoveEmptyEntries);
   foreach (string word in parts)
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;
}