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. String str = "insurance"; 4. Finding which string is greater The CompareTo() method compares two strings to find which string is alphabetically greater. String str = "car finance"; 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"; 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"; 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"; 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"; 9. Hash code of a string. The hashCode() method is used to get the hash code of a string. String str = "Auto Insurance"; 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"; 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"; 12. Converting a string to lower case. The toLowerCase() method is used to covert a string to lower case letters. String str = "Insurance Quote"; 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. 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. SignatureIf we don't specify endIndex, the method will return all the characters from startIndex. ParametersstartIndex : starting index is inclusive endIndex : ending index is exclusive Returnsspecified string Exception ThrowsStringIndexOutOfBoundsException is thrown when any one of the following conditions is met.
Internal implementation substring(int beginIndex)Internal implementation substring(int beginIndex, int endIndex)Java String substring() method exampleFileName: SubstringExample.java Test it NowOutput: Java String substring() Method Example 2FileName: SubstringExample2.java Output: Javatpoint point Exception in thread "main" java.lang.StringIndexOutOfBoundsException: begin 5, end 15, length 10 Applications of substring() Method1) 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.
|