Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 SteelArrow21 · Jul 01, 2014 at 09:21 PM · string

Edit a string C#

SO lets say I have a string:

 public string Test = "OneTwo";

Now, I want to use a script to change it from "OneTwo" to "OneFour". I don't want to make a whole new string, I want to directly edit the string. Remove Two. Add Four. Thanks, if it's possible!

Comment
Add comment · Show 2
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 SteelArrow21 · Jul 01, 2014 at 09:38 PM 0
Share

Just to be clear, I also don't want to do;

 Test = "OneFour";

I want to do String.Format or something

avatar image CanisLupus · Jul 01, 2014 at 10:02 PM 2
Share

Unlike in the C language, where "strings" are simply arrays of chars, strings in C#("true" strings) are immutable, meaning that, once created, their contents will not change. See $$anonymous$$SDN for more on this.

If what you are really asking is how to build the second string from the first (invariably resulting in a new string object), christoph_r has your answer. If you want to build longer strings with multiple concatenations (each concatenation creates a new string, as seen in the $$anonymous$$SDN page), you can try using the StringBuilder class.

1 Reply

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

Answer by christoph_r · Jul 01, 2014 at 09:39 PM

Take a look at string.Substring In your example this would be:

 Test = Test.Substring(0,3);   // 0 and 3 specify from and to which character the substring ranges
 Test += "Two";

By the way: it's good practice to start variable names with a lower case letter.

Comment
Add comment · Show 2 · 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 SteelArrow21 · Jul 01, 2014 at 09:49 PM 0
Share

Yes, that is exactly what I was looking for. Just a side question, is it possible to test for certain characters? For example: i want to test if the third character of the string "Test" is an e. Thank you!

avatar image christoph_r · Jul 01, 2014 at 10:15 PM 0
Share
 bool CheckCharacter(int position, string testString, string testCharacter)
 {
    if(testString.Length >= position)
    {
       if(testString[position] == testCharacter)
       {
          return true;
       }
       else {
       return false;
       }
    }
    else {
    return false;
    }
 }

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

23 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

Related Questions

Creating a Token At function 0 Answers

Remove the last word from a string 2 Answers

How to count the number of strings in a string class? 2 Answers

Does Unity have a "Nice Name" method? 1 Answer

How do you deserialize string into Vector3 using XMLSerializer? 0 Answers


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