Character.isWhitespace alternative? (whitespace check)

499 views
Skip to first unread message

Ed

unread,
Sep 28, 2009, 3:39:35 PM9/28/09
to Google Web Toolkit
What is the correct way to check for a white space character?

This is done by the method Character.isWhitespace(char) but not
supported by GWT. What is the alternative?

I know check for whiteSpaces through
someString.charAt(index) == ' ';

But I am not sure if that's good enough as I think I forget things
like \t....

Dominik Steiner

unread,
Sep 28, 2009, 10:18:54 PM9/28/09
to Google Web Toolkit
did you try to use the regex method

boolean matches = someString.matches("\\S");

HTH

Dominik

Ed

unread,
Sep 29, 2009, 3:11:51 AM9/29/09
to Google Web Toolkit
Yes I can, but don't want to, as it's way to heave for just checking
if a character is a white space... Especialy in my case where
performance is an issue.

Dominik Steiner

unread,
Sep 29, 2009, 3:41:00 PM9/29/09
to google-we...@googlegroups.com
Hi Ed,

for performance reason I would definitely recommend you to actually
use regex instead of equals or equalsIgnoreCase methods of the String
class. Especially IE has really performance problems with former ones.
Concerning heavy, I would actually say that it is much cleaner to use
regex and more adaptable in case you would need in the future

HTH

Dominik

Ed

unread,
Sep 30, 2009, 4:32:36 AM9/30/09
to Google Web Toolkit
Hmmmm Interesting Dominik.
Thanks for the information.
It's sure is clean, but didn't know that performance of regexp is
"good" in this case..

What I want is: if a character at index X is a white space. That means
I always have to take first a substring of one character and then do a
match. So besides checking for a whitespace, I also take a substring,
does that still perform well ??...
Besides that: I always understood that object creation should be
reduces as much as possible. In this case it always creates more
objects...

Might it be a good idea to store the pattern in a static field, to
overcome unnecessary pattern compilation?
So do something like this:

private final static Pattern PATTERN_WHITE_SPACE = Pattern.compile("\
\S");
..
...
..
In a helper method:

public static boolean matchWhiteSpace(String input) {
return PATTERN_WHITE_SPACE.matcher(input).matches();
}


mars1412

unread,
Sep 30, 2009, 9:41:28 AM9/30/09
to Google Web Toolkit
if performance is really that critical you should test several
alternatives and check which one is really the best for your case (and
don't forget to test cross-browser; especially IE)
Reply all
Reply to author
Forward
0 new messages