Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
1
Question by Tim_Kanbur · Oct 28, 2017 at 09:26 PM · arraystringforeachsplit

Change part of a string [Solved]

I want to change a part of a string like:

Hello (username), this is an example.

to

Hello exapleusername, this is an example.

I tried:

 sentence = xyz; 
         string[] array = xyz.Split(' ');
         foreach (string token in array)
         {
             if(Playername == token)
             {
                 //change this token
             }
         }


but i dont know how to change the token.
Can you help me?

Comment
Add comment · Show 1
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 MagicStyle · Oct 28, 2017 at 09:41 PM 0
Share

I don't understand your question. What exactly are you trying to do?

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

Answer by Hellium · Oct 29, 2017 at 08:19 AM

Why making things complicated ?

 string sentence = "Hello (username)" ;
 string output = sentence.Replace( "(username)", "Bob" ) ;
 // output = "Hello Bob" ;

However, if you don't know the value between your parenthesis, then use regular expressions :

 string sentence = "Hello (something)" ;
 System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex("\\(\\w+\\)") ;
 string output = regex.Replace( sentence, "Bob" ) ;
 // output = "Hello Bob" ;

Comment
Add comment · Show 1 · 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 Tim_Kanbur · Oct 29, 2017 at 06:07 PM 0
Share

Thank you for your good answer!

avatar image
0

Answer by avalagames · Oct 28, 2017 at 09:50 PM

So let me understand you correctly.

Lets say the player enters his/her username into an input field you want to display it as a string like following:
["Hello"] + ["username from inputfield"]

So I think it will look similar to something like this:

 public Inputfield usernameInputfield;
 public string storeUsername;
 public Text outputContainer;
     
 //So lets say you click on a button or something you willdo:
 storeUsername = usernameInputfield.text;
     
 //Your string ["storeUsername] will then be = to the text entered into the ["usernameInputfield"]
     
 //Lets say we want to test the result if we click on a button or something we will do:
 Debug.Log("Hello, " + storeUsername);
     
 //To get it in a string:
 string newString = "Hello, " + storeUsername;
     
 //You can then replace the text of ["outputContainer"] like the following:
 outputContainer.text = "Hello, " + storeUsername;
 OR
 outputContainer.text = newString;



I hope that this is what you are looking for.

Comment
Add comment · Show 1 · 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 Tim_Kanbur · Oct 28, 2017 at 10:40 PM 0
Share

First, thank you for the answer. I have probably expressed myself unclearly. I mean, i have a Queue with strings. in the next step I dequeue this different strings and then i want to change in the string, the username-placeholder with a string from another script. In the last step i want to match the changed placeholder and the rest of the string.

Example:

Hello (usernameplaceholder)....

next i only want to change the usernameplaceholder with another string (for example bob) but i want to keep the Hello in the sentence.

Hello Bob

Is that possible?

avatar image
0

Answer by NatCou · Jun 12, 2019 at 04:11 PM

Am just adding how I split my string here hopefully it helps somebody else The original string is "Drag @ to Square" replacing the @ with other text

 private void substituteCharInStringForNewWord(string myCommand)
             {
     
                 string[] splitArray = myCommand.Split(char.Parse("@"));
                 Debug.Log("Split array count = " + splitArray.Length  +"Command" + myCommand);
     
                 string tempO = splitArray[0];  ///Drag
                 Debug.Log("splitArray[0] " + tempO);
     
                 string tempTwo = splitArray[1]; /// to Square
                 Debug.Log("splitArray[1] " + tempTwo);
     
                 ///Drag colour to Square
                 reSubtitutedString = tempO + newColourmatchThisToLinkedDropTile + tempTwo;
                 Debug.Log("CombinedString" + reSubtitutedString);
     
             }


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

85 People are following this question.

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

Related Questions

how to get game object by string? 0 Answers

How do I step through every other occurance of a word in a string? 1 Answer

Check if specific object exist in list or array, Best Practices? 2 Answers

Splitting String only on a "space". Using my method --- FIXED 2 Answers

convert string to list of lists 1 Answer


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