>
Note 1: Make sure that substring is the only String class method in Java, that does not follow Camel-case.
Note 2: Indexes are managed as shown in the following example:
For example: Considering String message = "Welcome to Java";
Click HERE for the Comparison Methods of String class.
The String Class: (Part 3)
Substring Methods of String class:

Note 1: Make sure that substring is the only String class method in Java, that does not follow Camel-case.
Note 2: Indexes are managed as shown in the following example:
For example: Considering String message = "Welcome to Java";

- Example: (substring ->one argument)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class test { public static void main(String ar[]) { String str1 = "Good Morning!"; System.out.println(str1.substring(3)); // prints d Morning -> from index 3 to end of string System.out.println(str1.substring(0)); // prints Good Morning! -> from index 0 to end of string System.out.println(str1.substring(str1.length()-1)); // prints ! -> length: 13. So print from index 12 to end of string, that is the last character. } } - Example: (substring ->two arguments)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
class test { public static void main(String ar[]) { String str1 = "Good Morning!"; System.out.println(str1.substring(3,7)); // inclusive of the first argument, and exclusive of last argument. // prints d Mo starting with index 3 to 7-1 (that is till 6) System.out.println(str1.substring(0,str1.length())); // prints Good Morning! starting with index 0 to 13-1 (that is till 12) System.out.println(str1.substring(5,str1.length())); // prints Morning! starting with index 5 to 13-1 (that is till 12) } }
Click HERE for the Comparison Methods of String class.
Comments
Post a Comment