Professional Documents
Culture Documents
Chapter 4 Strings
Chapter 4 Strings
El Dick - I2211 1
Class String
2
STRINGS
Chapter 4 M. El Dick - I2211
Once created, a string cannot be changed: Advantage: more efficient, no need to copy
No methods changes the string
String s1 = "Sun"; String s1 = "Sun";
String s2 = s1; String s2 = new String(s1);
Such objects are called immutable
s1 s1 "Sun"
"Sun"
s2 s2 "Sun"
No-args and copy constructors are not used much int length ();
returns the number of characters in the string
result += s3;
Returns:
concatenates s3 to result
”strawberry".substring (2,5); "raw"
"unhappy".substring (2); "happy"
"emptiness".substring (9); "" (empty string) result += num;
converts num to String and concatenates it to result
0 8 11 15 boolean b = s1.equals(s2);
returns true if the string s1 is equal to s2
String date ="July 5, 2012 1:28:19 PM";
Returns:
boolean b = s1.equalsIgnoreCase(s2);
date.indexOf ('J'); 0
returns true if the string s1 matches s2, case-blind
date.indexOf ('2'); 8
date.indexOf ("2012"); 8 int diff = s1.compareTo(s2);
date.indexOf ('2', 9); 11 Starts searching at
position 9 returns the “difference” s1 - s2
3 ways to convert a number into a string: The DecimalFormat class can be used for formatting
String s = "" + num; numbers into strings
import java.text.DecimalFormat;
...
String s = Integer.toString (i); DecimalFormat money = new DecimalFormat("0.00");
String s = Double.toString (d); ...
double amt = 56.7381;
...
String s = money.format (amt);
String s = String.valueOf (num); 56.7381
"56.74"
Java 5.0 added printf and format methods: if s not a valid number (integer, real number), ERROR
int m = 5, d = 19, y = 2007;
double amt = 123.5;
String s1 = "-123", s2 = "123.45";
System.out.printf (
int n = Integer.parseInt(s1);
"Date: %02d/%02d/%d Amount = %7.2f\n", m,
d, y, amt); double x = Double.parseDouble(s2);
Exercise 1
24
23 Exercises Hexadecimal digits : digits '0' through '9' + letters 'A' through 'F'
Write a function hexValue that uses a switch statement to find the
hexadecimal value of a given character
Character is a parameter to the function
Hexadecimal value of character is the return value of the function
If parameter is not one of the legal hexadecimal digits, return -1
Exercise 2
character.
for ( i = 0; i < hex.length(); i++ )
25 if ( hexValue(hex.charAt(i)) == -1 ) { // Character number i is bad.
valid = false ;
Hexadecimal integer = sequence of hexadecimal digits
break; // Leave the for loop, since we are now sure of the answer.
If str is a string containing a hexadecimal integer, then its }
decimal integer is computed : if ( valid ) { // If the input is valid, compute and print base-10 value
value = 0; dec = 0 ;
for ( i = 0; i < str.length(); i++ ) for ( i = 0 ; i < hex.length() ; i++ )
dec = 16*dec + hexValue( hex.charAt(i) );
value = value*16 + hexValue( str.charAt(i) );
System.out.println("Base-10 value is: " + dec);
}
Write a program that reads a string from the user. else { // Input is not valid, print an error message
If all the characters in the string are hexadecimal digits, System.out.println("Error: Input is not a hexadecimal number.");
print out the corresponding decimal value. }
dec = 0;
for ( i = 0; i < hex.length(); i++) {
Exercise 3
28
Write a program that splits then prints the 3 components of Write a function that generates a password for a
the following course description: person using its initials and age given as parameters
INFO 311 : Java Programming
as shown below:
Course Name: INFO Use this function in a main method and print the result to
Course Number: 311 the screen
Course Description: Java Programming
Write a program that can exchange the last Write a class that contains :
names of 2 persons a static method findMin(String[] set) that returns the
minimum of a set of positive integers given as an
argument
a method main() defined as follows :
public static void main(String[] arg){
String[] t = {“16”, “2”, “36”, “-64”};
System.out.println(“The minimum is : “ +findMin(t));
}