2. String
• Class provided by the JDK.
• Represents character strings, not same as an
array of characters.
• Any string literal such as “abc” represents an
instance of the String class.
• Strings are constant (immutable), CANNOT be
modified after creation.
3. String API transformations
• Many methods in the String API to transform
the string.
• Gives the impression that Strings can be
modified.
• However, all the methods return a new String,
the original string is unchanged.
• String API methods can be chained.
4. Important Instance Methods from the
String API
• charAt, compareTo, concat ( + operator), contains
• format
• getChars
• indexOf, isEmpty
• lastIndexOf, Length
• matches
• replace, replaceAll, replaceFirst,
• split, startsWith, substring
• toCharArray, toLowerCase, toUpperCase, trim
5. Regular Expressions
• Used by the matches method of the String
class.
• Java Regular Expressions Tutorial
6. Chaining methods
• String orig = “ Hello, how are you? “;
• String modified =
orig.trim().toUpperCase().substring(3,8);
• Each of the above methods returns a new
instance of a String.
• The original String (orig) remains unchanged.
7. Static Methods of String API
• valueOf returns the string representation of
different primitive types, char array or any
Object.