Grails Cookbook - A collection of tutorials and examples

Java Substring

One of the necessary inclusion of your tool in controlling String is the substring method. Java Substring is a valuable method in Core Java Programming. It's job is to remove or take out a sequence of characters from a String and swaps it with a new String value. By the end of this post, you will be able to understand how Java Substring works.

Signatures

There are two variants in Java String's substring method that we should be aware of, the first one is that it only takes one argument, and on the other hand, it takes two arguments.

  • substring(int beginIndex)

    When a new String object contains the substring of the set string from the specified startIndex (inclusive), this method will surely send it back. Now from the given beginIndex through the last character of the String, this method will acquire the part of a String.

    Understanding The Syntax
    public String substring(int beginIndex)
    

    This only have one argument beginIndex - which is the index from the beginning removing the returned substring(inclusive)

    If you're still confused, think it this way: the main idea is that all the characters from the index given by beginIndex will be obtained. Consider this, we have the String "ADDRESS", understand that at index 3 is the character "R". So all the characters from "R" upto "S" will be returned, therefore, our String is "RESS".

    Sample Program

    Check out the written examples of the method:
    public class SubstringSampleTesting {
       public static void main(String[] args) {
          String SampletestString = "JKLMNOPQRS";
          System.out.println(testString.substring(0));
          System.out.println(testString.substring(2));
          System.out.println(testString.substring(4));
          System.out.println(testString.substring(6));
          System.out.println(testString.substring(8));
       }
    }
    
    Observe the result of the code below:
    JKLMNOPQRS
    LMNOPQRS
    NOPQRS
    PQRS
    RS
    

  • substring(int beginIndex, int endIndex)

    Just like the concept of substring(int beginIndex), only this time, we shall introduce you with substring(int endIndex). This method will return a new String object containing the substring from the startIndex to endIndex. From the specified beginIndex through the last character of the String, this method will acquire a portion of a String.

    Understanding The Syntax
    public String substring(int beginIndex, int endIndex)
    

    This now have two arguments. The first is beginIndex - the index where to begin extracting the returned substring.(inclusive). The second is endIndex - the index where to end extracting the returned substring. (exclusive).

    The objective is that we know where to begin and finish duplicating characters from the source String. Note that the beginIndex is included, that's not the case with endIndex.

    Let's say we have the String "SILVER", since "I" is found at index 1 and "R" is at index 5, the returned value would be the characters from "I" upto the "E" (ILVE). Note that we didn't include the last character "R". This is because beginIndex acquires the String where we start extracting and the endIndex returns the String where we end the substring but not exactly taking out that specific substring.

    Observe the written example below for a better understanding:

    Understanding The Example

    public class SampleSubstringTesting {  
    	   public static void main(String[] args) {
    	      String SampleString = "KLMNOPQRST";
    	      System.out.println(testString.substring(0,5));
    	      System.out.println(testString.substring(2,9));
    	      System.out.println(testString.substring(5,10));
    	   }
    	}
    
    Check out the output below:
    KLMNO
    MNOPQRS
    PQRST