Replace text while keeping case intact in C#

后端 未结 2 1671
一个人的身影
一个人的身影 2021-02-15 02:08

I have a set of sentences i need to use to do a replace, for example:

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

And i have a text where to make the chan

2条回答
  •  無奈伤痛
    2021-02-15 02:30

    Not sure how well this will work, but this is what I came up with:

            string input = "A bgt abc hyi. Abc Ab df h";
            Dictionary map = new Dictionary();
            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
    

    EDIT After reading the modified question, here's my modified code (it turns out to be similar steps to @Sephallia's code.. and similar variable names lol )

    The code now is a bit more complicated.. but I think it's ok

            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 map = new Dictionary();
            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.
            */
    

提交回复
热议问题