to avoid null pointer exceptions.

If("".equals(test)) {

}


On 24 Jun 2004, at 19:39, Tom K wrote:

Every once in a while you see this question pop-up and the same thing
happens...you get a "whole" bunch of answers. Remember that a String is
an Object and not a primitive e.g. int, long etc. So in that regard if
it is not an int but an Integer what would you use to test for an empty
String. Think about it.
Tom Kochanowicz


-----Original Message----- From: Frank Zammetti [mailto:[EMAIL PROTECTED] Sent: Thursday, June 24, 2004 12:27 PM To: [EMAIL PROTECTED] Subject: Re: how to check if a String is empty?

Depends I guess :)  I personally find the use of a "magic number" to be
more
complex.  To my eyes, it's clearer to see an empty string.  The
intention is
more clear.

Kind of a silly debate I suppose because neither is exactly rocket
science
;)


From: "Robert F. Hall" <[EMAIL PROTECTED]>
Reply-To: "Tomcat Users List" <[EMAIL PROTECTED]>
To: Tomcat Users List <[EMAIL PROTECTED]>
Subject: Re: how to check if a String is empty?
Date: Thu, 24 Jun 2004 10:22:06 -0700

Howdy,

if (test == null || test.trim().length() == 0 ) { }  is simpler.

/Robert

Frank Zammetti wrote:

I've always done

if (test == null || test.trim().equalsIgnoreCase("")) { }

(I'm anal about always using equalsIgnoreCase unless I know for sure
that
case sensitivity is required). No need to do anything more complex
than
that in my experience. "Always do the simplest thing that will work".

Frank


From: Robert Bateman <[EMAIL PROTECTED]>
Reply-To: "Tomcat Users List" <[EMAIL PROTECTED]>
To: "Tomcat Users List" <[EMAIL PROTECTED]>
Subject: Re: how to check if a String is empty?
Date: Fri, 18 Jun 2004 12:58:30 -0400

Wouldn't test.trim().length() be a better test? length() after trm

would
tell you if non white-space was left.

Bob


On Thursday 24 June 2004 12:30 pm, Peter Guyatt wrote:
Hi There,

    You could do the check test.length() > 0

Pete

-----Original Message-----
From: Carl Olivier [mailto:[EMAIL PROTECTED]
Sent: 24 June 2004 17:18
To: 'Tomcat Users List'
Subject: RE: how to check if a String is empty?


There is a trim() funtion in java.lang.String

?

-----Original Message-----
From: Marten Lehmann [mailto:[EMAIL PROTECTED]
Sent: 24 June 2004 06:20 PM
To: 'Tomcat Users List'
Subject: how to check if a String is empty?


Hello,

maybe this is not the perfect group for my question, but as my
problem
appears at the development of JSPs and tomcat is concerned with
that,
I
hope you can answer it.

I often see the condition

String test = req.getParameter("test");

if (test == null) {
    /* string is empty */
} else {
    /* string contains something */
}

But if test contains just blanks and other whitespaces, it's not
null,
but doesn't contain usable data anyhow. How can I check if a
string
contains whitespaces only? I though of something like

if (test == null || test.trim().equals("")) {
}

but there's no trim()-function, right? How do you solve this
problem?
With whitespaces I mean blanks, tabs and newlines.

Regards
Marten



-------------------------------------------------------------------- -
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



_________________________________________________________________ Make the most of your family vacation with tips from the MSN Family
Travel
Guide! http://dollar.msn.com


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


_________________________________________________________________ FREE pop-up blocking with the new MSN Toolbar – get it now! http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]

---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.682 / Virus Database: 444 - Release Date: 5/11/2004


--- Outgoing mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.682 / Virus Database: 444 - Release Date: 5/11/2004



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to