Home > Java > javaTutorial > How to determine whether a string is empty or null in java?

How to determine whether a string is empty or null in java?

青灯夜游
Release: 2019-12-31 16:12:10
Original
4720 people have browsed it

How to determine whether a string is empty or null in java?

#How does Java determine whether a string is empty or null?

First, distinguish between empty strings and null strings:

The empty string "" is a string of length 0, which has its own string length (0) and content (empty), the method to determine whether a string is empty:

 if (str.length() == 0)
Copy after login

or

if (str.equals(""))
Copy after login

null string means that there is currently no object associated with the variable, the method to check whether a string is null:

if (str == null)
Copy after login

Check that a string is neither a null string nor an empty string. Use the following method to determine:

if (str != null && str.length() != 0)
Copy after login

Note: You must first check that str is not null, otherwise in An error will occur if the length() method is increased by a null value.

Use the StringUtils tool class to determine whether it is neither null nor empty, as follows:

if (StringUtils.isNotBlank(str))
Copy after login

Recommended learning: Java video tutorial

The above is the detailed content of How to determine whether a string is empty or null in java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template