Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by user-12590 (yahoo) · Jun 08, 2011 at 09:57 PM · stringchat

String.Replace() not working

As the title says, I am trying to use the String.Replace() function, but it is not working as the information I have found suggests it should. It is currently not editing the target string at all. This is my current code which contains that function.

 if(strTest.Contains("<name> ")){
     genString = "<name>";
     strTest.Replace(genString, "");
     print(genString);
     genString = strTest;
     strTest = "has changed their name to " + strTest;
     currentPlayer.name = genString;
 }
Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image lampartsygames · Aug 31, 2019 at 09:30 PM 0
Share

ins$$anonymous$$d of strTest.Replace(genString, ""); do this strTest = strTest.Replace(genString, "");

avatar image Bunny83 lampartsygames · Sep 01, 2019 at 02:01 AM 1
Share

That's what Chris said 8 years ago....

4 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by Chris D · Jun 08, 2011 at 10:05 PM

 genString = "";
 strTest.Replace(genString, "");

isn't this just saying "replace no character with no character"?

EDIT: as per @Eric5h5 's answer below, the Replace() function doesn't modify a string. So...

Presumably, you're trying to eliminate spaces from your string. If so, setting

 genString = " ";
 strTest = strTest.Replace(genString, ""); 


instead should do it.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Wolfram · Jun 08, 2011 at 10:50 PM 1
Share

Problem with Unity Answers, suppressing text enclosed in '' - he does have stuff in his string.

avatar image Chris D · Jun 08, 2011 at 10:53 PM 0
Share

oh. haha, welp ignore all this, then! Is the only way to see it by having edit privileges?

avatar image ina · Nov 16, 2011 at 02:45 AM 0
Share

< also works - that's & l t ; with no spaces in between

avatar image
3

Answer by Eric5h5 · Jun 08, 2011 at 10:10 PM

This code doesn't do anything:

 genString = "";
 strTest.Replace(genString, "");

Also, String.Replace returns a new string. It doesn't change strings in-place. http://msdn.microsoft.com/en-us/library/system.string.replace.aspx

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Chris D · Jun 08, 2011 at 10:14 PM 0
Share

So would

  genString = " ";
  strTest = strTest.Replace(genString, ""); 

work, then?

avatar image Eric5h5 · Jun 08, 2011 at 10:23 PM 0
Share

It should...give it a shot and see.

avatar image Wolfram · Jun 08, 2011 at 10:49 PM 1
Share

Click "edit" on his question - he actually has stuff in there, but the new Unity Answers is trying to parse tags starting with '

avatar image
1

Answer by Hassan-Kanso · Mar 01, 2015 at 07:56 AM

solution:

 if(strTest.Contains("<name> ")){
      genString = "<name>";

     strTest = strTest.Replace(genString, ""); // I changed this line


      print(genString);
      genString = strTest;
      strTest = "has changed their name to " + strTest;
      currentPlayer.name = genString;
  }

string.Replace(); get a new string but it doesn't save it in any string variable, you should save it!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by lampartsygames · Aug 31, 2019 at 09:28 PM

  if(strTest.Contains("<name> ")){
      genString = "<name>";
      strTest = strTest.Replace(genString, "");
      print(genString);
      genString = strTest;
      strTest = "has changed their name to " + strTest;
      currentPlayer.name = genString;
  }

what changed, why? here... myString.replace(old , new); This returns a value. The value needs to be stored. so if you want it to actually work use it like this: myString = myString.replace(old , new);

hope, this helps after 8 years of people surfing through here and not giving this answer.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Question regarding chat-input? 1 Answer

Textfield doesnt work; it doesnt apply string 0 Answers

How to put a list of strings in a Text Unity 4.6 UI element 3 Answers

List of string is duplicated [C#] 1 Answer

Strange string appending 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges