➤ How to Code a Game
➤ Array Programs in Java
➤ Java Inline Thread Creation
➤ Java Custom Exception
➤ Hibernate vs JDBC
➤ Object Relational Mapping
➤ Check Oracle DB Size
➤ Check Oracle DB Version
➤ Generation of Computers
➤ XML Pros & Cons
➤ Git Analytics & Its Uses
➤ Top Skills for Cloud Professional
➤ How to Hire Best Candidates
➤ Scrum Master Roles & Work
➤ CyberSecurity in Python
➤ Protect from Cyber-Attack
➤ Solve App Development Challenges
➤ Top Chrome Extensions for Twitch Users
➤ Mistakes That Can Ruin Your Test Metric Program
Import StringUtils in Java | In this blog, we will study how to import the StringUtils class in Java. Importing this class is not relatively easy hence we have planned to make a blog so that it might be easy for the users to use StringUtils.
The StringUtils class is part of the Apache Common Lang package but not the part of Java language package. Therefore to import and use StringUtils class in our Java class first we need to add the jar file of the Apache Common Lang package.
If you are working with a Maven project then you can simply add the dependency for it in the pom.xml file or if you are working with a simple normal Java project then you can download the jar file and add it into the project classpath.
Import StringUtils in Java Maven Project
If you are working with the Maven project then to use StringUtils class we can use Maven Dependency. Maven Dependency is just an archive like a JAR or ZIP file which is needed for the project to run, compile, build or test. These dependencies are usually stored in the pom.xml file.
Add the following entry for Apache Common Lang dependency in between <dependencies> and </dependencies>.
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
You can also get the latest dependency information from here. After adding the dependency update the Maven project so that it can download the jar file and source code of the added dependency.
Import StringUtils in Java Eclipse
If you are developing a simple Java project then you have to download the Apache Common Lang jar file and add it to the project classpath. Download the Binaries file, and extract commons-lang3-<version>.jar from the zip or tar file.
Select project => Right click => Build Path => Configure build path => Libraries => Classpath => Add external jar => Select path of commons-lang3-3.12.0.jar file => Apply => Apply and Close.
Java StringUtils Import Example
After adding all the required dependencies we need to import StringUtils to the program hence test the below code.
import org.apache.commons.lang3.StringUtils;
public class Test {
public static void main(String[] args) {
String value = StringUtils.abbreviate("Happy morning", 4);
System.out.println(value);
value = StringUtils.capitalize("Happy morning");
System.out.println(value);
value = StringUtils.chop("Happy morning");
System.out.println(value);
int a = StringUtils.compare(value, value);
System.out.println(a);
boolean b = StringUtils.contains("Happy morning", "ft");
System.out.println(b);
int c = StringUtils.indexOf(value, 'f');
System.out.println(c);
int d = StringUtils.lastIndexOf("Happy morning", 't');
System.out.println(d);
value = StringUtils.lowerCase("HAPPY MORNING");
System.out.println(value);
value = StringUtils.repeat("Happy morning", 2);
System.out.println(value);
value = StringUtils.reverse("Happy morning");
System.out.println(value);
value = StringUtils.truncate("Happy morning", 2);
System.out.println(value);
value = StringUtils.upperCase("Happy morning");
System.out.println(value);
}
}
Output:-
H…
Happy morning
Happy mornin
0
false
-1
-1
happy morning
Happy morningHappy morning
gninrom yppaH
Ha
HAPPY MORNING
If the Apache Common Lang package was added successfully then StringUtils can be imported and used in the class else you will get an error.
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!