250 likes | 356 Vues
Learn about Java String class, its constructors and methods for manipulating strings. Understand constructing immutable and interned strings, comparisons using methods like equals and compareTo, string length & operations. Examples on substrings, conversions, replacements, and splitting strings. Explore character matching, conversion between strings & arrays, and formatting strings.
E N D
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.
String Comparisons(Cont.) Using equals Function:
String Comparisons(Cont.) Using compare To Function:
String Length, Characters, and Combining Strings • The String class provides the methods for obtaining length, retrieving individual characters, and concatenating strings.
Obtaining Substrings • You can also obtain a substring from a string using the substring method in the String class.
Converting, Replacing, and Splitting Strings • The String class provides the methods for converting, replacing, and splitting strings, as shown in Figure.
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.
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.