[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 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.

Can you change the code that generates the script? Because if you use “{0}” instead of “/b/” you can use the standard Format method: String.Format Método (System) | Microsoft Learn

If not, you should be able to use String.Replace: String.Replace Method (System) | Microsoft Learn

I don’t understand the problem with “stringnamehere”, but following your code your missing code should be:

c = a.Replace("/b/",b);

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.