Write a program to return part of a string

You can get part of a string by following two methods:

  1. substring(index) : This is a method of removing string before the index position
  2. substring(0,index) : This is a method to return string from 0 index to its given index

Suppose, there is a string as, “Hello Java”

If you want to return only Hello, then you have to use substring(0,6). In this string, there are total 10 characters. substring(0,6) means, remove characters after 6th character. So it will return hello.

If you want to return Java, then you have to use substring(6). It will remove 1st 6 characters from the string and will return Java.

Program to evaluate above scenario:

public static void main(String[] args) {
      String s="Hello Java";
        System.out.println(s.substring(0,6));
        System.out.println(s.substring(6));
    }
Output:
Hello 
Java
about author

admin

salmansrabon@gmail.com

If you like my post or If you have any queries, do not hesitate to leave a comment.

Leave a Reply

Your email address will not be published. Required fields are marked *