Class 1, Part 2 |
Intro to Java 30-IT-396 |
|
More String Methods
-
All of these functions are for Strings. So, to use them, you need
to have a String object, let's call it myString, then a period, then a
function name.
-
Example: myString.length(); will return the length of the String
held in the myString object.
-
length() returns the length, in characters, of the String.
-
Strings are immutable, or can't be changed. If you need to change
the value of a String object, you must make a new String, possibly via
the substring() method, and assign it to back the String object.
In other words, there is no method to change an individual character of
a String object. Changing a String results in a whole new String.
-
The advantage? If object a = "Hello" and object b = "Hello", they
both actually point to the same area in memory.
-
If you are constantly changing a String, you can save time with the StringBuffer
object. This is optimized for String manipulation, and can easily
return a string via the toString() method.
-
toUpperCase() makes a String all upper case.
-
toLowerCase() makes a String all lower case.
-
replace(char newChar, char oldChar) returns a new String where all of the
oldChars are replaced with newChar.
Comparing Strings


Created by: Brandan
Jones January 4, 2002