operations on strings. 8/8/2005 copyright 2006, by the authors of these slides, and ateneo de manila...
TRANSCRIPT
Operations on Strings
8/8/2005Copyright 2006, by the authors of these slides, and Ateneo
de Manila University. All rights reserved
L: String Manipulation
Slide 2
Strings In Java, strings are objects and String is a
“built-in” class with special treatment String s = new String( “Hello” ); is the same as
String s = “Hello”; “Hello” is an example of a String literal representing
a String object The + operator can be used for String concatenation
The String class has certain methods length() substring(…) equals(…) equalsIgnoreCase(…) others
8/8/2005Copyright 2006, by the authors of these slides, and Ateneo
de Manila University. All rights reserved
L: String Manipulation
Slide 3
String Assignment Since String is a class (just like
BankAccount), a String variable stores a reference to a String object
String object: sequence of characters delimited by quotes; e.g., “Hello” Can be viewed as equivalent to new
String( “Hello” ) Assignment
Points the String reference (left-hand side) to the String instance (right-hand side)
Similar to object declaration and assignment Uses the “=” operator
String s = “Hello”;
8/8/2005Copyright 2006, by the authors of these slides, and Ateneo
de Manila University. All rights reserved
L: String Manipulation
Slide 4
String Concatenation
Strings have special treatment in Java in that the + operator works on string objects
Concatenation Appends a variable or object with a String Returns the linked String as the result Uses the “+” operatorString a = s + “ World!”; // a = “Hello World!”String b = s + 5; // b = “Hello5”
“+=” can also be used on Stringsb += “ World!”; // b = “Hello5 World!”
8/8/2005Copyright 2006, by the authors of these slides, and Ateneo
de Manila University. All rights reserved
L: String Manipulation
Slide 5
String Methods
The length() method returns an int representing the number of characters in the string String s = “Hello there”;
System.out.println( s.length() ); // prints 11 The substring() method requires two ints as
parameters and returns a part of the string bounded by those integers String s = “Hello there”;
String t = s.substring(2,7); // assigns “llo t” Other string methods for equality and
comparison
8/8/2005Copyright 2006, by the authors of these slides, and Ateneo
de Manila University. All rights reserved
L: String Manipulation
Slide 6
String Equality
Equality Requires a String parameter and returns a boolean Value equality: equals(), equalsIgnoreCase()
method Checks if the value of two Strings are equalString a = “myString”;
String b =“this is myString again”.substring(8,16);
boolean ansA = a.equals( b ); // true
boolean ansB = a == b; // false Pointer equality: “==”
Checks if the two Strings are pointing to the same object Not used to check if Strings contain the same value
8/8/2005Copyright 2006, by the authors of these slides, and Ateneo
de Manila University. All rights reserved
L: String Manipulation
Slide 7
String Comparison Natural comparison of Strings
A more flexible comparison (not just checking for equality)
Used by calling the compareTo() method Returns a number
ExampleString a = “application”;int result = a.compareTo( “applet” );
Returns a negative number when a is less than b Returns 0 when a is equal to b Returns a positive number when a is greater than b
What if we change the first line to String a = “Application”?
8/8/2005Copyright 2006, by the authors of these slides, and Ateneo
de Manila University. All rights reserved
L: String Manipulation
Slide 8
String Formatting
There are string methods that can help with Text alignment Formatting of numeric values
Java provides the a String formatting method String.format() The desired effect is also available
through System.out.printf()
8/8/2005Copyright 2006, by the authors of these slides, and Ateneo
de Manila University. All rights reserved
L: String Manipulation
Slide 9
String Formatting
Methods which produce formatted outputs requires a format string and an argument list Example
double pi = 3.14159265;
String s = String.format( “Pi: %.2f”, pi );
// s = “Pi: 3.14”
Format string Contains fixed text and format specifiers
that process the arguments
8/8/2005Copyright 2006, by the authors of these slides, and Ateneo
de Manila University. All rights reserved
L: String Manipulation
Slide 10
Format Specifiers
Indicates how the arguments should be processed and where they should be inserted
Syntax % - start of the specifier arg_index + '$' (optional)
position of argument in the argument list flags (optional)
provides special formatting (e.g. left align, padding with zeroes)
8/8/2005Copyright 2006, by the authors of these slides, and Ateneo
de Manila University. All rights reserved
L: String Manipulation
Slide 11
Format Specifiers
Syntax (continued) width (optional)
minimum number of spaces to print argument puts blanks in unused spaces
'.' + precision (optional) number of digits after decimal
Conversion Formatted into:
d integers
f floating-point number
s String
c character
conversion – how the argument should be formatted
8/8/2005Copyright 2006, by the authors of these slides, and Ateneo
de Manila University. All rights reserved
L: String Manipulation
Slide 12
Example
Print a price list
String header = String.format("%s branch sales for %s“, "Katipunan“, “July” );
int num = 1;double price1 = 3.75;double price2 = 100;...double price10 = 12.5;
System.out.println( header );System.out.printf( "%s %s" "Item #", "Price" );System.out.printf( "%-6d P%5.2f \n", num++, price1 );System.out.printf( "%-6d P%5.2f \n", num++, price2 );...System.out.printf( "%-6d P%5.2f \n", num++, price10 );
Output:Katipunan branch sales for JulyItem # Price1 P 3.752 P 100.00... ...10 P 12.50
8/8/2005Copyright 2006, by the authors of these slides, and Ateneo
de Manila University. All rights reserved
L: String Manipulation
Slide 13
Summary
String is a class; there are String objects with which String methods() can be carried out
Strings also have a special treatment in Java String literals; e.g., “hello” The concatenation operators ( +, += )