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
6
Question by $$anonymous$$ · Oct 25, 2012 at 11:15 AM · c#characterstringlength

How to delete the last character of a string

I've a num pad system in my game where you just click buttons that will return a number in string. Each number you press it adds to a result string that is used for a code.

 public void ApplyNumber (string codeNumber) {
         
     resultCode += codeNumber;
     curChars++;
         
     if(curChars >= maxChars) {
         Reset ();
     }
         
 }

This is the function that update the string. Now I'm trying to make another button that have to delete the last char of the string, but I'm clueless on how to do that. Any idea?

Would prefer examplesin c#. Thanks in advance.

Comment
Add comment
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

3 Replies

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

Answer by Dave-Carlile · Oct 25, 2012 at 12:38 PM

   string value = "abcde";
   value = value.Substring(0, value.Length - 1);


The Substring function lets you grab part of a string. The first parameter is the starting position, the second is the length you want. By passing the current string length - 1, you're getting everything but the last character.

Comment
Add comment · Show 3 · 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 $$anonymous$$ · Oct 25, 2012 at 02:03 PM 1
Share

I find this solution the most viable, but still i get an error when going in negative, so if I keep deleting the string when reaching < 0 I get an error. I guess I need to put a failsafe in the code to set back to 0 the string when reaching < 0.

avatar image $$anonymous$$ · Oct 25, 2012 at 02:10 PM 0
Share

Ok solved this, thanks to everyone for the help.

avatar image EpicGeeth · Jul 26, 2015 at 01:36 PM 0
Share

This works.

avatar image
15

Answer by markpdolby · Oct 25, 2012 at 12:48 PM

 string s = "test";
 s = s.Remove(s.length - 1);

Using something like this should give you the string but with one less character, so in this example s would end up as "tes"

Hope that helps

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 Fattie · Apr 26, 2013 at 06:11 PM 0
Share

a beautiful answer

avatar image
3

Answer by Unity_gamer · Oct 25, 2012 at 12:20 PM

Hi Neurological ,

Try Splitting the String Using "Stringname.split" and Save that Value .

 // Say the String Is "Unity,3d"
 //If you want to split the String with ","
 String[] parts  = codeNumber.Split(","[0]);
 codeNumber = parts[0];  // Here in codeNumber you will get "Unity"

Thanks

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 Tsurugi21 · Feb 23, 2015 at 04:40 AM 0
Share

It also works, although if you know exacly where you want to trim, use Substring, as Split and For() loops requires a lot of processing and will slow down the game if called too quickly...

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

11 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

Related Questions

Distribute terrain in zones 3 Answers

How to prevent sql injection on the Unity Side 1 Answer

Multiple Cars not working 1 Answer

Run speed in world-based character controller 0 Answers

3D: how to avoid jerks in a character movement using accelerometer 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