Java substring example | method, indexof, find from end

A SubString is part of any string. Some places programmers need to get, find or fetch a substring in java. Java substring nothing a substring is a subset of another string.

Java substring example

To getting a substring in java you can use the inbuilt “Java substring method“. A SubString() method has 2 variants.

  • Single parameter- substring(int beginIndex)
  • double parameter- substring(int beginIndex, int endIndex)

Java substring example

Let’s see example one by using both variants of the subString Java method. There can be more ways to do it, but if the method is available then must use it.

1. substring(int beginIndex)

Parameters :

  • begIndex: the begin index, inclusive.

Pass the int value in the substring() method, it will return a string. A return value of substring is started char from given int value till the end of the string.

public class Hello {

    public static void main(String args[]) {

        String Str = new String("Hello world Java tutorial ");

        // using substring() to extract substring
        // returns tutorial
        System.out.print("The extracted substring is: ");
        System.out.println(Str.substring(17));
    }
}

Output: The extracted substring is: tutorial

2. substring(int beginIndex, int endIndex)

Parameters :

  • beginIndex: the begin index, inclusive.
  • endIndex: the end index, exclusive.

Need to pass the 2 values, the first value will start with a character and the second one will stop without taking this character. See below the code of it.

public class Hello {

    public static void main(String args[]) {

        String Str = new String("Hello world Java tutorial ");

        // using substring() to extract substring
        // returns java
        System.out.print("The extracted substring  is: ");
        System.out.println(Str.substring(12, 16));
    }
}

Output: The extracted substring is: Java

What is the Java substring indexof?

In Java String indexOf() is the method, which returns the index within this string of the first occurrence of the given character or -1, if the character does not found. See the below example.

public class Hello {

    public static void main(String args[]) {

        String Str = new String("Hello world Java tutorial ");

        System.out.println(Str.indexOf('w'));
    }
}

Output: 6

Find substring in a string java

Use the contain method to find substring or word in any string.

str1.contains(str2))

Question: How to get Java substring from the end?

Answer: See below example code extract last 8 characters from a string in Java.


public class Hello {

    public static void main(String args[]) {

        String Str = new String("Hello world Java tutorial");

        String lastchar = getLastnCharacters(Str,8);

        System.out.println("Last 8 digits are " + lastchar);
    }

    public static String getLastnCharacters(String inputString,
                                     int subStringLength){
        int length = inputString.length();
        if(length <= subStringLength){
            return inputString;
        }
        int startIndex = length-subStringLength;
        return inputString.substring(startIndex);
    }
}

Output: Last 8 digits are tutorial

Question: What will be output if substring 0 0 java.

Answer: A method will return nothing (empty string). Because the index starts from zero, so if you pass the value 0, 0 then counts of value are also zero, which returns nothing.

Do comment if you have any doubts and suggestions on this tutorial. There can be more ways to do it if you know any other best way, which should be in this tutorial then comment it.

Note: This example (Project) is developed in IntelliJ IDEA 2018.2.6 (Community Edition)
JRE: 11.0.1
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.14.1
Java version 11
All Java substring Examples are in Java 11, so it may change on different from Java 9 or 10 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.