Unit-2 Objects and Classes String Class
String Class A string is a sequence of characters. In many languages, strings are treated as an array of characters, but in Java a string is an object. The String class has 11 constructors and more than 40 methods for manipulating strings.
Constructing a String
Immutable Strings and Interned Strings
String Comparisons
String Comparisons(Cont.) Using equals Function:
String Comparisons(Cont.) Using compare To Function:
Program(Equal to & Compare TO)
Program(regionMatches() )
Program (endWith)
Program (startWith)
String Length, Characters, and Combining Strings The String class provides the methods for obtaining length, retrieving individual characters, and concatenating strings.
Program
Obtaining Substrings You can also obtain a substring from a string using the substring method in the String class.
Example
Converting, Replacing, and Splitting Strings The String class provides the methods for converting, replacing, and splitting strings, as shown in Figure.
Program
Example(Split () )
String Matching
Finding a Character or a Substring in a String The String class provides several overloaded indexOf and lastIndexOf methods to find a character or a substring in a string.
Program
Conversion between Strings and Arrays
Formatting Strings
Converting Characters and Numeric Values to Strings There are several overloaded versions of the valueOf method that can be used to convert a character and numeric values to strings with different parameter types, char, double, long, int, and float.