- Home /
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.
Don't quite understand the example, but are you looking for regular expression (RegEx) pattern matching?
I mean, if the player enters njkaf683kjsf72k, the fact that it has 3 k in it means to do something specific
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.
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"))
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.
Sounds like a job for Regex. Then you can count the matches of 'k'. Google up regex may help.
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.
And it you want to eli$$anonymous$$ate the caps, don't forget to use:
TestString.ToLower();
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.
Your answer
Follow this Question
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