java - Check whether a string is not null and not empty -
how can check whether string not null , not empty?
public void dostuff(string str) { if (str != null && str != "**here want check 'str' empty or not**") { /* handle empty string */ } /* ... */ }
what isempty() ?
if(str != null && !str.isempty()) be sure use parts of && in order, because java not proceed evaluate second part if first part of && fails, ensuring not null pointer exception str.isempty() if str null.
beware, it's available since java se 1.6. have check str.length() == 0 on previous versions.
to ignore whitespace well:
if(str != null && !str.trim().isempty()) wrapped in handy function:
public static boolean empty( final string s ) { // null-safe, short-circuit evaluation. return s == null || s.trim().isempty(); } becomes:
if( !empty( str ) )
Comments
Post a Comment