How do I use StringUtils in Java?

Java

Java Problem Overview


I'm a beginner in Java. I want to use StringUtils.replace but Eclipse outputs "StringUtils cannot be resolved".

I tried import java.lang.*;, but it doesn't work.

Java Solutions


Solution 1 - Java

java.lang does not contain a class called StringUtils. Several third-party libs do, such as Apache Commons Lang or the Spring framework. Make sure you have the relevant jar in your project classpath and import the correct class.

Solution 2 - Java

StringUtils is an Apache Commons project. You need to download and add the library to your classpath.

To use:

import org.apache.commons.lang3.StringUtils;

Solution 3 - Java

StringUtils is in org.apache.commons.lang.* not in java.lang.*. Most importantly learn to read javadoc file. All java programmers after learning basic java learn to read javadoc, execute tests from public projects, use those jars in their projects.

If you are working on eclipse or netbeans you can make a directory (folder) called lib in your project (from within the IDE) and copy the downloaded jar from hard disk and paste it in that directory from eclipse or netbeans. Next you have to add it to your project.

E.g in case of eclipse from Project->Properties select Java Build Path -> Add Jars, point to the jar you copied earlier. In your case it might be commons-lang-version.jar.

After this step whenever you add above import in a java file, those libraries will be available on your project (in case of eclipse or netbeans).

From where do you get the jar for commons-lang? Root directory of any apache commons is http://commons.apache.org/ And for commons-lang it is http://commons.apache.org/lang/

Some of these libraries contain User Guide and other help to get you started, but javadoc is the ultimate guide for any java programmer.

It is right time you asked about this library, because you should never re-invent the wheel. Use apache commons and other well tested libraries whenever possible. By using those libraries you omit some common human errors and even test those libraries (using is testing). Sometimes in future when using this library, you may even write some modifications or addition to this library. If you contribute back, the world benefits.

Most common use of StringUtils is in web projects (when you want to check for blank or null strings, checking if a string is number, splitting strings with some token). StringUtils helps to get rid of those nasty NumberFormat and Null exceptions. But StringUtils can be used anywhere String is used.

Solution 4 - Java

StringUtils is a utility class from Apache commons-lang (many libraries have it but this is the most common library). You need to download the jar and add it to your applications classpath.

Solution 5 - Java

If you're developing for Android there is TextUtils class which may help you:

import android.text.TextUtils;

It is really helps a lot to check equality of Strings.
For example if you need to check Strings s1, s2 equality (which may be nulls) you may use instead of

if( (s1 != null && !s1.equals(s2)) || (s1 == null && s2 != null) ) 
{ ... }

this simple method:

if( !TextUtils.equals(s1, s2) )
{ ... }

As for initial question - for replacement it's easier to use s1.replace().

Solution 6 - Java

The mostly used StringUtils class is the Apache Commons Lang StringUtils (org.apache.commons.lang3.StringUtils). To use this class you first have to download the Apache Commons Lang3 package then you have to add it to your project libraries.

You can go to this link to get more details: http://examples.javacodegeeks.com/core-java/apache/commons/lang3/stringutils/org-apache-commons-lang3-stringutils-example/

Solution 7 - Java

I am using Maven and I had to add this dependency to the pom.xml file:

<dependencies>
	<dependency>
		<groupId>org.apache.commons</groupId>
		<artifactId>commons-lang3</artifactId>
		<version>3.4</version>
	</dependency>
</dependencies>

Solution 8 - Java

StringUtils is part of Apache Commons Lang (http://commons.apache.org/lang/, and as the name suggest it provides some nice utilities for dealing with Strings, going beyond what is offered in java.lang.String. It consists of over 50 static methods.

There are two different versions available, the newer org.apache.commons.lang3.StringUtils and the older org.apache.commons.lang.StringUtils. There are not really any significant differences between the two. lang3.StringUtils requires Java 5.0 and is probably the version you'll want to use.

Solution 9 - Java

You need to import the jar file under: I am using IntelliJ so sharing my experience.

File/ProjectStructure/libraries/

you can download Jar file from this website:

https://commons.apache.org/proper/commons-lang/download_lang.cgi

Solution 10 - Java

It's simple to use.

StringUtils.isBlank(PassyourString);

This method will return either true or false without any NullPointerException.

But if you don't apache StringUtils. Then follow the below snippet

if(obj.getValue!=null)

or if you validating with a constant then follow the below code.

if("INDIA".equals(obj.getCountry)){
// write your code
}

This will not throw NullPointerException even if the object or value is null.

Solution 11 - Java

You can use this library with the below dependencies.

Maven

<dependency>

    <groupId>org.apache.commons</groupId>

    <artifactId>commons-lang3</artifactId>

    <version>3.12.0</version>
</dependency>

Gradle

implementation 'org.apache.commons:commons-lang3:3.12.0'

Note: The versions can differ.

One of the nice examples for usage is as below.

StringUtils.isBlank() This checks if a CharSequence is empty (""), null, or whitespace only.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLeo JiangView Question on Stackoverflow
Solution 1 - JavaJonikView Answer on Stackoverflow
Solution 2 - JavaErhan BagdemirView Answer on Stackoverflow
Solution 3 - JavaPramodView Answer on Stackoverflow
Solution 4 - JavaAravind YarramView Answer on Stackoverflow
Solution 5 - JavasberezinView Answer on Stackoverflow
Solution 6 - JavaRivu ChakrabortyView Answer on Stackoverflow
Solution 7 - JavaBartłomiej JanuszView Answer on Stackoverflow
Solution 8 - JavaWASEEMView Answer on Stackoverflow
Solution 9 - JavaManinderView Answer on Stackoverflow
Solution 10 - JavaSomanath BeheraView Answer on Stackoverflow
Solution 11 - JavaLahiru WijesekaraView Answer on Stackoverflow