Download presentation
Presentation is loading. Please wait.
Published byElinor Carr Modified over 9 years ago
1
BPJ444: Business Programming Using Java – String Handling Tim McKenna Seneca@York
2
The String Class u Strings are "immutable" objects: once instantiated, a String object is constant and not changeable. u because String objects are immutable, they can be shared fearlessly: no one can change your object. u Java optimizes memory by maintaining a pool of shared Strings: the constants "A", "A", "A" will have three references to the same String object. u Note: an empty string object is not null u literal: "" is a String object with a length() of 0 u Example: StringDemo.java
3
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");
4
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() );
5
The String Class u comparison of two String objects: u thisString.equals(thatString)compares contents u this is what most of us mean most of the time u thisString == thatStringcompares obj.ref. u this may seem like it works but is unreliable u when is thisString really thatString when is thisString really thatString u an array of characters vs a String object u Example: StringDemo2.java
6
String Class Methods u length()returns int of character count u trim() returns String exclusive of lead/trail blanks u toUpperCase(), to LowerCase() returns consistent case u valueOf()returns String of any primitive u indexOf()returns int locating a char or substring u charAt()allows processing of string like char[] u substring()returns a substring from this string u replace()changes characters u replaceAll() changes strings with regular expressions u split()splits a string into an array of strings using reg.exp. u Examples:StringDemo3.java, StringDemoGUI.java
7
The StringBuffer/Builder Class u mutable string objects u much better performance for building up a String u useful methods for string manipulation u append() – add a string or char u insert() setCharAt() – change a string u setLength() – truncates or pads u Example: StringDemo4.java
8
StringTokenizer Class u parsing: extract words (i.e. tokens) from a string u 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
9
StringTokenizer alternatives u API recommends using String.split("\\s+") u split uses a regular expression to identify delimiters u can be powerful but reg. exp. are tricky and require extensive, thorough, and exhaustive testing u split takes 3-4 times longer than StringTokenizer u Scanner class is a new alternative in J2SE 5.0 u it takes 15-30+ times longer than StringTokenizer u has other functions to parse primitives and BigDecimals u Example: ParseString.java
10
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();
11
The Character Class u some useful methods: u isDigit () u isLetter () isLetterOrDigit( ) u isLowerCase () toLowerCase () u isUpperCase () toUpperCase () u isWhitespace ()
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.