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

1
public boolean isEmpty()
public boolean isEmpty()


Example

1
2
3
4
5
6
7
8
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

1
2
3
4
5
6
7
8
9
10
11
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

1
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

1
2
3
4
5
6
7
8
9
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"));  

}
}  


About the Author

Karibasappa G C (KB)
Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on