myString.Replace with case... Is there a better way ?

This is a discussion on myString.Replace with case... Is there a better way ? within the C# Programming forums, part of the General Programming Boards category; Hello all, i am working on little exercise in my book and it ask for me to make a cartoon ...

  1. #1
    Registered User Mobidoy's Avatar
    Join Date
    May 2007
    Posts
    18

    myString.Replace with case... Is there a better way ?

    Hello all, i am working on little exercise in my book and it ask for me to make a cartoon speech simulator.... well lets say that the program needs to replace all the r for a w.

    I didn't have any problem with it but, (there is always a but) if i type R instead of r, it wont replace it (normal... not the same case) so what i did is that i add a second line to go thru my string again and replace the R with a W:

    Code:
    string sentence
    
    Console.Write("Type a sentence");
    sentence = Console.ReadLine();
    Console.WriteLine(""); //This is only to add a blank line
    
    sentence.Replace("R", "W");
    Console.WriteLine( sentence.Replace("r", "w"));
    
    
    Console.Write("Press enter to quit");
    Console.ReadLine();
    Is there another way to work around the case without changing it ?

    Thanx
    If your code fail, blame Canada !

    If your code makes your computer crash, Blame me, i'm a french canadian !!

  2. #2
    and the Hat of Clumsiness GanglyLamb's Avatar
    Join Date
    Oct 2002
    Location
    between photons and phonons
    Posts
    1,109
    You can always write your own function to do this.

    Go trough each character in the string.
    Check each character for the character you wish to replace ( for both cases ).
    If found replace with proper case.
    Return the changed string.

    This way you only "loop" once over the whole string.

    Then if you are worried that maybe your function would be slower then calling the build in function twice, you can always do some testing and see which one is faster.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault!?!?!?
    By Viper187 in forum Windows Programming
    Replies: 57
    Last Post: 10-02-2008, 09:40 PM
  2. Format Precision & Scale
    By mattnewtoc in forum C Programming
    Replies: 1
    Last Post: 09-16-2008, 10:34 AM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. Creating pop up menus
    By Ti22 in forum C++ Programming
    Replies: 22
    Last Post: 01-18-2005, 08:27 PM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21