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
0
Question by aWolfKing · Jun 01, 2016 at 01:41 PM · stringreplace

[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";

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 Bunny83 · Jun 01, 2016 at 02:22 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 4 · 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 aWolfKing · Jun 01, 2016 at 02:03 PM 0
Share

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.

avatar image Dave-Carlile · Jun 01, 2016 at 02:15 PM 0
Share

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"?

avatar image aWolfKing Dave-Carlile · Jun 01, 2016 at 02:21 PM 0
Share
 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.

avatar image Dave-Carlile Dave-Carlile · Jun 01, 2016 at 02:37 PM 0
Share

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 + "/".

avatar image
0

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);
Comment
Add comment · Show 4 · 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 aWolfKing · Jun 01, 2016 at 02:15 PM 0
Share

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"

avatar image DiegoSLTS · Jun 01, 2016 at 02:21 PM 0
Share

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.

avatar image aWolfKing DiegoSLTS · Jun 01, 2016 at 02:33 PM 0
Share

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"

avatar image aWolfKing DiegoSLTS · Jun 01, 2016 at 03:00 PM 0
Share

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?

avatar image
0

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.

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 aWolfKing · Jun 01, 2016 at 03:37 PM 0
Share

PS. if anyone is interested in my script just let me know, I whould be happy to share it with you now it works.

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

46 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

Related Questions

SOLVED - String replace % with " in C# 1 Answer

replacing a character in a String 1 Answer

How to edit string? How to add text to string? 1 Answer

Replace() with string and Regex not replacing numbers/integers 2 Answers

Replace the first string C# 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