String trim, length, is empty, and substring

In this chapter you will learn:

  1. Trim a string, remove the leading and ending spaces
  2. How to get sub string from a string
  3. How to check if a string is an empty string
  4. How to Get the length of a string

Trim a string, remove the leading and ending spaces

String trim() returns a copy of the string, with leading and trailing whitespace omitted.

public class Main {
  public static void main(String[] argv) {
    String str = "     d e m o 2 s.com    ";
    System.out.println(">"+str+"<");
    str = str.trim();//from   j av a  2 s  .  c o  m
    System.out.println(">"+str+"<");
  }
}

The output:

Get sub string from a string

String class has methods to get only part of a string. They are useful when we want to get specific part of a string. We usually use the indexOf and lastIndexOf method to get the index first and then use the substring method get the string we are looking for.

  • CharSequence subSequence(int beginIndex, int endIndex) returns a new character sequence that is a subsequence of this sequence.
  • String substring(int beginIndex) returns a new string that is a substring of this string.
  • String substring(int beginIndex, int endIndex) returns a new string that is a substring of this string.
public class Main {
  public static void main(String[] argv) {
    String str = "demo2s.com";
    System.out.println(str.substring(2));
    System.out.println(str.substring(1,3));
  }/* j  a v a2s.c  om*/
}

The output:

if string is empty

boolean isEmpty() returns true if, and only if, length() is 0. It is useful to check if a string has no value in it, in other words, if a string is "". However isEmpty would not work if a string object is null.

public class Main {
  public static void main(String[] argv) {
    String str = "";
    System.out.println(str.isEmpty());/*from   ja v a  2 s.c o  m*/
    System.out.println(str.length());
    
    str = "demo2s.com";
    System.out.println(str.isEmpty());
    System.out.println(str.length());
  }
}

The output:

To check if a string object is null, use the following

public class Main {
  public static void main(String[] argv) {
    String str = "";
    System.out.println(str.isEmpty());// j a v a 2 s . c  om
    System.out.println(str.length());
    
    str = null;
    if(str == null){
       System.out.println("string is empty");
    }
    
    str = "demo2s.com";
    System.out.println(str.isEmpty());
    System.out.println(str.length());
    
    
  }
}

Get string length

The length of a string is the number of character in a string. int length() returns the length of this string.

public class Main {
  public static void main(String[] argv) {
    String str = "";
    System.out.println(str.isEmpty());/*from   j av a2s.c  o  m*/
    System.out.println(str.length());
    
    str = "demo2s.com";
    System.out.println(str.isEmpty());
    System.out.println(str.length());
  }
}

The output:

Next chapter...

What you will learn in the next chapter:

  1. Replaces all occourances of given character with new one and returns new String object
  2. Replaces only first occourances of given String with new one and returns new String object
  3. Replaces all occourances of given String with new one and returns new String object
  4. How to replace escaped string
  5. Replace \r\n with the <br> tag
Home » Java Tutorial » String

String

    Java String type
    Java String Concatenation
    Java String Creation
    Java String Compare
    Java String Search
    Java String and char array
    Java String Conversion
    String trim, length, is empty, and substring
    String replace

StringBuffer

    StringBuffer class
    StringBuffer Insert and Append
    StringBuffer length and capacity
    StringBuffer char operation
    StringBuffer Operations
    Search within StringBuffer
    StringBuffer to String

StringBuilder

    StringBuilder
    StringBuilder insert and append
    StringBuilder length and capacity
    StringBuilder get,delete,set char
    StringBuilder delete, reverse
    StringBuilder search with indexOf and lastIndexOf
    StringBuilder to String

String Format

    Formatter class
    Format Specifier
    Format String and characters
    Format integer value
    Format decimal
    Scientific notation format
    Format octal and hexadecimal value
    Format date and time value
    Escape Formatter
    Minimum Field Width
    Specifying Precision
    Format Flags
    Uppercase Option
    Formatter Argument Index
    Align left and right
    Left and right padding a string

String Format Utilities

    Abbreviate string
    Caplitalize a string
    Uncapitalize a string
    Utility class for right padding
    Left padding
    Centers a String
    Transforms words