GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java - String substring() Method
Selected Reading
Java - String substring() MethodThe Java String substring() method is used to retrieve a substring of a String object. The substring starts with the character at the given index and continues to the end of the current string. Since String is immutable in Java, this method always returns a new string, leaving the previous string untouched. There are two polymorphic variants of this method, syntaxes of which are given below; one allows you to specify only the start index, and the other allows you to specify both the start and end indexes. SyntaxFollowing is the syntax for Java String substring() method: public String substring(int beginIndex) // first syntax or, public String substring(int beginIndex, int endIndex) // second syntax Note − The method will return all the characters from startIndex if endIndex is not specified. Parameters
Return ValueThis method returns the specified substring. Getting the Substring from a String Using Begin Index ExampleThe following example shows the usage of Java String substring() method by finding the substring of the string "This is tutorials point". Here, only the begin index is passed as a argument to this method − package com.tutorialspoint;
public class StringDemo {
public static void main(String[] args) {
String str = "This is tutorials point";
String substr = "";
// prints the substring after index 7
substr = str.substring(7);
System.out.println("substring = " + substr);
// prints the substring after index 0 i.e whole string gets printed
substr = str.substring(0);
System.out.println("substring = " + substr);
}
}
OutputIf you compile and run the above program, it will produce the following result − substring = tutorials point substring = This is tutorials point Getting the Substring from a String Using Begin and End Index ExampleFollowing is an example to show the use of substring() method in Java by finding the substring of the provided string. Here both the beginning and the ending of the string is passed as parameters − package com.tutorialspoint;
public class StringDemo {
public static void main(String[] args) {
String str = "This is tutorials point";
String substr = "";
// prints the substring after index 7 till index 17
substr = str.substring(7, 17);
System.out.println("substring = " + substr);
// prints the substring after index 0 till index 7
substr = str.substring(0, 7);
System.out.println("substring = " + substr);
}
}
OutputLet us compile and run the program above, the output will be displayed as follows − substring = tutorials substring = This is Getting the Substring from a String Between Braces ExampleIn this example we are trying to retrieve the substring with in the brackets from a string. Here, we are using indexOf() method to find the indices of both open and closed braces. Once we obtain these we are using the substring() method to get the substring within this range. package com.tutorialspoint;
public class StringDemo {
public static void main(String[] args) {
String s = "Stabbed in the (back)";
// getting substring between '(' and ')' delimeter
int begin = s.indexOf("(");
int ter = s.indexOf(")");
String res = s.substring(begin + 1, ter);
System.out.println("The substring is: " + res);
}
}
OutputOn executing the program above, the output is obtained as follows − The substring is: back Getting the Substring from a String Between Braces ExampleTill now we have used index values to determine the boundaries of a desired substring. We can also print a substring existing between two given string values. Following is an example to do so − package com.tutorialspoint;
public class StringDemo {
public static void main(String[] args) {
String s = "Stabbed in the (back)";
// getting substring between '(' and ')' delimeter
int begin = s.indexOf("(") + 1;
int ter = s.indexOf(")");
String res = s.substring(begin, ter);
System.out.println("The substring is: " + res);
}
}
OutputFollowing is the output of the above program − The substring is: back Removing the first and last characters from a string ExampleThe substring() method is used in this program to remove the first and last characters from a string. package com.tutorialspoint;
public class StringDemo {
public static void main(String args[]) {
String s = new String("Programming");
System.out.print("Substring after eliminating the first character is: ");
// excluding the first character since index(0) is excluded
System.out.println(s.substring(1));
System.out.print("Substring after eliminating the last character is: ");
// excluding the last character by providing index as -1
System.out.println(s.substring(0, s.length() - 1));
System.out.println(
"Substring after eliminating both the first and the last character is: " + s.substring(1, s.length() - 1));
}
}
OutputFollowing is an output of the above code − Substring after eliminating the first character is: rogramming Substring after eliminating the last character is: Programmin Substring after eliminating both the first and the last character is: rogrammin java_lang_string.htm Advertisements
|
| You are visitor 01402520 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |