KEMBAR78
Java String class | PPSX
String Class :
 String represent a sequence of character.
 The String class belongs to the java.lang package, which
does not require an import statement.
 Sequence of character is represented in java is by using
a character array
char charArray[ ] = new char[4]
charArray[0]= ‘J’;
charArray[1]= ‘a’;
charArray[2]= ‘v’;
charArray[3]= ‘a’;
1 RAJESHREE KHANDE
String Class
X[0]
X[1]
X[2]
X[2][2]
X[1][3]
X[0][1]
Variable Size Array
2RAJESHREE KHANDE
String Class
 Character array not good enough to support the range
of operations we may like to perform on the string.
 In java String are class object & implemented using
two classes namely Sting & StringBuffer.
 A java string is an instantiated object of String Class.
 Java string is not character array & is not NULL
terminated.
 Strings are Immutable.
3 RAJESHREE KHANDE
String Class
String stringName = new String(“string”);
e.g.
String name = new String(“Rajeshree”);
To find the length of String length method of
string class is used.
int m = name.length();
4 RAJESHREE KHANDE
String Array
 We can also create & use array that contain
strings.
String itemArray[] = new String[3];
We can assign the element to this array by using
for loop.
5 RAJESHREE KHANDE
Literal Strings
 String literals are anonymous objects of the String class.
 are defined by enclosing text in double quotes. “This is a
literal String”
 don’t have to be constructed.
 can be assigned to String variables.
 can be passed to methods and constructors as
parameters.
6 RAJESHREE KHANDE
Literal String examples
String name = “Robert”;
//calling a method on a literal String
char firstInitial=“Robert”.charAt(0);
//calling a method on a String variable
char firstInitial = name.charAt(0);
7 RAJESHREE KHANDE
Empty Strings
 An empty String has no characters. It’s
length is 0.
 Not the same as an uninitialized String.
String word1 = “ ”;
String word2 = new String();
private String errorMsg; errorMsg
is null
Empty strings
8 RAJESHREE KHANDE
String Methods : Changing Case
 toLowerCase : Convert String to lowercase
 toUpperCase : Convert String to uppercase
String word2 = word1.toUpperCase();
String word3 = word1.toLowerCase();
 returns a new string formed from word1 by converting its
characters to upper (lower) case
String word1 = “HeLLo”;
String word2 = word1.toUpperCase(); //”HELLO”
String word3 = word1.toLowerCase(); //”hello”
//word1 is still “HeLLo“
9 RAJESHREE KHANDE
String Methods : Changing Case
word1
remains
unchanged
 Example: to “convert” word1 to upper case, replace
the reference with a new reference.
 A common bug:
word1 = word1.toUpperCase();
word1.toUpperCase();
10 RAJESHREE KHANDE
String Methods : length, chatAt
 ”Problem".length();
 ”Window".charAt (2);
int length();
char charAt(i);
 Returns the number of characters in
the string
 Returns the char at position i.
7
’n'
Returns:
Character positions in strings are numbered
starting from 0 – just like arrays.
11 RAJESHREE KHANDE
String Methods : substring
television
i k
“lev"
“mutable"
"" (empty string)
”television".substring (2,5);
“immutable".substring (2);
“bob".substring (9);
television
i
 String subs = word.substring (i, k);
 returns the substring of chars in positions
from i to k-1
 String subs = word.substring (i);
 returns the substring from the i-th char to
the end
Returns a new String by copying characters from an existing String.
12 RAJESHREE KHANDE
String Methods : concat
String word1 = “re”, word2 = “think”; word3 = “ing”;
int num = 2;
String result = word1 + word2;
//concatenates word1 and word2 “rethink“
String result = word1.concat (word2);
//the same as word1 + word2 “rethink“
result += word3;
//concatenates word3 to result “rethinking”
result += num; //converts num to String
//and concatenates it to result “rethinking2”
13 RAJESHREE KHANDE
String Methods : indexOf
String name =“President George Washington";
name.indexOf (‘P'); 0
name.indexOf (‘e'); 2
name.indexOf (“George"); 10
name.indexOf (‘e', 3); 6
name.indexOf (“Bob"); -1
name.lastIndexOf (‘e'); 15
0 2 6 10 15
(starts searching
at position 3)
(not found)
Returns:
14 RAJESHREE KHANDE
String Methods : Equality
boolean b = word1.equals(word2);
 returns true if the string word1 is equal to word2
boolean b = word1.equalsIgnoreCase(word2);
 returns true if the string word1 matches word2,
case
b = “Raiders”.equals(“Raiders”);//true
b = “Raiders”.equals(“raiders”);//false
b = “Raiders”.equalsIgnoreCase(“raiders”);//true
if(“team”.equalsIgnoreCase(“raiders”))
System.out.println(“Go You ” + “team”);
15 RAJESHREE KHANDE
String Methods : Comparisons
S1.compareTo(S2) : Return negative if S1<S2, positive if S1>S2,
zero if s1 equal to S2
int diff = word1.compareTo(word2);
int diff = word1.compareToIgnoreCase(word2);
Usually programmers don’t care what the numerical
“difference” of word1 - word2 is, just whether the difference
is negative (word1 comes before word2), zero (word1 and
word2 are equal) or positive (word1 comes after word2).
Often used in conditional statements.
if(word1.compareTo(word2) > 0){
//word1 comes after word2…
}
16 RAJESHREE KHANDE
Comparisons :Eaxmples
//negative differences
diff = “apple”.compareTo(“berry”); //a before b
diff = “Zebra”.compareTo(“apple”); //Z before a
diff = “dig”.compareTo(“dug”); //i before u
diff = “dig”.compareTo(“digs”); //dig is shorter
//zero differences
diff = “apple”.compareTo(“apple”); //equal
diff = “dig”.compareToIgnoreCase(“DIG”);/ /equal
//positive differences
diff = “berry”.compareTo(“apple”); //b after a
diff = “apple”.compareTo(“Apple”); //a after A
diff = “BIT”.compareTo(“BIG”); //T after G
diff = “huge”.compareTo(“hug”); //huge is longer
17 RAJESHREE KHANDE
String Methods : trim
s2 = s1.trim();
Removes white spaces at the beginning & at the end of string s1
 String word2 = word1.trim ();
 returns a new string formed from word1 by removing white
space at both end does not affect whites space in the middle
String word1 = “ Hi Bob ”;
String word2 = word1.trim();
//word2 is “Hi Bob” – no spaces on either end
//word1 is still “ Hi Bob “ – with spaces
18 RAJESHREE KHANDE
String Methods : replace
 String word2 = word1.replace(‘x’, ‘y’);
returns a new string formed from word1 by replacing
all occurrences of x with y
String word1 = “rare“;
String word2 = “rare“.replace(‘r’, ‘d’);
//word2 is “dade”, but word1 is still “rare“
19 RAJESHREE KHANDE
Numbers to Strings
Three ways to convert a number into a string:
1. String s = "" + num;
2. String s = Integer.toString (i);
String s = Double.toString (d);
3. String s = String.valueOf (num);
Integer and Double
are “wrapper” classes
from java.lang that
represent numbers as
objects. They also
provide useful static
methods.
s = String.valueOf(123) ; //”123”
s = “” + 123;//”123”
s = Integer.toString(123);
//”123”
s = Double.toString(3.14);
//”3.14”
20 RAJESHREE KHANDE
StringBuffer class
 StringBuffer is mutable object.
 The StringBuffer class is an alternative to the String
class.
 StringBuffer is more flexible than String. You can add,
insert, or append new contents into a string buffer.
21 RAJESHREE KHANDE
StringBuffer : Methods
Method Description
s1.setCharAt(n, x) Modifies the nth char to x of string s1
s1.append(s2) Append the string s2 to s1 at the end
s1.insert(n,s2) Insert the string s2 at the position n of string s1
s1.setLength(n) Set the length of the string s1 to n
If n < s1,length() s1 is truncated.
If n > s1.length zeros are added to s1
s1.deleteCharAt(n) Delete nth character from string s1
22RAJESHREE KHANDE

Java String class

  • 1.
    String Class : String represent a sequence of character.  The String class belongs to the java.lang package, which does not require an import statement.  Sequence of character is represented in java is by using a character array char charArray[ ] = new char[4] charArray[0]= ‘J’; charArray[1]= ‘a’; charArray[2]= ‘v’; charArray[3]= ‘a’; 1 RAJESHREE KHANDE
  • 2.
  • 3.
    String Class  Characterarray not good enough to support the range of operations we may like to perform on the string.  In java String are class object & implemented using two classes namely Sting & StringBuffer.  A java string is an instantiated object of String Class.  Java string is not character array & is not NULL terminated.  Strings are Immutable. 3 RAJESHREE KHANDE
  • 4.
    String Class String stringName= new String(“string”); e.g. String name = new String(“Rajeshree”); To find the length of String length method of string class is used. int m = name.length(); 4 RAJESHREE KHANDE
  • 5.
    String Array  Wecan also create & use array that contain strings. String itemArray[] = new String[3]; We can assign the element to this array by using for loop. 5 RAJESHREE KHANDE
  • 6.
    Literal Strings  Stringliterals are anonymous objects of the String class.  are defined by enclosing text in double quotes. “This is a literal String”  don’t have to be constructed.  can be assigned to String variables.  can be passed to methods and constructors as parameters. 6 RAJESHREE KHANDE
  • 7.
    Literal String examples Stringname = “Robert”; //calling a method on a literal String char firstInitial=“Robert”.charAt(0); //calling a method on a String variable char firstInitial = name.charAt(0); 7 RAJESHREE KHANDE
  • 8.
    Empty Strings  Anempty String has no characters. It’s length is 0.  Not the same as an uninitialized String. String word1 = “ ”; String word2 = new String(); private String errorMsg; errorMsg is null Empty strings 8 RAJESHREE KHANDE
  • 9.
    String Methods :Changing Case  toLowerCase : Convert String to lowercase  toUpperCase : Convert String to uppercase String word2 = word1.toUpperCase(); String word3 = word1.toLowerCase();  returns a new string formed from word1 by converting its characters to upper (lower) case String word1 = “HeLLo”; String word2 = word1.toUpperCase(); //”HELLO” String word3 = word1.toLowerCase(); //”hello” //word1 is still “HeLLo“ 9 RAJESHREE KHANDE
  • 10.
    String Methods :Changing Case word1 remains unchanged  Example: to “convert” word1 to upper case, replace the reference with a new reference.  A common bug: word1 = word1.toUpperCase(); word1.toUpperCase(); 10 RAJESHREE KHANDE
  • 11.
    String Methods :length, chatAt  ”Problem".length();  ”Window".charAt (2); int length(); char charAt(i);  Returns the number of characters in the string  Returns the char at position i. 7 ’n' Returns: Character positions in strings are numbered starting from 0 – just like arrays. 11 RAJESHREE KHANDE
  • 12.
    String Methods :substring television i k “lev" “mutable" "" (empty string) ”television".substring (2,5); “immutable".substring (2); “bob".substring (9); television i  String subs = word.substring (i, k);  returns the substring of chars in positions from i to k-1  String subs = word.substring (i);  returns the substring from the i-th char to the end Returns a new String by copying characters from an existing String. 12 RAJESHREE KHANDE
  • 13.
    String Methods :concat String word1 = “re”, word2 = “think”; word3 = “ing”; int num = 2; String result = word1 + word2; //concatenates word1 and word2 “rethink“ String result = word1.concat (word2); //the same as word1 + word2 “rethink“ result += word3; //concatenates word3 to result “rethinking” result += num; //converts num to String //and concatenates it to result “rethinking2” 13 RAJESHREE KHANDE
  • 14.
    String Methods :indexOf String name =“President George Washington"; name.indexOf (‘P'); 0 name.indexOf (‘e'); 2 name.indexOf (“George"); 10 name.indexOf (‘e', 3); 6 name.indexOf (“Bob"); -1 name.lastIndexOf (‘e'); 15 0 2 6 10 15 (starts searching at position 3) (not found) Returns: 14 RAJESHREE KHANDE
  • 15.
    String Methods :Equality boolean b = word1.equals(word2);  returns true if the string word1 is equal to word2 boolean b = word1.equalsIgnoreCase(word2);  returns true if the string word1 matches word2, case b = “Raiders”.equals(“Raiders”);//true b = “Raiders”.equals(“raiders”);//false b = “Raiders”.equalsIgnoreCase(“raiders”);//true if(“team”.equalsIgnoreCase(“raiders”)) System.out.println(“Go You ” + “team”); 15 RAJESHREE KHANDE
  • 16.
    String Methods :Comparisons S1.compareTo(S2) : Return negative if S1<S2, positive if S1>S2, zero if s1 equal to S2 int diff = word1.compareTo(word2); int diff = word1.compareToIgnoreCase(word2); Usually programmers don’t care what the numerical “difference” of word1 - word2 is, just whether the difference is negative (word1 comes before word2), zero (word1 and word2 are equal) or positive (word1 comes after word2). Often used in conditional statements. if(word1.compareTo(word2) > 0){ //word1 comes after word2… } 16 RAJESHREE KHANDE
  • 17.
    Comparisons :Eaxmples //negative differences diff= “apple”.compareTo(“berry”); //a before b diff = “Zebra”.compareTo(“apple”); //Z before a diff = “dig”.compareTo(“dug”); //i before u diff = “dig”.compareTo(“digs”); //dig is shorter //zero differences diff = “apple”.compareTo(“apple”); //equal diff = “dig”.compareToIgnoreCase(“DIG”);/ /equal //positive differences diff = “berry”.compareTo(“apple”); //b after a diff = “apple”.compareTo(“Apple”); //a after A diff = “BIT”.compareTo(“BIG”); //T after G diff = “huge”.compareTo(“hug”); //huge is longer 17 RAJESHREE KHANDE
  • 18.
    String Methods :trim s2 = s1.trim(); Removes white spaces at the beginning & at the end of string s1  String word2 = word1.trim ();  returns a new string formed from word1 by removing white space at both end does not affect whites space in the middle String word1 = “ Hi Bob ”; String word2 = word1.trim(); //word2 is “Hi Bob” – no spaces on either end //word1 is still “ Hi Bob “ – with spaces 18 RAJESHREE KHANDE
  • 19.
    String Methods :replace  String word2 = word1.replace(‘x’, ‘y’); returns a new string formed from word1 by replacing all occurrences of x with y String word1 = “rare“; String word2 = “rare“.replace(‘r’, ‘d’); //word2 is “dade”, but word1 is still “rare“ 19 RAJESHREE KHANDE
  • 20.
    Numbers to Strings Threeways to convert a number into a string: 1. String s = "" + num; 2. String s = Integer.toString (i); String s = Double.toString (d); 3. String s = String.valueOf (num); Integer and Double are “wrapper” classes from java.lang that represent numbers as objects. They also provide useful static methods. s = String.valueOf(123) ; //”123” s = “” + 123;//”123” s = Integer.toString(123); //”123” s = Double.toString(3.14); //”3.14” 20 RAJESHREE KHANDE
  • 21.
    StringBuffer class  StringBufferis mutable object.  The StringBuffer class is an alternative to the String class.  StringBuffer is more flexible than String. You can add, insert, or append new contents into a string buffer. 21 RAJESHREE KHANDE
  • 22.
    StringBuffer : Methods MethodDescription s1.setCharAt(n, x) Modifies the nth char to x of string s1 s1.append(s2) Append the string s2 to s1 at the end s1.insert(n,s2) Insert the string s2 at the position n of string s1 s1.setLength(n) Set the length of the string s1 to n If n < s1,length() s1 is truncated. If n > s1.length zeros are added to s1 s1.deleteCharAt(n) Delete nth character from string s1 22RAJESHREE KHANDE