Results 1 to 2 of 2

Thread: Why won't this string operation work?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    May 2002
    Posts
    1,602

    Why won't this string operation work?

    string tS = "JOB !12 JAN 2002 ! 12:22:12";

    tS.Replace("2","r");
    Console.Write(tS);

    the output is:
    JOB !12 JAN 2002 ! 12:22:12

    although the replace method should have replaced the 2 with r


    HOWEVER

    this one works
    Console.Write(tS.Replace("2","r"));

    Anyone has any suggestions???

    best regards
    Henrik

  2. #2
    PowerPoster Lethal's Avatar
    Join Date
    Oct 2000
    Location
    Ohio
    Posts
    2,496
    You need to assign the result of the replace() method to a new string. When working with string objects, a new string is created everytime you make a change to the string. If you are doing alot of string manipualtion, read up on the StringBuilder class.

    Code:
        string myNewString = tS.Replace("2","r");
        Console.Write(myNewString);

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width