There are two variations of the substring() technique. This article portrays every one of them, as follows :
1. String substring(): This technique has two variations and returns another string that is a substring of this string. The substring starts with the person at the predefined list and reaches out to the furthest limit of this string. What's more, the file of substring begins from 1 and not from 0.
Syntax : public String substring(int begIndex) Parameters : begIndex : the begin index, inclusive. Return Value : The specified substring.
// Java code to demonstrate the // working of substring(int begIndex) public class Substr1 { public static void main(String args[]) { // Initializing String String Str = new String("Welcome to geeksforgeeks"); // using substring() to extract substring // returns (whiteSpace)geeksforgeeks System.out.print("The extracted substring is : "); System.out.println(Str.substring(10)); } }
Output:
The extracted substring is : geeksforgeeks
2. String substring(begIndex, endIndex): This strategy has two variations and returns another string that is a substring of this string. The substring starts with the person at the predefined list and stretches out to the furthest limit of this string or up to endIndex – 1 if the subsequent contention is given.
Syntax : public String substring(int begIndex, int endIndex) Parameters : beginIndex : the begin index, inclusive. endIndex : the end index, exclusive. Return Value : The specified substring.
// Java code to demonstrate the // working of substring(int begIndex, int endIndex) public class Substr2 { public static void main(String args[]) { // Initializing String String Str = new String("Welcome to geeksforgeeks"); // using substring() to extract substring // returns geeks System.out.print("The extracted substring is : "); System.out.println(Str.substring(10, 16)); } }
Output:
The extracted substring is : geeks
Possible application: The substring extraction discovers its utilization in numerous applications including prefix and suffix extraction. For instance to remove a Lastname from the name or concentrate just division from a string containing both sum and money image. The last one is clarified underneath.
// Java code to demonstrate the // application of substring() public class Appli { public static void main(String args[]) { // Initializing String String Str = new String("Rs 1000"); // Printing original string System.out.print("The original string is : "); System.out.println(Str); // using substring() to extract substring // returns 1000 System.out.print("The extracted substring is : "); System.out.println(Str.substring(3)); } }
Output :
The original string is : Rs 1000 The extracted substring is : 1000
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.