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 Negagames · Aug 16, 2013 at 01:57 AM · javascriptstringparsingexternal files

Parsing a float from a string with text

Is there a way to find the value of a float or integer from a string which includes both numbers and characters, for example: "Music Volume: 100%"

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

1 Reply

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

Answer by hoy_smallfry · Aug 16, 2013 at 02:56 AM

If you need to extract the number string out of any string, such as "12"out of "12abc", or in this case "100" out of "Music Volume: 100%", you can use the Regex.Match function, which is used to find the first sub-string that matches a regular expression (basically, a pattern) in strings.

 import System.Text.RegularExpressions; // for Regex

 //...

 var theNumberString = Regex.Match(theOriginalString, "[+-]?\d*\.?\d*").Value;

The "[+-]?\d*\.?\d*" is a Regex pattern for extracting a set of digits from where they start to where they stop. For more examples of Regex patterns, use this as a guide.

Once you have extracted a string that represents the number, you can then parse it out with the usual functions, such as Int32.Parse, System.Convert.ToInt32, etc.

Comment
Add comment · Show 6 · 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 hoy_smallfry · Aug 16, 2013 at 03:43 AM 2
Share

Here's a little breakdown of what that Regex pattern means:

  • [] is a range of symbols. With it, the pattern begins with any one symbol inside the brackets, in this case, + or -, to represent positive or negative numbers.

  • ? says that the expression before it is optional. That basically means the pattern doesn't have to start out with + or -, such as "10" ins$$anonymous$$d of "+10".

  • \d refers to any number symbol, 0 through 9. It starts out with a \` to diffrentaiate it from the character symbol d. - * says to look for zero or more of the last expression. This lets the pattern match continue if there are more number symbols after the first, and stops when nothing else of the first matches, such as "123" out "123a456" - .` refers to a dot symbol for decimal place, because . has different Regex meaning. It's optional because of the ? after it, so that a number without a decimal place, like "1234", is considered valid.

  • more numbers for after the decimal place, for when the decimal place is included in the match.

avatar image Negagames · Aug 16, 2013 at 04:08 PM 0
Share

Looks good!

avatar image Negagames · Aug 16, 2013 at 04:19 PM 0
Share

Hold on! Using the regex pattern returns an error: Unexpected characher 'd' I think it's because unity only recognizes the \ character for functions like \n or \t, is there a way to fix this?

avatar image hoy_smallfry · Aug 16, 2013 at 06:17 PM 1
Share

Ah yeah, this is my bad. I was testing this in C#, where you can use the @ symbol before the string to avoid having to type out escape characters.

Since @ doesn't have the same meaning in Unityscript, I took it out but forgot to put the escape characters back in. Use this expanded form ins$$anonymous$$d:

 "[\\+\\-]?\\d+\\.?\\d+"

I also replaced the * with + so that it looks for one or more ins$$anonymous$$d of zero or more, just for "correctness". It should work now. Sorry, I totally spaced out about it.

avatar image ShadoX · Aug 26, 2013 at 06:43 PM 2
Share

Additionally you can check if your pattern works at http://regexpal.com/

Show more comments

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

18 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

Related Questions

How to convert a string to an int? 1 Answer

How can i split a string[] ? 1 Answer

Iterating through multidimensional arrays 2 Answers

iOS Not Converting String to Integer 0 Answers

Character at index of string? Finding which character is at an index like in Javascript charAt 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