if Statement checking for null String

do not know "not equal" to syntax

Page 1 of 1

3 Replies - 45689 Views - Last Post: 12 February 2009 - 10:55 AM Rate Topic: -----

#1 simonc  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 17-November 08

if Statement checking for null String

Post icon  Posted 12 February 2009 - 10:28 AM

Hi,

I am trying to check if a referenced variable (field) is not equal to String "null".
I know that in order to check if a referenced variable is equal to something the syntax is:

if (field.equals("null")) {

but what about if I want to check if a referenced variable is NOT equal to "null"?

need something like this

if (field.NOTequals("null")) {

Could someone help please

Many thanks

Is This A Good Question/Topic? 0
  • +

Replies To: if Statement checking for null String

#2 Locke  Icon User is offline

  • Sarcasm Extraordinaire!
  • member icon

Reputation: 521
  • View blog
  • Posts: 5,596
  • Joined: 20-March 08

Re: if Statement checking for null String

Posted 12 February 2009 - 10:45 AM

Just stick a negation operator in front of it.

if (field != null)) <-- the ! will handle the "Not".

Happy coding! :D

EDIT: You can also use if (!field.equals(""))

This post has been edited by Locke: 12 February 2009 - 10:54 AM

Was This Post Helpful? 0
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: if Statement checking for null String

Posted 12 February 2009 - 10:46 AM

The (string.equals(otherstring)) compares the contents of the string objects. To see if a variable references an object use the equality operator if (string1 == null) or as you asked if (string1 != null)

This post has been edited by n8wxs: 12 February 2009 - 10:49 AM

Was This Post Helpful? 0
  • +
  • -

#4 simonc  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 55
  • Joined: 17-November 08

Re: if Statement checking for null String

Posted 12 February 2009 - 10:55 AM

thanks that worked
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1