Substring Contains String

Substring Contains String

In C/C++, string is an array of characters with a terminating /0. But in Java, the scenario is completely different. In Java, string exists as a class String from java.lang package. To make string manipulation easy, Designers introduced many methods in String class with different functionality. Of them two are given substring() and contains() in this tutorial Substring Contains String.

Before going into example, let us see method signatures as defined in String class. Both are overloaded with different parameters.

  1. String substring(int indexNum): Returns a string containing all the characters existing from indexNum to the end in the given string.
  2. String substring(int indexNum1, int indexNum2): Returns a string containing all the characters existing from indexNum1 to indexNum2-1 in the given string.
  1. boolean contains(String str): Checks whether a string exists in another string. Returns boolean value of str exists in the original string.
Following is an example on Substring Contains String

Substring ContainsOutput Screenshot on Substring Contains String

Methods are self-explanatory. Let us write a practical example.

Substring ContainsOutput Screenshot on Substring Contains Java

Leave a Reply

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