Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

String wont replace after using Replace

Discussion in 'Scripting' started by light766, Apr 24, 2014.

  1. light766

    light766

    Joined:
    Apr 23, 2009
    Posts:
    250
    been debugging this for quite some time and still cant find a solution, the text is adding the ";"s and removes them in the end, the problem here is that the Replace is not replacing, so at the end the text remains the same. :confused:

    Code (csharp):
    1. public void SomeoneLeft(string user){
    2.    
    3.         if(photonView.isMine){
    4.             print (user);
    5.  
    6.             teamOneText += ";";
    7.             teamOneText = ";" + teamOneText;
    8.             teamOneText.Replace(";" + user + ";",";;");
    9.             teamOneText = teamOneText.Substring(0, teamOneText.Length - 1);
    10.             teamOneText = teamOneText.Substring(1);
    11.  
    12.             teamTwoText += ";";
    13.             teamTwoText = ";" + teamTwoText;
    14.             teamTwoText.Replace(";" + user + ";",";;");
    15.             teamTwoText = teamTwoText.Substring(0, teamTwoText.Length - 1);
    16.             teamTwoText = teamTwoText.Substring(1);
    17.         }
    18.  
    19.     }
     
  2. devandart

    devandart

    Joined:
    Jun 7, 2013
    Posts:
    144
    The Replace() method returns the replaced string like Substring reuturns a substring, so you have to write
    Code (csharp):
    1.  
    2. teamOneText = teamOneText.Replace(";" + user + ";",";;");
    3.  
    cheers
     
  3. light766

    light766

    Joined:
    Apr 23, 2009
    Posts:
    250
    yup, I just found that out, haha i feel so stupid, but thanks man!