- Home /
[solved] String: replace between start char and end char [Edited]
Lets say i have string 'a' which is "This is a string with /stringnamehere/ to replace", I want to replace "/stringnamehere/" with whatever string that string holds; Question: how whould I do this?
Notes:
'/' is used to show the script that the string should be replaced.
the problem is that stringnamehere is a variable and not a set string so i cant replace "/stringnamehere/" with "a string" becouse "/stringnamehere/" actually is "/"+stringnamehere+"/".
What I want to achieve:
string a = "This is a string with /b/ to replace";
string b = "nothing";
string c;
//replace code
print(c);
output: "This is a string with nothing to replace"
I hope I made myself clear and I hope someone could help me.
[Edit] there are 2 variable senteces, if one of them contains the name of the other between 2 "/"s then this will be replaced with what that variable holds;
string VariableString1 = "Nothing";
string VariableString2 = "VariableString1 holds : /VariableString1/ as value";
//output
Debug.Log(VariableString2) should be "VariableString1 holds : Nothing as value".
My main problem is that both strings are variables so i cannot simply change a set sentance with a set replacement like: string Replacement = "this"; string Change1ToThis = "Change 1 to this"; //replace 1 with this "Change this to this";
I hope I made myself clear
No, you didn't ^^. As you presented your problem you want to replace a random string between your two slashes with some replacement. Since you don't seem to care about what's between the two slashes it's not clear what your problem is exactly.
You should edit your question and add more details about how you actually choose your replacement and what's the point of that string between the slashes.
Answer by Dave-Carlile · Jun 01, 2016 at 01:51 PM
I would suggest using string.Format.
You put placeholders in the string and they get replaced with values you specify.
Debug.Log(string.Format("This is a string with {0} to replace", "nothing"));
The linked documentation shows more examples and additional things you can do.
Alternatively you can use string.Replace.
Thanks for your answer, but I still have a problem: In this code you specify what {0} should be replaced with, however I dont know what I whould replace it with because the string '{0}' influences what whould be "nothing"; For example:
string a = "This is a string with {0} to replace";
string replacement1 = "nothing";
string replacement2 = "much";
//if '{0}' = replacement1 then "This is a string with nothing to replace"
//if '{0}' = replacement2 then "This is a string with mush to replace"
I will look if there is a way to print or return {0} but whanted to ask this anyway. Thanks for your reply.
So the value you're trying to replace represents the name of a variable, and you want to replace the variable name contained in the string with the value of the variable?
So...
string myVariable = "bleah";
string myText = "Replace /myVariable/ with the value of myVariable";
You want the result to be "Replace bleah with the value of myVariable"?
Identity|StringHere
Test|Identity=/Identity/
^^This is my text file which is split and put in a dictionairy as: key value Identity = "StringHere" Test = "Identity = /Identity/" I want Debug.Log(aDictionairy[Test]) have "Identity = StringHere" as output.
Okay. You'll need to parse out the /Identity/ string and then use that as a key to look up the other value in the dictionary. To parse that out you'll need to scan the string to find "/", then grab everything up to the next "/". If you can have multiples of those then repeat.
You can use string.IndexOf to find the position of the "/" characters, string.Substring to extract out the string so you can use it for a key.
Once you've looked up the value then you can use string.Replace to replace "/Identity/" with the new value. You'll already have the "Identity" part in a string since you used it to look up the value so you'll do a replace on "/" + key + "/".
Answer by DiegoSLTS · Jun 01, 2016 at 01:53 PM
Can you change the code that generates the script? Because if you use "{0}" instead of "/b/" you can use the standard Format method: https://msdn.microsoft.com/es-es/library/system.string.format%28v=vs.110%29.aspx
If not, you should be able to use String.Replace: https://msdn.microsoft.com/en-us/library/fk49wtc1%28v=vs.110%29.aspx
I don't understand the problem with "stringnamehere", but following your code your missing code should be:
c = a.Replace("/b/",b);
Thanks for your reply; the problem is that "/b/" is not assighned as "/b/" but is a variable, the same as b. I will explain what i am trying to script: -I made a c# script that reads a txt file and puts the data in a Dictionairy. the dictionairy contains identity as key and a string as value.
Example dictionairy
key string
identity1 = string1
identity2 = string2
b = "nothing"
stringtoreplace = "replace this with /b/"
the script indexes the library/dictionairy and gets the string which key(identity) holds if there is / some text / in the string is replaces /b/ with the string b ("nothing") In other words: the string with identity stringtoreplace contains /b/, becouse b is a string identity in the dictionairy so /b/ will be replaced with the value the key 'b' holds "replace this with /b/" => "replace this with nothing"
From your other comment I think I understand what you actually want to do. You get a string that at some point has a placeholder text between "/"s. That placeholder text is the name of the variable you should use for the replacement.
So, if the string says: "This is a string with /someVariable/ to replace" you want to paste there the value of an already existent variable called "someVariable", and if it says "This is a string with /anotherVariable/ to replace" you want to use an existent "anotherVariable" variable.
One solution would be to use reflection to find a variable of that name: http://stackoverflow.com/questions/9801624/get-name-of-a-variable-or-parameter
Another would be to have a Dictionary with string key and value, the string key would be the things you might find between the "/"s and the string value would be the text to use when replacing.
No matter what method you use, you'll need the string.Replace method since clearly you can't format your strings properly to be used with string.Format.
Yes exactly, the string between the 2 "/"s is the name of a variable or key that holds a string. the string between the 2 "/"s will be replaced with the string that the variable holds. string ReferenceToThisString = "the string the variable holds"; string aVariableString = "This is a variable like 'ReferenceToThisString', which holds /ReferenceToThisString/ as value"
Hi thanks for your reply, since you understand what i am trying to achieve i think your link is what i need, however I cant get it work like it should; Do you know what i am doing wrong here?
Debug.Log("Replace /b/ as b".ToString().TrimStart('/').TrimEnd('/').Trim()); outputs "Replace /b/ as b", like TrimStart, -end and Trim did nothing, so i do not understand how i would get the string between the 2 "/"s as output. If you understand how I would do that, could you help me?
Answer by aWolfKing · Jun 01, 2016 at 03:07 PM
I kind of found out how it works too, but it is still not the way i was hoping to:
for (int i=0; i< "Replace /b/ as b".Split('/').Length; i++)
{
if(i/2 == (Mathf.Floor(i/2))) //if even number since 2 x "/" is needed
{
Debug.Log("Replace /b/ as b".Split('/')[i-1]);
}
}
outputs 'b' from '/b/' so i can work with this.
Everyone, thanks for helping and help is still appreciated if someone knows how to do this the way I wanted.
PS. if anyone is interested in my script just let me know, I whould be happy to share it with you now it works.