Jump to content
  • Advertisement
Sign in to follow this  
glhf

How to check string for certain characters?

This topic is 2346 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How do you check if a string only has letters in it and no special characters like *|,^ etc or spaces?

I imagine you must make some kinda tricky loop that checks every char in the string one at a time and a second tricky loop to check only for the certain acsi characters (think its called asci? where a number represents a char?) for each char in the string.

Please help me!

Share this post


Link to post
Share on other sites
Advertisement
the regular expression "^[A-Za-z0-9]*$" will match any string that only contains letters and numbers.

if you only want letters you can use "^[A-Za-z]*$"

most languages will have regexp functions in the standard library.

for C#


using System.Text.RegularExpressions;
....
....
....
Regex r = new Regex("^[A-Za-z0-9]*$");
Match m = r.Match("SomeStringYouWantToTest");
if (m.Success()) {
// we got a match
}

(This will match an empty string aswell, replace * with + if you want atleast 1 character to get a match)

if you use a different regexp you can get multiple matches and can use a while loop and m.NextMatch() to iterate over the matches. (^ in the regexp indicates the start of the string and $ the end so the one i used will try to match the full string and thus the match result will either be the string you tried to match or nothing at all, if you skip the $ and send in for example "hello65world" it would succeed and give you the result "hello" and skipping both ^ and $ should give you 2 matches, "hello" and "world"

Edit:

For C++ (11)


#include <regex>
.....
.....
.....
std::regex reg("^[A-Za-z0-9]*$");
if (std::regex_match("StringToTest",reg)) {
//only letters or numbers in string
}
Edited by SimonForsman

Share this post


Link to post
Share on other sites

the regular expression "^[A-Za-z0-9]*$" will match any string that only contains letters and numbers.

if you only want letters you can use "^[A-Za-z]*$"

most languages will have regexp functions in the standard library.

for C#


using System.Text.RegularExpressions;
....
....
....
Regex r = new Regex("^[A-Za-z0-9]*$");
Match m = r.Match("SomeStringYouWantToTest");
if (m.Success()) {
// we got a match
}

(This will match an empty string aswell, replace * with + if you want atleast 1 character to get a match)

if you use a different regexp you can get multiple matches and can use a while loop and m.NextMatch() to iterate over the matches. (^ in the regexp indicates the start of the string and $ the end so the one i used will try to match the full string and thus the match result will either be the string you tried to match or nothing at all, if you skip the $ and send in for example "hello65world" it would succeed and give you the result "hello" and skipping both ^ and $ should give you 2 matches, "hello" and "world"

Edit:

For C++ (11)


#include <regex>
.....
.....
.....
std::regex reg("^[A-Za-z0-9]*$");
if (std::regex_match("StringToTest",reg)) {
//only letters or numbers in string
}



Thanks a lot but game maker doesnt have any expression like that.. :(
I have to create it myself... :S

I think game maker is strange because it has made so much simple stuff but it doesnt have any text fields you can add...I even had to create my own textboxes where I draw the text and the the box for the text and listen for keyboard keys.

they only have get string but thats a popup message box then.

Share this post


Link to post
Share on other sites

For C++ (11)


#include <regex>
.....
.....
.....
std::regex reg("^[A-Za-z0-9]*$");
if (std::regex_match("StringToTest",reg)) {
//only letters or numbers in string
}


In C++, the [font=courier new,courier,monospace]std::regex_match()[/font] function is a whole-string match (ie. has implicit "^.*$" anchors).

It's also a pretty heavyweight approach to compile and run a regular expression (trust me). If you need to check that a string contains only alphanumeric characters, the fastest and simplest way would be something like this.

bool is_alnum = true; // assume the best
std::string s = "stringToTest";
for (std::string::iterator c = s.begin(); c != s.end(); ++c)
{
if (!std::isalnum(*c))
{
is_alnum = false;
}
}
if (is_alnum)
{
std::cout << "it's OK\n";
}
else
{
std::cout << "no go\n";
}

Share this post


Link to post
Share on other sites
Small nitpick, you should have break after
[source lang="cpp"]

[color=#000088]if[color=#000000] [color=#666600](![color=#000000]std[color=#666600]::[color=#000000]isalnum[color=#666600](*[color=#000000]c[color=#666600]))
[color=#666600]{
[color=#000000] is_alnum [color=#666600]=[color=#000000] [color=#000088]false[color=#666600];
[color=#ff0000] break;
[color=#666600]}

[/source]
since we know we have non alpha-numeric character, so no reason to keep going.

Share this post


Link to post
Share on other sites
Even though the OP is not using C++, here is a way that is much shorter and most probably at least as fast:

bool all_letters(std::string input) {
return input.end() ==
std::find_if(input.begin(), input.end(), [](char c) { return !std::isalpha(c);});
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!