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";
about author



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 *