- Home /
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?
I don't understand your question. What exactly are you trying to do?
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" ;
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.
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?
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);
}
Your answer
Follow this Question
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