Which of this method of class String is used to extract a substring from a String object

Java String substring() method returns the substring of this string. This method always returns a new string and the original string remains unchanged because String is immutable in Java.

Java String substring() Methods

Which of this method of class String is used to extract a substring from a String object
Java String substring method is overloaded and has two variants.

  1. substring(int beginIndex): This method returns a new string that is a substring of this string. The substring begins with the character at the specified index and extends to the end of this string.
  2. substring(int beginIndex, int endIndex): The substring begins at the specified beginIndex and extends to the character at index endIndex - 1. Thus the length of the substring is (endIndex - beginIndex).

String substring() Method Important Points

  1. Both the string substring methods can throw IndexOutOfBoundsException if any of the below conditions met.
    • if the beginIndex is negative
    • endIndex is larger than the length of this String object
    • beginIndex is larger than endIndex
  2. beginIndex is inclusive and endIndex is exclusive in both substring methods.

Java String substring() Example

Here is a simple program for the substring in java.

package com.journaldev.util;

public class StringSubstringExample {

	public static void main(String[] args) {
		String str = "www.journaldev.com";
		System.out.println("Last 4 char String: " + str.substring(str.length() - 4));
		System.out.println("First 4 char String: " + str.substring(0, 4));
		System.out.println("website name: " + str.substring(4, 14));
	}
}

Output of the above substring example program is:

Last 4 char String: .com
First 4 char String: www.
website name: journaldev

Checking Palindrome using substring() Method

We can use the substring() method to check if a String is a palindrome or not.

package com.journaldev.util;

public class StringPalindromeTest {
	public static void main(String[] args) {
		System.out.println(checkPalindrome("abcba"));
		System.out.println(checkPalindrome("XYyx"));
		System.out.println(checkPalindrome("871232178"));
		System.out.println(checkPalindrome("CCCCC"));
	}

	private static boolean checkPalindrome(String str) {
		if (str == null)
			return false;
		if (str.length() <= 1) {
			return true;
		}
		String first = str.substring(0, 1);
		String last = str.substring(str.length() - 1);
		if (!first.equals(last))
			return false;
		else
			return checkPalindrome(str.substring(1, str.length() - 1));
	}
}

Here we are checking if the first letter and the last letter is the same or not. If they are not the same, return false. Otherwise, call the method again recursively passing the substring with the first and last letter removed.

You can checkout more string examples from our GitHub Repository.

Reference: Oracle API Doc

Which of this method of class String is used to extract a substring from a String object


This JavaScript tutorial explains how to use the string method called substr() with syntax and examples.

Description

In JavaScript, substr() is a string method that is used to extract a substring from a string, given a start position and a length. Because the substr() method is a method of the String object, it must be invoked through a particular instance of the String class.

WARNING: It is recommended that you use the substring() method instead, if possible. The substr() method is a legacy function.

Syntax

In JavaScript, the syntax for the substr() method is:

string.substr(start_position [, length]);

Parameters or Arguments

start_positionThe position in string where the extraction will start. The first position in string is 0. If a negative value is provided for this parameter, the substr() method will measure the position from the end of the string. For example, a value of -1 is the last character in the string, a value of -2 is the second last character in the string and so on.lengthOptional. It is the number of characters to extract from string. If this parameter is not provided, the substr() method will extract to the end of the string.

Returns

The substr() method returns a substring that has been extracted from string given the start_position and length to extract.

Note

  • The substr() method does not change the value of the original string.
  • See also the slice() and the substring() methods.

Example

Let's take a look at an example of how to use the substr() method in JavaScript.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.substr(0,4));
console.log(totn_string.substr(4,2));
console.log(totn_string.substr(6,3));
console.log(totn_string.substr(9,3));

In this example, we have declared a variable called totn_string that is assigned the string value of 'TechOnTheNet'. We have then invoked the substr() method of the totn_string variable to extract the substring from the string.

We have written the output of the substr() method to the web browser console log, for demonstration purposes, to show what the substr() method returns.

The following will be output to the web browser console log:

Tech
On
The
Net

As you can see, the substr() method returned 'Tech' for the first call, 'On' for the second call, 'The' for the third call and 'Net' for the fourth call.

Specifying a Negative Start Position

You can also use a negative start_position to extract a substring using the substr() method. A negative start_position allows you to determine a position relative to the end of the string when extracting the substring.

For example:

var totn_string = 'TechOnTheNet';

console.log(totn_string.substr(-3));

The following will be output to the web browser console log:

Net

In this example, we have set the start_position parameter to a value of -3 and we have not provided a length parameter. This means that the substr() method will extract the substring starting at the third last character until the end of the string (allowing us to extract the last 3 characters of the string).

Which of these method of class String is used to extract?

charAt() is the method of class String is used to extract a single character from a String Object. Hence, option(c) is the correct answer.

How does substring () method works?

This method is used to return a new String object that includes a substring of the given string with their indexes lying between startIndex and endIndex. If the second argument is given, the substring begins with the element at the startIndex to endIndex -1. endIndex: the ending index, exclusive.

Which one of the following method is used to make substring?

Java String substring() The Java String class substring() method returns a part of the string. We pass beginIndex and endIndex number position in the Java substring method where beginIndex is inclusive, and endIndex is exclusive.

Which of this method of class String is used to obtain a length of String object?

Which of this method of class String is used to obtain a length of String object? Explanation: Method length() of string class is used to get the length of the object which invoked method length().