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 /
  • Help Room /
avatar image
1
Question by afshin_a_1 · Jan 31, 2017 at 02:26 AM · c#scripting problemstringstrings

checking numbers in strings

Hi. first of all i'm beginner.
i'm using c# and i need help about this problem. i need to check if the first part of an string Variable is equel to for example "Hello" and Its continuation are numbers(if it continued by numbers. any number) do something. now i know for the first part we have stringVar.StartsWith() , but what about the numbers part? do we have any symbol in strings like #?&$ for representing numbers or any methods? help me please what code should i use?
imagination:

 if(stringVar.StartsWith("Hello") && numbers check part)
 {
   do something
 }
 else
 {
   do something else
 }

think our string variable is "Hello754".
note: the optimized Performance for this part of my game is very important and don't tell me about going through too many loops and arrays or...
I welcome and appreciate any help :)

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 afshin_a_1 · Jan 31, 2017 at 05:10 AM

what about this? :) since my question was under moderator review too long, i had time to search in string methods and write this code. this is working perfectly to me:

 int result;
 if(stringVar.StartsWith("Hello") && int.TryParse(stringVar.Substring(5), out result))
 {
      Debug.Log("i'm Hello with my numbers friends");
 }

hope it helps anyone else.

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 afshin_a_1 · Jan 31, 2017 at 05:14 AM 1
Share

thank you very much @ErikHallmarkDev . but i think i find this better as i don't know what are Regular expressions. what's your idea about it?

avatar image ErikHallmarkDev afshin_a_1 · Jan 31, 2017 at 06:17 PM 0
Share

That solution should work very well. But if I remember correctly both StartsWith and TryParse are running regex in the background. So by using your own expression you could cut down on the number of function calls. But that would be a almost unnoticeable performance enhancement, so your solution is fine as is.

avatar image
1

Answer by ErikHallmarkDev · Jan 31, 2017 at 03:40 AM

If I understand what you need, you should be able to achieve this with a Regular expression.

First you'll need to include the System.Text.RegularExpressions namespace.

Then you should be able to run this check on your string

 if(Regex.IsMatch("Hello754",  @"^Hello[0-9]+") //This will return true if the string is the word "Hello" followed by number characters

If you'd like to learn more about Regex so you can create a system that maybe works more the way you want it to, I'd suggest checking out Daniel Shiffmans series on it.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is it possible to have 2 references in an array? 1 Answer

Accesing specific int from inspector 2 Answers

Checking if a list has an object - Error 1 Answer

Storing input to array 1 Answer

Attack delay not working correctly 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