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. public class StringEmptyExample{  
  2. public static void main(String args[]){  
  3. String s1="";  
  4. String s2="java";  
  5. System.out.println(s1.isEmpty());  
  6. System.out.println(s2.isEmpty());  
  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());  
}
}  


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. public class StartsWithExample{  
  2. public static void main(String args[]){  
  3. String s1="java in simple way";  
  4. System.out.println(s1.startsWith("ja"));  
  5. System.out.println(s1.startsWith("java in"));
  6.  
  7. System.out.println(s1.startsWith("simple"));  
  8. System.out.println(s1.startsWith("simple",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));

}
}  



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. public class EndsWithExample{  
  2. public static void main(String args[]){  
  3. String s1="java in simple way";  
  4. System.out.println(s1.endsWith("way"));  
  5. System.out.println(s1.endsWith("simple way"));
  6. System.out.println(s1.endsWith("simple"));  
  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"));  

}
}  


About the Author

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