test if the string is a blank data string

M

Matt

If I just want to test if a string is blank data string or not. Do you
think this method is good enough?? Or any better approach. Please
advise. Thanks.

public boolean isBlankDataString(String s)
{ return s.trim().length() == 0;
}

boolean b = isBlankDataString(" ");
 
H

Hal Rosser

If I just want to test if a string is blank data string or not. Do you
think this method is good enough?? Or any better approach. Please
advise. Thanks.

public boolean isBlankDataString(String s)
{ return s.trim().length() == 0;
}

boolean b = isBlankDataString(" ");

Some Database fields give trouble - if the Field is 'Not required', but does
not allow zero-length.
In some cases, there will not be anything in that field - so it may be null.
I would check that possibility as well, because zero-length does not mean
null.
 
K

karlheinz klingbeil

Matt schrub am Donnerstag, 16. September 2004 01:39
folgendes:
If I just want to test if a string is blank data
string or not. Do you think this method is good
enough?? Or any better approach. Please advise.
Thanks.

public boolean isBlankDataString(String s){
return (s==null) ? true : s.trim().equals("");
}
 
B

bugbear

Hal said:
I would check that possibility as well, because zero-length does not mean
null.

Heh. In the SQL92 spec, you're quite right.
Oracle (sadly) believe that zero-length does mean
null.

Shit happens.

BugBear
 
M

Mario Maestro

karlheinz said:
Matt schrub am Donnerstag, 16. September 2004 01:39
folgendes:




public boolean isBlankDataString(String s){
return (s==null) ? true : s.trim().equals("");
}

If you have:

String s = " "; // some spaces
System.out.println(s.equals(s.trim()));

it prints "true", since strings that contain whitespaces only won't be
modified by trim(). I use the following code:

public static boolean isNullOrWhitespace(String s) {
if (s == null) {
return true;
}

for (int i = 0; i < s.length(); i++) {
if (!Character.isWhitespace(s.charAt(i))) {
return false;
}
}

return true;
}

Mario
 
C

Chris Smith

Mario said:
If you have:

String s = " "; // some spaces
System.out.println(s.equals(s.trim()));

it prints "true", since strings that contain whitespaces only won't be
modified by trim().

Eh? From the API specification:

Otherwise, if there is no character with a code greater than
'\u0020' in the string, then a new String object representing an
empty string is created and returned.

I haven't tried this. Does the reference implementation disagree with
the specification here?

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

Mario Maestro

Eh? From the API specification:
Otherwise, if there is no character with a code greater than
'\u0020' in the string, then a new String object representing an
empty string is created and returned.

I haven't tried this. Does the reference implementation disagree with
the specification here?

Sorry your're right! I wrote that method once when I had to deal with
Unicode space characters, that are not trimmed. My example 2-lines code
should effectively return false, so it depends on what the user needs.
If only ISO-LATIN-1 white spaces are relevant, then Karlheinz's code is
perfect. Otherwise, another method is needed.

Mario
 
J

Joona I Palaste

karlheinz klingbeil said:
Matt schrub am Donnerstag, 16. September 2004 01:39
folgendes:
public boolean isBlankDataString(String s){
return (s==null) ? true : s.trim().equals("");
}

The same can be accomplished with:

return s==null || s.trim().equals("");

There is very seldom any real need to use "true" or "false" in
conjuction with the ?: operator.

--
/-- Joona Palaste ([email protected]) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"A bee could, in effect, gather its junk. Llamas (no poor quadripeds) tune
and vow excitedly zooming."
- JIPsoft
 
T

Thomas Kellerer

Joona I Palaste wrote on 17.09.2004 22:13:
The same can be accomplished with:

return s==null || s.trim().equals("");

Wouldn't

return (s == null || s.trim().length() == 0)

be a bit quicker (and avoid the creation of the "" String)?

Thomas
 
T

Tor Iver Wilhelmsen

bugbear said:
Oracle (sadly) believe that zero-length does mean
null.

- Let us all thank Oracle for the need for ResultSet.wasNull().
- THANK YOU, ORACLE!

:)
 
J

Joona I Palaste

Thomas Kellerer said:
Joona I Palaste wrote on 17.09.2004 22:13:

return (s == null || s.trim().length() == 0)
be a bit quicker (and avoid the creation of the "" String)?

Perhaps. The point is, both versions avoid the ?: operator, which shows
that boolean constants are very rarely needed with the ?: operator.
BTW, you don't need the parantheses. "return" is a statement, not a
method call or an operator.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top