isEmpty, startswith and endsWith
isEmpty
This method is used to check whether string has any characters in it or its empty string. It returns true if length of a string is zero and false otherwise.
Method signature
- public boolean isEmpty()
public boolean isEmpty()
Example
- public class StringEmptyExample{
- public static void main(String args[]){
- String s1="";
- String s2="java";
- System.out.println(s1.isEmpty());
- System.out.println(s2.isEmpty());
- }
- }
public class StringEmptyExample{ public static void main(String args[]){ String s1=""; String s2="java"; System.out.println(s1.isEmpty()); System.out.println(s2.isEmpty()); } }
Note: This method is not safe if the string value is null as it throws NullPointerException in that case.
So as an alternative to this method, we can get some utility methods from StringUtils to find empty string and also they take care of null value.
StartsWith
This method checks if the given string starts with a string specified in the parameter and returns true if its starts with the specified string and false otherwise.
It has 2 overloaded methods
public boolean startsWith(String prefix)
public boolean startsWith(String prefix, int toffset)
public boolean startsWith(String prefix)
Tests if this string starts with the specified prefix.
It returns true if the character sequence represented by the argument is a prefix of the character sequence represented by this string false otherwise. Note also that true will be returned if the argument is an empty string or is equal to this String object as determined by the equals(Object) method.
public boolean startsWith(String prefix, int toffset)
Tests if the substring of this string beginning at the specified index starts with the specified prefix.
It returns true if the character sequence represented by the argument is a prefix of the substring of this object starting at index toffset false otherwise. The result is false if toffset is negative or greater than the length of this String object
Example
- public class StartsWithExample{
- public static void main(String args[]){
- String s1="java in simple way";
- System.out.println(s1.startsWith("ja"));
- System.out.println(s1.startsWith("java in"));
- System.out.println(s1.startsWith("simple"));
- System.out.println(s1.startsWith("simple",8));
- }
- }
public class StartsWithExample{ public static void main(String args[]){ String s1="java in simple way"; System.out.println(s1.startsWith("ja")); System.out.println(s1.startsWith("java in")); System.out.println(s1.startsWith("simple")); System.out.println(s1.startsWith("simple",8)); } }
EndsWith
This method checks if the given string ends with a specified string in the parameter and returns true if its ends with the specified string and false otherwise.
Method signature
- public boolean endsWith(String suffix)
public boolean endsWith(String suffix)
This method Tests if this string ends with the specified suffix.
It returns true if the character sequence represented by the argument is a suffix of the character sequence represented by this object false otherwise. Also note that the result will be true if the argument is the empty string or is equal to this String object
Example
- public class EndsWithExample{
- public static void main(String args[]){
- String s1="java in simple way";
- System.out.println(s1.endsWith("way"));
- System.out.println(s1.endsWith("simple way"));
- System.out.println(s1.endsWith("simple"));
- }
- }
public class EndsWithExample{ public static void main(String args[]){ String s1="java in simple way"; System.out.println(s1.endsWith("way")); System.out.println(s1.endsWith("simple way")); System.out.println(s1.endsWith("simple")); } }