Check if wxString Contains Alphabetic Characters Only Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
User avatar
Deluge
Earned some good credits
Earned some good credits
Posts: 122
Joined: Fri Apr 30, 2010 4:52 am
Location: USA
Contact:

Check if wxString Contains Alphabetic Characters Only

Post by Deluge »

My understanding is that wxString::IsWord() is deprecated, & that I should use the wxRegEx class or the wxString::Matches method. I am not good with regular expressions, but all I want to do is check if a wxString (or wxChar) contains alphabetic characters only.

According to the docs, I should be able to use the "bracket expression" alpha. I thought it would be done like wxString::Matches("[:alpha:]"), but apparently I am mistaken.

Here is the code I am testing with:

Code: Select all

#include <iostream>
#include <wx/string.h>

using namespace std;


bool isAlphaOnly(wxChar c) {
	if (!wxString(c).Matches("[:alpha:]")) return false;
	return true;
}

bool isAlphaOnly(wxString s) {
	cout << "Checking string \"" << s.c_str() << "\":\t";

	for (wxChar c : s) {
		if (!isAlphaOnly(c)) {
			return false;
		}
	}

	return true;
}

string toString(bool b) {
	if (b) return "true";
	else return "false";
}


int main(int argc, char** argv) {
	wxString s1 = "HellowxWidets";
	wxString s2 = "Hello wxWidgets";
	wxString s3 = "HellowxWidgets!";
	wxString s4 = "Hello8wxWidgets";

	cout << "\nAlpha only:\n";
	cout << toString(isAlphaOnly(s1)) << " (should be 'true')\n";
	cout << toString(isAlphaOnly(s2)) << " (should be 'false')\n";
	cout << toString(isAlphaOnly(s3)) << " (should be 'false')\n";
	cout << toString(isAlphaOnly(s4)) << " (should be 'false')\n";

	return 0;
}
If someone could point out my error I would greatly appreciate it. Meanwhile, I will go back to reading the docs.

--- Edit ---

Or maybe bracket expressions only work with the wxRegEx class?
Projects:
Debreate
MyABCs
Stendhal

OSes:
Windows 10 Home (missing my Linux & Freebsd :()
User avatar
Deluge
Earned some good credits
Earned some good credits
Posts: 122
Joined: Fri Apr 30, 2010 4:52 am
Location: USA
Contact:

Re: Check if wxString Contains Alphabetic Characters Only

Post by Deluge »

Found an answer. I can use the expression "^[A-Za-z]+$" with the wxRegEx class (doesn't seem to work with wxString::Matches(_T("^[A-Za-z]+$"))):

Code: Select all

...

const static wxRegEx alpha(_T("^[A-Za-z]+$"));

...

bool isAlphaOnly(wxString s) {
	cout << "Checking string \"" << s.c_str() << "\":\t";

	return alpha.Matches(s);
}
Projects:
Debreate
MyABCs
Stendhal

OSes:
Windows 10 Home (missing my Linux & Freebsd :()
alys666
Super wx Problem Solver
Super wx Problem Solver
Posts: 329
Joined: Tue Oct 18, 2016 2:31 pm

Re: Check if wxString Contains Alphabetic Characters Only

Post by alys666 »

if you need speed, just scan the string with custom function isAlphaChar(..)
regular expressions are slow

Code: Select all

bool isAlphaChar(char fch){
	return (fch >='a'  && fch <='z') || (fch>='A' && fch<='Z');
}

bool isAlphaString(const wxString &fs){
	for(int i=0; i<fs.Length(); ++i){
		if(!isAlphaChar(fs[i])) return false;
	}
	return true;
}
ubuntu 20.04, wxWidgets 3.2.1
User avatar
doublemax
Moderator
Moderator
Posts: 19163
Joined: Fri Apr 21, 2006 8:03 pm
Location: $FCE2

Re: Check if wxString Contains Alphabetic Characters Only

Post by doublemax »

Beware that all solutions in this thread won't work with Unicode strings. E.g. if the string contains an "ä", the check would be false.
Use the source, Luke!
User avatar
evstevemd
Part Of The Furniture
Part Of The Furniture
Posts: 2409
Joined: Wed Jan 28, 2009 11:57 am
Location: United Republic of Tanzania

Re: Check if wxString Contains Alphabetic Characters Only

Post by evstevemd »

If reasons for deprecating IsWord are not affecting your program you can just copy IsWord function from wxWidgets sources as temporary solution.
I don't know why it was deprecated and have no better suggestion!
Chief Justice: We have trouble dear citizens!
Citizens: What it is his honor?
Chief Justice:Our president is an atheist, who will he swear to?
User avatar
Deluge
Earned some good credits
Earned some good credits
Posts: 122
Joined: Fri Apr 30, 2010 4:52 am
Location: USA
Contact:

Re: Check if wxString Contains Alphabetic Characters Only

Post by Deluge »

alys666 wrote: Sun Jun 30, 2019 2:20 pm

Code: Select all

bool isAlphaChar(char fch){
	return (fch >='a'  && fch <='z') || (fch>='A' && fch<='Z');
}

bool isAlphaString(const wxString &fs){
	for(int i=0; i<fs.Length(); ++i){
		if(!isAlphaChar(fs[i])) return false;
	}
	return true;
}
Interesting solution. Thank you.
doublemax wrote: Sun Jun 30, 2019 3:43 pm Beware that all solutions in this thread won't work with Unicode strings. E.g. if the string contains an "ä", the check would be false.
Thank you for the warning doublemax. My program only deals with the basic English alphabet, so I didn't take other Latin symbols into consideration. Wouldn't be hard to make it Unicode compatible with alys666's solution though. Would just need to know the integer values of the characters & extend the formula.
Projects:
Debreate
MyABCs
Stendhal

OSes:
Windows 10 Home (missing my Linux & Freebsd :()
alys666
Super wx Problem Solver
Super wx Problem Solver
Posts: 329
Joined: Tue Oct 18, 2016 2:31 pm

Re: Check if wxString Contains Alphabetic Characters Only

Post by alys666 »

Deluge wrote: Mon Jul 01, 2019 10:18 pm Interesting solution. Thank you.
it's how compilers use to check for alpha string, or identifiers(with added conditions for numbers and '_') .
it will work everywhere, when comparison operation for char type is legal.
unicode is just 16 bit numeric type, and latin alphabet has there the same codes as for ascii. so - no problem
ubuntu 20.04, wxWidgets 3.2.1
Post Reply