Jump to content

  • Log In with Google      Sign In   
  • Create Account

How to check string for certain characters?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 glhf   Banned   -  Reputation: -585

Like
0Likes
Like

Posted 11 July 2012 - 08:05 AM

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!

Sponsor:

#2 SimonForsman   Crossbones+   -  Reputation: 6106

Like
0Likes
Like

Posted 11 July 2012 - 08:22 AM

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, 11 July 2012 - 08:45 AM.

I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

#3 glhf   Banned   -  Reputation: -585

Like
0Likes
Like

Posted 11 July 2012 - 09:49 AM

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.

#4 Bregma   Crossbones+   -  Reputation: 5133

Like
0Likes
Like

Posted 11 July 2012 - 09:54 AM

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 std::regex_match() 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";
  }

Stephen M. Webb
Professional Free Software Developer

#5 DoctorGlow   Members   -  Reputation: 801

Like
0Likes
Like

Posted 11 July 2012 - 12:26 PM

Small nitpick, you should have break after
[source lang="cpp"][color=#000088]if[/color][color=#000000] [/color][color=#666600](![/color][color=#000000]std[/color][color=#666600]::[/color][color=#000000]isalnum[/color][color=#666600](*[/color][color=#000000]c[/color][color=#666600]))[/color][color=#666600]{[/color][color=#000000] is_alnum [/color][color=#666600]=[/color][color=#000000] [/color][color=#000088]false[/color][color=#666600];[/color][b][color=#ff0000] break;[/color][/b][color=#666600]}[/color][/source]
since we know we have non alpha-numeric character, so no reason to keep going.

#6 rnlf   Members   -  Reputation: 1119

Like
1Likes
Like

Posted 11 July 2012 - 01:53 PM

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);});
}

my blog (German)





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS