Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Not sure how well this will work, but this is what I came up with:</p> <pre><code> string input = "A bgt abc hyi. Abc Ab df h"; Dictionary&lt;string, string&gt; map = new Dictionary&lt;string, string&gt;(); 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 =&gt; { 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 </code></pre> <p><strong>EDIT</strong> 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 )</p> <p>The code now is a bit more complicated.. but I think it's ok</p> <pre><code> 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&lt;string, string&gt; map = new Dictionary&lt;string, string&gt;(); 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 =&gt; { 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 =&gt; char.IsUpper(w[0]) || !char.IsLetter(w[0])); isAllUpper = sentenceArray.All(c =&gt; 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 =&gt; 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. */ </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload