Which method of String class is used to extract a particular character in the String?

String is one of the widely used java classes. The Java String class is used to represent the character strings. All String literals in Java programs are implemented as instance of the String Class. Strings are constants and their values cannot be changed after they created. Java String objects are immutable and they can be shared. The String Class includes several methods to edit the content of string.

Creating a String Object:

A String object can be created as

String Str=”Car Insurance”;

The string object can also be created using the new operator as

String str= new String(“Car Insurance”);

Java provides a special character plus (+) to concatenate strings. The plus operator is the only operator which is overloaded in java. String concatenation is implemented through the String Builder of String Buffer class and their append method.

Examples of Java String Class:

1. Finding the length of the string

The length() method can be used to find the length of the string.

String str = "Car insurance"
System.out.println(str.length());

2. Comparing strings

The equals() method is used to comapre two strings.

String str = "car finance";

if ( str.equals("car loan") )
{
System.out.println("Strings are equal");
}
else
{
System.out.println("Strings are not equal");
}

3. Comparing strings by ignoring case

The equalsIgnoreCase() method is used to compare two strings by ignoring the case.

String str = "insurance";

if ( str.equalsIgnoreCase("INSURANCE") )
{
System.out.println("insurance strings are equal");
}
else
{
System.out.println("insurance Strings are not equal");
}

4. Finding which string is greater

The CompareTo() method compares two strings to find which string is alphabetically greater.

String str = "car finance";

if ( str.CompareTo("auto finance") > 0)
{
System.out.println("car finance string is alphabetically greater");
}
else
{
System.out.println("car finance string is alphabetically lesser");
}

5. Finding which string is greater while ignoring the case.

The compareToIgnoreCase() method is same as the CompareTo() method except that it ignores case while comparing.

String str = "car finance";

if ( str.compareToIgnoreCase("CAR finance") = 0)
{
System.out.println("strings are alphabetically same");
}
else
{
System.out.println("strings are alphabetically not same");
}

6. Position of a string in another string.

The indexOf() method is used to find the position of a string in another string.

String str= "car insurance";
System.out.println( str.indexOf("car") );

7. Extract single character from a string.

The CharAt() method is used to extract a single character by specifying the position of the character.

String str = "Auto Finance";
System.out.println( str.charAt(5) );

8. Extracting part of a string.

The substring() is used to method part of a string by specifying the start position and end position.

String str = "Car Finance";
System.out.println( str.substring(1,3));

9. Hash code of a string.

The hashCode() method is used to get the hash code of a string.

String str = "Auto Insurance";
System.out.println(str.hashCode());

10. replacing characters in a string.

The replace() method is used to replace a character in a string with new character.

string str = "Auto Loan";
System.out.println(str.replace("A", "L"));

11. Converting a string to upper case.

The toUpperCase() method is used to convert a string to upper case letters.

String str = "Cheap Car Insurance";
System.out.println(str.toUpperCase());

12. Converting a string to lower case.

The toLowerCase() method is used to covert a string to lower case letters.

String str = "Insurance Quote";
System.out.println(str.toLowerCase());

Recommended Reading:


Java HashMap Class Example

If you like this article, then please share it or click on the google +1 button.

About Us

McqMate.com is an educational platform, Which is developed BY STUDENTS, FOR STUDENTS, The only objective of our platform is to assist fellow students in preparing for exams and in their Studies throughout their Academic career.

what we offer ?

» We provide you study material i.e. PDF's for offline use.
» We take free online Practice/Mock test for exam preparation.
» Each MCQ is open for further discussion on discussion page.
» All the services offered by McqMate are free.

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. In other words, the beginIndex starts from 0, whereas the endIndex starts from 1.

There are two types of substring methods in Java string.

Signature

If we don't specify endIndex, the method will return all the characters from startIndex.

Parameters

startIndex : starting index is inclusive

endIndex : ending index is exclusive

Returns

specified string

Exception Throws

StringIndexOutOfBoundsException is thrown when any one of the following conditions is met.

  • if the start index is negative value
  • end index is lower than starting index.
  • Either starting or ending index is greater than the total number of characters present in the string.

Internal implementation substring(int beginIndex)

Internal implementation substring(int beginIndex, int endIndex)

Java String substring() method example

FileName: SubstringExample.java

Test it Now

Output:

Java String substring() Method Example 2

FileName: SubstringExample2.java

Output:

Javatpoint
point
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 5, end 15, length 10

Applications of substring() Method

1) The substring() method can be used to do some prefix or suffix extraction. For example, we can have a list of names, and it is required to filter out names with surname as "singh". The following program shows the same.

FileName: SubstringExample3.java

Output:

Yuvraj Singh
Harbhajan Singh
Gurjit Singh
Sandeep Singh
Milkha Singh

2) The substring() method can also be used to check whether a string is a palindrome or not.

FileName: SubstringExample4.java

Output:

madam is a palindrome.
rock is not a palindrome.
eye is a palindrome.
noon is a palindrome.
kill is not a palindrome.


Which methods are used to extract the characters from string?

The String class provides accessor methods that return the position within the string of a specific character or substring: indexOf() and lastIndexOf() . The indexOf() methods search forward from the beginning of the string, and the lastIndexOf() methods search backward from the end of the string.

Which method is used to extract a single character from a string in Java?

charAt() method: Get the string and the index. Get the specific character using String. charAt(index) method.

Which of the following method extracts a part of a string from a string object?

You can extract a substring from a string using the slice() method. You pass it: the index at which to start extracting.

Which method is used to extract a single character at an index?

Use the charAt method to get a character at a particular index.