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 AxisRob · Jan 19, 2014 at 06:03 PM · stringfor loop

Remove Numbers from a String

Hi. I need to remove some numbers from a string.

The string looks something like "XX points", where XX is a number. It can be one or two digits.

I tried things with character arrays and such, but I can't get anything to work. Here's the code I'm working on now (it says that the debug.log returns "array index is out of range" error messages).

 if (MainBallPoints != null)
             {
                 string ballvalue = string.Empty;
                 string currentvalue = MainBallPoints.value.ToString();
                 for (int i = 0; i < currentvalue.Length; i++)
                 {
                     if (((int)currentvalue[i] >= 0) && ((int)currentvalue[i] <= 9))
                     {
                         ballvalue += currentvalue[i];
                     }
                 }
                 Debug.Log(ballvalue[1]);
                 Utility.MainBallScore = int.Parse(ballvalue);
                 Debug.Log(Utility.MainBallScore);
             }

Any advice is very welcome. I also don't feel comfortable casting inside of a for loop, but I'm not sure of a way around that.

Thank you for your time.

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

2 Replies

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

Answer by YoungDeveloper · Jan 19, 2014 at 06:12 PM

There are probably ready functions for this. But one way of doing this could be comparing string to ascii table.

http://www.asciitable.com/

This will return a new string without numbers, and set number itself. I didn't test this, but should work.

 int num;

 string RemoveNumbers(string text){
     string newText = "";
     string number = "";

     for(int i = 0; i < text.Length; i++){
         if ((text[i] < 48) || (text[i] > 57)){ //is a char
             newText += text[i];
         }
         else{ //is number
             number += text[i];
         }
     }

     num = int.Parse(number);
     return newText;
 }
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 fafase · Jan 19, 2014 at 06:17 PM 0
Share

I was about to give something like this but then remembered that there should be a regex out there that does it with line. Also, this needs to be checked since I think Unity uses UTF-16 and not ascii (no ascii for sure) but I would think the first byte is just a bunch of 0 so the 48/57 thingy should do.

avatar image YoungDeveloper · Jan 19, 2014 at 06:19 PM 0
Share

Hmm, i have posted this type of solutions before and tested them in unity, worked like a charm.

avatar image
0

Answer by fafase · Jan 19, 2014 at 06:09 PM

http://stackoverflow.com/questions/4734116/c-sharp-find-and-extract-number-from-a-string

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

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

19 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

Related Questions

Find the length of a string in Javascript 1 Answer

How do you set a string while in game ? 2 Answers

Edit a string C# 1 Answer

Using Raycast for RTS game ? 2 Answers

Recovering function from input string 2 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