Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could loop through the String as an array of characters and use the <a href="http://msdn.microsoft.com/en-us/library/system.char.isupper%28v=vs.71%29.aspx" rel="nofollow">Char.IsUpper(char parameter)</a></p> <ol> <li>Instantiate a blank string</li> <li>Set up a loop to loop through the characters</li> <li>Check if you need to change the character to a different one <ol> <li>Yes: Check whether or not the character is upper or lower case, depending on the result, put the appropriate letter in the new string.</li> <li>No: Just throw that character into the new string</li> </ol></li> <li>Set the original string to the new string.</li> </ol> <p>Might not be the most efficient or spectacular way of doing things, but it is simple, and it works.</p> <p>On a side note: I am not sure how you are converting the characters, but if you are say, shifting the characters down the alphabet (when you DO want to convert them) by a constant amount, let's say you're shifting by 3. So a -> d and E -> G or something like that, then you could get the ASCII value from the character, add 3 (if you want to convert it) and then get the character from the ASCII value. As described <a href="http://forums.devx.com/showpost.php?s=57bc9183228905df688a7b4a8037589c&amp;p=64365&amp;postcount=4" rel="nofollow">here</a>. You would have to do checks though to make sure that you loop back from the end of the alphabet. (or the beginning, if you're shifting left).</p> <p>Edit #1: (Going to keep the above there)</p> <p>Really big block of code... Sorry! This was the best way I could see to do what you were asking. Hopefully someone might come up with a more elegant way. Please do comment or anything if you require clarification!</p> <pre><code> // (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&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; { 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 &lt;= 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 &lt; numWords; i++) { if (char.IsUpper(matchSplit[i][0])) { bool allIsUpper = true; int c = 1; while (allIsUpper &amp;&amp; c &lt; matchSplit[i].Length) { if (!char.IsUpper(matchSplit[i][c]) &amp;&amp; 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 &lt; 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 &gt; 0) { return valueSplit[i-1].Length + 1 + ArrayPosition(i - 1, valueSplit); } else { return 0; } return 0; } </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. 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.
 

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