Java String Class

Review the String class. Declare, concatenate and manipulate strings.

String. Deep in the cave you encounter a pool. You discover a message written upon the water's surface. It is a string with letters and words.

String class methods. To use our mysterious string, we will need methods. Here are many of the methods on strings. We call these methods on a String object.charAtcompareTocompareToIgnoreCaseconcatcontainscontentEqualscopyValueOfendsWithequalsequalsIgnoreCaseformatgetBytesindexOfisEmptyjoinlastIndexOflengthmatchesregionMatchesreplacereplaceAllreplaceFirstsplitstartsWithsubSequencesubstringtoCharArraytoLowerCasetoUpperCasetrimvalueOf

Truncate. This method uses substring() internally. We can get strings relative to other strings—between, before and after other strings.Substring: TruncateSubstring: Between, Before and After

Arrays. The internal data, the characters, of a String can be thought of as an array. And with built-in methods, we can convert Strings into, and from, different types of arrays.Byte Array, StringChar Array, String

Whitespace. Strings often contain whitespace characters, and the trim() method does not handle all problems with whitespace. We convert UNIX and Windows newlines.Whitespace: methodsWhitespace: newlines, lineSeparator

Uppercase first letter. With toUpperCase we change all letters in a String. But often we want to modify just the first letter, or the first letter of individual words.Uppercase First Letter

StringTokenizer. Java is full of old things we should not use. StringTokenizer is one example. Its functionality is easily replaced with split() but it exists for backwards compatibility.StringTokenizer

Reverse. Strings cannot be easily manipulated in their initial form. Instead we transform them to a char array. In this way we can even reverse them.Reverse String

A final note. A String can represent any data. But textual representation is rarely the most efficient form. Directly storing numbers as ints is faster.
Dot Net Perls
© 2007-2020 Sam Allen. Every person is special and unique. Send bug reports to