Unit-2 Objects and Classes

Click here to load reader

download Unit-2 Objects and Classes

of 24

description

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. - PowerPoint PPT Presentation

Transcript of Unit-2 Objects and Classes

Unit-2 Objects and Classes

String ClassUnit-2 Objects and ClassesString 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 StringsThe String class provides the methods for obtaining length, retrieving individual characters, and concatenating strings.

Program

Obtaining SubstringsYou can also obtain a substring from a string using the substring method in the String class.

Example

Converting, Replacing, and Splitting StringsThe 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 StringThe 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 StringsThere 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.