Substring Method In Java

Substring Method In Java | A substring is a subset of a string. Java provides a substring() method in the String class to extract some part of the string. This is a built-in method in java. There is a total of 2 substring() methods in the Java string class including its overloaded forms.

1) public String substring(int beginIndex)

  • Parameter:- beginIndex, the beginning index, inclusive.
  • Return Value:- the specified substring.
  • Throws:- IndexOutOfBoundsException, if beginIndex is negative or larger than the length of given String object.

2) public String substring(int beginIndex, int endIndex)

  • Parameter:- beginIndex, the beginning index, inclusive. endIndex the ending index, exclusive.
  • Return Value:- the specified substring.
  • Throws:- IndexOutOfBoundsException, if beginIndex is negative or endIndex is larger than the length of given String object or beginIndex is larger than the endIndex.

Note that the substring() method receives two parameters having beginIndex as inclusive and endIndex is exclusive.

Java Substring Example

Example-1:

String string = "I Love Programming";
System.out.println(string.substring(7));

Programming

We have mentioned only one index which is beginIndex, therefore, the substring() method returns all the characters from the given index to the end of the string. Hence the output is “Programming”.

Example-2:

String string = "I Love Programming";
System.out.println(string.substring(7,10));

Output:-

Pro

It returns “Pro” as ‘P’ starts from index 7 and ‘o’ is at index 9. This example takes two arguments the start index and end index and returns the characters between these two indexes. 

Find Substring In Java

In the main method, we call the substring() method and specify the start and end index. It returns the specified substring.

public class Main {
  public static void main(String[] args) {
    String string = "Know Program";
    String substring = string.substring(5, 12);
    System.out.println(substring);
  }
}

Output:-

Program

Java Replace Substring

We can also replace a substring with any other string, by using replace method now we will replace a substring.

public class Main {
   public static void main(String args[]) {
      String string = "I love programming";
      String substring = string.substring(7);
      String newString = string.replace(substring, "coding");

      System.out.println("Substring: " + substring);
      System.out.println("New String: " + newString);
   }
}

Output:-

Substring: programming
New String: I love coding

Longest Substring Without Repeating Characters Java

To find the longest substring we can apply the brute-force approach, this is the easiest approach to find the substring. To do this we loop the string from start to end i.e. we use a nested loop and if any unique characters are found return the length of that longest substring.

public class Main {
  public static String longestSubstring(String str) {
    int maxLen = 0;
    int beginIndex = 0;
    int endIndex = 0;
    for (int i = 0; i < str.length(); i++) {
      for (int j = i; j < str.length(); j++) {
        if (repetition(str, i, j)) {
          if(maxLen < (j-i+1)) {
            maxLen = j - i + 1;
            beginIndex = i;
            endIndex = j+1;
          }
        }
      }
    }
    return str.substring(beginIndex, endIndex);
  }

  private static boolean repetition(String str, int start, int end) {
    int[] chars = new int[128];
    for (int i = start; i <= end; i++) {
      char ch = str.charAt(i);
      chars[ch]++;
      if (chars[ch] > 1) {
        return false;
      }
    }
    return true;
  }

  public static void main(String args[]) {
    String str = "Know Program";
    System.out.println(longestSubstring(str));
  }
}

Output:-

Know Pr

It returns the length of the longest substring with unique characters “Know Pr”. After ‘r’ character, ‘o’ character comes which is already available in the substring.

Java Substring From Character

The below program returns a substring from a set of characters, here the substring takes only the start index and returns all the characters from that start index.

public class Main {
  public static void main(String args[]) {
    String string = new String("Know Program");
    System.out.print("Substring starting from index 5: ");
    System.out.println(string.substring(5));
  }
}

Output:-

Substring starting from index 5: Program

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.