65

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.

1
  • 3
    This is a 3rd party library, so you have to pull it in and recompile with it in your classpath. Google search "apache commons jar". Download the zip or gz file for your architecture and java version. Make a new directory in your project called lib, or wherever it doesn't matter, and put the jar in there. when doing a compile via IDE, tell the project to include that jar. If you're using naked javac and java, add the jar to the classpath to point to that jar. At the top of your code file you have to utter the proper incantation: something like import org.apache.commons.lang3.StringUtils; Aug 12, 2018 at 3:51

11 Answers 11

60

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.

2
  • 1
    which of these should be used? I have the options of apache (both axis and soap), spring, and ibm. That's four choices!
    – Xonatron
    Feb 21, 2012 at 17:47
  • 2
    @MatthewDoucette Well, which utility method do you need? The StringUtils classes in those projects are far from identical. If you need something common that is provided by many classes that you already have in your project, it probably doesn't matter much. Read their respective Javadocs and choose one that suits your needs.
    – Jonik
    Feb 22, 2012 at 14:17
56

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;
13

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.

0
5

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.

3
  • Im a bit lost since Im just a newbie in Java. Im using eclipse I know how to import and add classpath BUT can't see downloadable libraries or jar in your link provided. Can you please help me sir? Thanks
    – Raf
    Aug 22, 2012 at 7:07
  • @user1078782 here is the download link commons.apache.org/lang/download_lang.cgi Aug 22, 2012 at 13:16
  • Which do you download and what do you do once it's downloaded? Jun 3, 2013 at 15:56
4

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().

2

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/

2

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>
1

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.

0

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

0

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.

0

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.