In this post, we will see org.apache.commons.lang3.StringUtils isNumeric() example in Java. The isNumeric() method is used to check given character is a number or not. It returns true if the given character is a number else returns false.
We need to add the below dependency in maven to use org.apache.commons.lang3.StringUtils isNumeric() method. We can download apache-commons maven dependency from here.
pom.xml changes
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
StringUtils isNumeric() Example in Java
package com.javatute;
import org.apache.commons.lang3.StringUtils;
public class StringUtilsIsNumeric {
public static void main(String[] args) {
String s1 = "a12";
String s2 = "30";
System.out.println(StringUtils.isNumeric(s1));
System.out.println(StringUtils.isNumeric(s2));
}
}
Out put is
false
true
Syntax of isNumeric() – public static boolean isNumeric(final CharSequence cs)
Note – Internally isNumeric() uses the Character.isDigit() method. The isNumeric() method checks for empty also. If we provide an empty string or a character it will return false. It accepts CharSequence as argument.
package com.javatute;
import org.apache.commons.lang3.StringUtils;
public class StringUtilsIsNumeric {
public static void main(String[] args) {
System.out.println(StringUtils.isNumeric(null));//false
System.out.println(StringUtils.isNumeric("f21"));//false
System.out.println(StringUtils.isNumeric("21"));//true
System.out.println(StringUtils.isNumeric("jbl"));//false
System.out.println(StringUtils.isNumeric(" "));//false
}
}
Out put is
false
false
true
false
false
Let’s see a few questions related to StringUtils isNumeric() method.
How do I make sure a string only has number?
We can use StringUtils isNumeric() method. if it returns true then the given string has only numbers.
Note – We can also use Character.isDigit() to check given String isNumeric or not.
Is StringUtils isNumeric() method null safe?
Yes. It will return false for null or empty. See the above code snippet.
What is the method StringUtils isNumeric() internally uses?
The isNumeric() method internally uses Character isDigit() method.
How to know if any String contains a number?
Consider we have string s1 = “12hg89” and we want to check whether s1 contains a number or not. Unfortunately, java doesn’t have any API to validate this use case. But we can use String matches()
method to check given String contains a number or not.
package com.javatute.com;
public class StringUtilExample{
public static void main(String[] args) {
String str = "12hhgg7";
System.out.println(str.matches(".*\\d.*"));
//true
}
}
Output is –
true
That’s all about StringUtils isNumeric() example in Java.
See docs.
Related post.
- CollectionUtils isEmpty() Example in Java.
- StringUtils isEmpty() and IsBlank() Example in Java.
- StringUtils join() Example in Java.
- CollectionUtils union() Example in Java
- CollectionUtils intersection() Example in Java
- CollectionUtils isEqualCollection() Example in Java
- StringUtils isAlpha() example in Java