• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
glhf

How to check string for certain characters?

5 posts in this topic

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!
0

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#

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

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

Share this post


Link to post
Share on other sites
[quote name='SimonForsman' timestamp='1342016577' post='4958034']
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#

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

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

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.
0

Share this post


Link to post
Share on other sites
[quote name='SimonForsman' timestamp='1342016577' post='4958034']
For C++ (11)

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

Share this post


Link to post
Share on other sites
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.
0

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:
[CODE]
bool all_letters(std::string input) {
return input.end() ==
std::find_if(input.begin(), input.end(), [](char c) { return !std::isalpha(c);});
}
[/CODE]
1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0