同时保持完好的情况下,在C#替换文本(Replace text while keeping case

2019-07-30 00:47发布

我有一组句子。我需要用做代替,例如:

abc => cde
ab df => de
...

我有一个文本在何处进行更改。 但是我没有办法知道上述文本的预案。 因此,举例来说,如果我有:

A bgt abc hyi. Abc Ab df h

我必须更换,并得到:

A bgt cde nyi. Cde De h

或者,正如接近越好,即保留的情况下

编辑:由于我看到得多混淆这个我会试图澄清一点:

我问的方式,以保持帽更换后,我不认为通过精心传递(不很好地解释什么看过那部限嗣继承),所以我会给使用真正的单词一个更现实的例子。

认为它像一个gossary,由他们sinonyms替换表达式可以这么说,如果我映射:

didn't achieve success => failled miserably

然后我得到作为输入setence:

As he didn't achieve success, he was fired

我会得到

As he failled miserably, he was fired

但如果没有被资本,所以才会failled,如果达到或成功是大写的,所以会很惨,如果任何有超过1个字母大写,所以将它的对手

我主要的可能性(那些我真的想考虑cosideration)

  • 只有第一个单词的第一个字母大写
  • 只有每个单词的第一个字母大写
  • 所有字母大写

如果我能处理这三个,这将是acceaptable已经我想 - 这是easyer的 - 当然更深入的解决办法是,如果availlable更好

有任何想法吗?

Answer 1:

不知道如何这会工作,但是这是我想出了:

        string input = "A bgt abc hyi. Abc Ab df h";
        Dictionary<string, string> map = new Dictionary<string, string>();
        map.Add("abc", "cde");
        map.Add("ab df", "de");

        string temp = input;
        foreach (var entry in map)
        {
            string key = entry.Key;
            string value = entry.Value;
            temp = Regex.Replace(temp, key, match =>
            {
                bool isUpper = char.IsUpper(match.Value[0]);

                char[] result = value.ToCharArray();
                result[0] = isUpper
                    ? char.ToUpper(result[0])
                    : char.ToLower(result[0]);
                return new string(result);
            }, RegexOptions.IgnoreCase);
        }
        label1.Text = temp; // output is A bgt cde hyi. Cde De h

编辑读取修改后的问题,这是我修改后的代码(它原来是类似的步骤@ Sephallia的代码..和类似的变量名大声笑)

现在,该代码是更复杂一点..但我认为这是确定

        string input = 
        @"As he didn't achieve success, he was fired.
        As he DIDN'T ACHIEVE SUCCESS, he was fired.
        As he Didn't Achieve Success, he was fired.
        As he Didn't achieve success, he was fired.";
        Dictionary<string, string> map = new Dictionary<string, string>();
        map.Add("didn't achieve success", "failed miserably");


        string temp = input;
        foreach (var entry in map)
        {
            string key = entry.Key;
            string value = entry.Value;
            temp = Regex.Replace(temp, key, match =>
            {
                bool isFirstUpper, isEachUpper, isAllUpper;

                string sentence = match.Value;
                char[] sentenceArray = sentence.ToCharArray();

                string[] words = sentence.Split(' ');

                isFirstUpper = char.IsUpper(sentenceArray[0]);

                isEachUpper = words.All(w => char.IsUpper(w[0]) || !char.IsLetter(w[0]));

                isAllUpper = sentenceArray.All(c => char.IsUpper(c) || !char.IsLetter(c));

                if (isAllUpper)
                    return value.ToUpper();

                if (isEachUpper)
                {
                    // capitalize first of each word... use regex again :P
                    string capitalized = Regex.Replace(value, @"\b\w", charMatch => charMatch.Value.ToUpper());
                    return capitalized;
                }


                char[] result = value.ToCharArray();
                result[0] = isFirstUpper
                    ? char.ToUpper(result[0])
                    : char.ToLower(result[0]);
                return new string(result);
            }, RegexOptions.IgnoreCase);
        }
        textBox1.Text = temp; 
        /* output is :
        As he failed miserably, he was fired.
        As he FAILED MISERABLY, he was fired.
        As he Failed Miserably, he was fired.
        As he Failed miserably, he was fired.
        */


Answer 2:

你可以使用String.IndexOf与StringComparison.CurrentCultureIgnoreCase指定找到匹配。 在这一点上,由字符替换的字符将努力做掉。 资本化可以通过与检查处理Char.IsUpper用于源字符,然后使用Char.ToUpper或Char.ToLower目的地适当。



Answer 3:

你通过String作为字符数组能循环和使用Char.IsUpper(炭参数)

  1. 实例化一个空字符串
  2. 设置一个循环来遍历字符
  3. 检查你需要的字符改变为不同的一种
    1. 是:检查字符是否是大写或小写字母,根据结果,提出相应的字母在新的字符串。
    2. 无:只要将字符到新字符串
  4. 将原来的字符串到新的字符串。

可能不是做事情的最有效或壮观的方式,但它很简单,它的作品。

在一个侧面说明:我不知道你是如何转换角色,但如果你是说,移人物下来字母(当你想将它们转换)恒定的量,假设你被3移所以一 - > d和e - > G或类似的东西,那么你就可以得到从字符的ASCII值,加3(如果你想将它转换),然后您可以通过ASCII值的字符。 如上所述这里 。 你必须做的检查,虽然以确保您从循环字母结束了。 (或开头,如果你左移)。

编辑#1:(要保持上面有)

代码真正的大块......对不起! 这是我能看到做你问什么是最好的方式。 希望有人会拿出一个更优雅的方式。 如果您需要澄清,请不要评论或任何东西!

    // (to be clear) This is Elias' (original) code modified.
    static void Main(string[] args)
    {
        string input = "As he DIDN'T ACHIEVE Success, he was fired";
        Dictionary<string, string> map = new Dictionary<string, string>();
        map.Add("didn't achieve success", "failed miserably");

        string temp = input;
        foreach (var entry in map)
        {
            string key = entry.Key;
            string value = entry.Value;
            temp = Regex.Replace(temp, key, match =>
            {
                string[] matchSplit = match.Value.Split(' ');
                string[] valueSplit = value.Split(' ');

                // Set the number of words to the lower one.
                // If they're the same, it doesn't matter.
                int numWords = (matchSplit.Length <= valueSplit.Length) 
                    ? matchSplit.Length
                    : valueSplit.Length;

                // only first letter of first word capitalized
                // only first letter of every word capitalized
                // all letters capitalized
                char[] result = value.ToCharArray(); ;
                for (int i = 0; i < numWords; i++)
                {
                    if (char.IsUpper(matchSplit[i][0]))
                    {
                        bool allIsUpper = true;
                        int c = 1;
                        while (allIsUpper && c < matchSplit[i].Length)
                        {
                            if (!char.IsUpper(matchSplit[i][c]) && char.IsLetter(matchSplit[i][c]))
                            {
                                allIsUpper = false;
                            }
                            c++;
                        }
                        // if all the letters of the current word are true, allIsUpper will be true.
                        int arrayPosition = ArrayPosition(i, valueSplit);
                        Console.WriteLine(arrayPosition);
                        if (allIsUpper)
                        {
                            for (int j = 0; j < valueSplit[i].Length; j++)
                            {
                                result[j + arrayPosition] = char.ToUpper(result[j + arrayPosition]);
                            }
                        }
                        else
                        {
                            // The first letter.
                            result[arrayPosition] = char.ToUpper(result[arrayPosition]);
                        }
                    }
                }

                return new string(result);
            }, RegexOptions.IgnoreCase);
        }
        Console.WriteLine(temp); 
    }

    public static int ArrayPosition(int i, string[] valueSplit)
    {
        if (i > 0)
        {
            return valueSplit[i-1].Length + 1 + ArrayPosition(i - 1, valueSplit);
        }
        else
        {
            return 0;
        }

        return 0;
    }


Answer 4:

在时间和使用替换一个字符

if(currentChar.ToString() == currentChar.ToUpper(currentChar).ToString())
{
   //replace with upper case variant 
}


Answer 5:

这差不多就是里德说。 唯一的问题是,我不知道,当你应该做的FindReplace字符串的长度不同。 所以,我选择了最小长度和使用...

static string ReplaceCaseInsensitive(string Text, string Find, string Replace)
{
    char[] NewText = Text.ToCharArray();
    int ReplaceLength = Math.Min(Find.Length, Replace.Length);

    int LastIndex = -1;
    while (true)
    {
        LastIndex = Text.IndexOf(Find, LastIndex + 1, StringComparison.CurrentCultureIgnoreCase);

        if (LastIndex == -1)
        {
            break;
        }
        else
        {
            for (int i = 0; i < ReplaceLength; i++)
            {
                if (char.IsUpper(Text[i + LastIndex])) 
                    NewText[i + LastIndex] = char.ToUpper(Replace[i]);
                else
                    NewText[i + LastIndex] = char.ToLower(Replace[i]);
            }
        }
    }

    return new string(NewText);
}


文章来源: Replace text while keeping case intact in C#