StringHandling

Download Report

Transcript StringHandling

Java – String Handling
The String Class
• Strings are "immutable" objects: once instantiated, a
String object is constant and not changeable.
• because String objects are immutable, they can be
shared fearlessly: no one can change your object.
• Java optimizes memory by maintaining a pool of
shared Strings: the constants "A", "A", "A" will have
three references to the same String object.
• Note: an empty string object is not null
– literal: "" is a String object with a length() of 0
• Example: StringDemo.java
Strings are special in Java
• Strings are used so often in programming, Java makes special
allowances for coding them
• construct a String without new String()
String s1 = new String("some text");
String s2 = "more text";
• the only overloaded operators in Java are
for Strings
• s1 += s2; // s1 = s1.concat(s2);
• s1 = s2 + "etc"; // s1 = s2.concat("etc");
anything can be a String, just ask
• static method String.valueOf()
– returns a String
– can take almost anything as a parameter: all
primitives, char array, any object.
• all objects inherit or override the Object class
toString() method
• System.out.println() automatically calls
toString() on any object in the parameter list
– System.out.println(myObject); // is same as
– System.out.println(myObject.toString() );
The String Class
• comparison of two String objects:
– thisString.equals(thatString)
contents
compares
• this is what most of us mean most of the time
– thisString == thatString
compares obj.ref.
• this may seem like it works but is unreliable
• when is thisString really thatString
• an array of characters vs a String object
• Example: StringDemo2.java
String Class Methods
•
•
•
•
•
•
•
•
•
•
length()
returns int of character count
trim()
returns String exclusive of lead/trail blanks
toUpperCase(), to LowerCase() returns consistent case
valueOf()
returns String of any primitive
indexOf()
returns int locating a char or substring
charAt()
allows processing of string like char[]
substring() returns a substring from this string
replace()
changes characters
replaceAll() changes strings with regular expressions
split()
splits a string into an array of strings using reg.exp.
• Examples:
StringDemo3.java,
StringDemoGUI.java
The StringBuffer/Builder Class
• mutable string objects
• much better performance for building up a
String
• useful methods for string manipulation
• append() – add a string or char
• insert() setCharAt() – change a string
• setLength() – truncates or pads
• Example: StringDemo4.java
StringTokenizer Class
• parsing: extract words (i.e. tokens) from a
string
• StringTokenizer st;
st = new StringTokenizer("this is a
test");
// default delimiter is whitespace
while (st.hasMoreTokens()) {
System.out.println( st.nextToken()); }
this
is
a
test
StringTokenizer alternatives
• API recommends using String.split("\\s+")
– split uses a regular expression to identify
delimiters
– can be powerful but reg. exp. are tricky and
require extensive, thorough, and exhaustive
testing
– split takes 3-4 times longer than StringTokenizer
• Scanner class is a new alternative in J2SE 5.0
– it takes 15-30+ times longer than StringTokenizer
– has other functions to parse primitives and
BigDecimals
• Example: ParseString.java
StringTokenizer and StringBuilder
• use these two classes to remove embedded blanks
from a string.
• String s; StringTokenizer st;
StringBuilder sb;
s=" My Spacebar
Is
Sticky "
st=new StringTokenizer(s);
sb=new StringBuilder( s.length());
while ( st.hasMoreTokens() ) {
sb.append( st.nextToken() ); }
s=sb.toString();
The Character Class
some useful methods:
isDigit ()
isLetter ()
isLetterOrDigit()
isLowerCase ()
toLowerCase ()
isUpperCase ()
toUpperCase ()
isWhitespace ()