Which of these method of class string is used to extract a substring from a string object * substring () substring () substring () none of the mentioned?
The JavaScript string is an object that represents a sequence of characters. The substr() method extracts parts of a string, beginning at the character at the specified position, and returns the specified number of characters. The substring() method returns the part of the string between the start and end indexes, or to the end of the string. Syntaxstring.substr(start, length) start: The position where to start the extraction, index starting from 0. length: The number of characters to extract (optional).examplevar s = "JavaScript"; var st = s.substr(4, 6); alert(st);The above code would return "Script". Syntaxstring.substring(start, end) start: The position where to start the extraction, index starting from 0. end: The position (up to, but not including) where to end the extraction (optional).examplevar s = "JavaScript"; var st = s.substr(4, 10); alert(st);The above code would return "Script" substr() Vs. substring()The difference is in the second argument. The second argument to substring is the index to stop at (but not include), but the second argument to substr is the maximum length to return. Moreover, substr() accepts a negative starting position as an offset from the end of the string. substring() does not. Next : How to replace all occurrences of a string in JavaScript? Improve Article Save Article Like Article Improve Article Save Article In StringBuffer class, there are two types of substring method depending upon the parameters passed to it. substring(int start)The substring(int start) method of StringBuffer class is the inbuilt method used to return a substring start from index start and extends to end of this sequence. The string returned by this method contains all character from index start to end of the old sequence. Syntax: public String substring(int start)Parameters: This method accepts only one parameter start which is Integer type value refers to the start index of substring. Return Value: This method returns the substring lie in the range start to end of old sequence. Exception: This method throws StringIndexOutOfBoundsException if start is less than zero, or greater than the length of this object. Below programs illustrate the StringBuffer substring() method: Example 1:
Output:String contains = GeeksForGeeks SubSequence = ForGeeks Example 2: To demonstrate StringIndexOutOfBoundsException
Output:Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: -7 substring(int start, int end)The substring(int start, int end) method of StringBuffer class is the inbuilt method used to return a substring start from index start and extends to the index end-1 of this sequence. The string returned by this method contains all character from index start to index end-1 of the old sequence. Syntax: public String substring(int start)Parameters: This method accepts two parameter start which is Integer type value refers to the start index of substring and end which is also a Integer type value refers to the end index of substring. Return Value: This method returns the substring lie in the range index start to index end-1 of old sequence. Exception: This method throws StringIndexOutOfBoundsException if start or end are negative or greater than length(), or start is greater than end. Below programs illustrate the StringBuffer.substring() method: Example 1:
Output:String contains = GeeksForGeeks SubSequence = For Example 2: To demonstrate StringIndexOutOfBoundsException
Output:Exception: java.lang.StringIndexOutOfBoundsException: String index out of range: -6 References:
DownloadApp
Home » JAVA Programming » Strings » Question
substring()
|