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. 2. Comparing strings The equals() method is used to comapre two strings. if ( str.equals("car loan") ) 3. Comparing strings by ignoring case The equalsIgnoreCase() method is used to compare two strings by ignoring the case. if ( str.equalsIgnoreCase("INSURANCE") )
System.out.println(str.length());
{
System.out.println("Strings are equal");
}
else
{
System.out.println("Strings are not equal");
}
{
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 NowOutput:
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.