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 jdog98 · Nov 18, 2014 at 07:54 PM · c#arraystringifcontains

if a string has certain letter(s) or number(s)

In my game, the player can enter a code to unlock certain things. I want they player to check to see if:

  • a) the code is one that can unlock something

  • b) if the code contains the letters to mean something

I don't want to say if(string == string) cause each code is unique and can only be use once. Any ideas on how to do that would be appreciated.

Comment
Add comment · Show 4
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 tanoshimi · Nov 18, 2014 at 07:56 PM 0
Share

Don't quite understand the example, but are you looking for regular expression (RegEx) pattern matching?

avatar image jdog98 · Nov 18, 2014 at 07:59 PM 0
Share

I mean, if the player enters njkaf683kjsf72k, the fact that it has 3 k in it means to do something specific

avatar image FairGamesProductions · Nov 18, 2014 at 08:08 PM 0
Share

For that see my answer below.

avatar image tanoshimi · Nov 18, 2014 at 08:41 PM 0
Share

If it's simply substring matching you're after, String.Contains will do the job. If it's more complex rules about word boundaries, capitalisation, allowed and disallowed characters, and patterns (I.e. "$$anonymous$$ust be at leat 3 k's followed by one or more characters and then a 3, but not at the end of the line"...) then use a RegEx.

2 Replies

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

Answer by FairGamesProductions · Nov 18, 2014 at 07:59 PM

You could use: string.contains("what to look for"). Example (js):

 private var TestString = "as3klkfjslgtke5tkt";
 
 function CheckCode ()
     {
     if (TestString.Contains("3k"))
         {
         Debug.Log("Code Correct");
         //do whatever you need to do for 3k
         }
     }


EDIT: Alternative:

 if (TestString.Contains("3") && (TestString.Contains("k"))
Comment
Add comment · Show 8 · 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 FairGamesProductions · Nov 18, 2014 at 08:37 PM 0
Share

Edited. See above.

avatar image jdog98 · Nov 18, 2014 at 08:41 PM 0
Share

That's kinda What I'm looking for, I want it to check to see if 3 and k are in the string, but not specifically together. For example jkl123. I would also like it to, if it's possible, check for how many k are in the string.

avatar image Rebaken-Enterprises · Nov 18, 2014 at 08:46 PM 0
Share

Sounds like a job for Regex. Then you can count the matches of 'k'. Google up regex may help.

avatar image FairGamesProductions · Nov 18, 2014 at 09:42 PM 0
Share

And, if you don't like/don't want to use regex, you can do this (js):

 private var TestString = "as3lkfjslgtke5tkt";
 private var $$anonymous$$s = 0;
 
 function CheckCode ()
     {
     $$anonymous$$s = 0;
     for (i in TestString)
         {
         if (i =='k')
             {
             $$anonymous$$s += 1;
             }
         }
     }

It's not pretty, and not efficient, but it'll do the job.

avatar image FairGamesProductions · Nov 18, 2014 at 09:44 PM 0
Share

And it you want to eli$$anonymous$$ate the caps, don't forget to use:

 TestString.ToLower();
Show more comments
avatar image
0

Answer by rob5300 · Nov 18, 2014 at 07:58 PM

You could try a system where you use String.split on the string and look for a sequence of numbers or letters at certain places? Info here: http://msdn.microsoft.com/en-us/library/system.string.split%28v=vs.110%29.aspx

Have a look at all the methods available and see whats best.

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

28 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

I am Trying to find what position a Space is in my Array and get them to a array of floats 2 Answers

C# String Array Has Missing or Incorrect Keycode Strings 2 Answers

C# Incrementing a String Array 1 Answer

Only assignment, call, increment, decrement, and new object expressions can be used as a statement 1 Answer

Converting a string to an int 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