Text Processing
String class
 Represents character strings.
 String is a sequence of Characters.
 Strings are Immutable.
Creation of Strings
String word1 = “Java”;
String word2 = “Java”;
String word1 = new String(“Java”);
String word2 = new String(“Java”);
word1
String Constant Pool Heap Memory
“Java”
“Java”
“Java”
word2
word1
word2
Using String Literal Using new keyword
Immutability
 Once created, a string cannot be changed.
Example:
String hostName = ''localhost'';
hostName.concat(''host'');
System.out.println(hostName); // localhost (immutable)
Disadvantage Of Immutability
• Need to create new String even for small change.
Example:
String school = ''hogwarts'';
char ch = Character.toUpperCase(school.charAt (0));
school = ch + school.substring(1); // Hogwarts
school
“Hogwarts”
“hogwarts”
StringBuffer Class
 Mutable, Synchronized
 toString() method is used to convert StringBuffer
to String
Example:
StringBuffer name = new StringBuffer("Hello");
name.append(" World");
System.out.println(name); // Hello World
StringBuilder Class
• StringBuilder is the same as the StringBuffer
class.
• Not synchronized.
• High Performance.
String Class Methods

int length()

char chatAt(int index)

String concat(String str)

String substring(int beginIndex, int endIndex)

int indexOf(char) / int indexOf(char, int)

String trim()

String replace(char oldChar, char newChar)

String toUpperCase()

String toLowerCase()
StringBuffer Methods
• int capacity()
• StringBuffer append()
• void setCharAt(int index, char ch)
• StringBuffer delete(int start, int end)
• StringBuffer reverse()
Comparison of Strings
 == operator
 boolean equals(Object obj)
 int compareTo(String str)
== operator
 == operator compares references not values.
Example:
class Test {
public static void main(String args[]) {
String s1 = "Wizard";
String s2 = "Wizard";
String s3 = new String("Wizard");
System.out.println(s1 == s2);// true
System.out.println(s1 == s3);// false
}
}
equals()
Example:
String s1 = "java";
String s2 = "java";
String s3 = new String("java");
String s4 = new String("java");
System.out.println(s1.equals(s2)); // true
System.out.println(s1.equals(s3)); // true
System.out.println(s3.equals(s4)); // true
StringBuffer sb1 = new StringBuffer("string");
StringBuffer sb2 = new StringBuffer("string");
System.out.println(sb1.equals(sb2)); // false
equals() method compares the content of the string.
CompareTo()
compareTo() method compares values lexicographically and
returns an integer value.
Example:
class CompareTo {
public static void main(String args[]) {
String s1 = "Jack";
String s2 = "Rose";
String s3 = "Jordan";
String s4 = new String("Rose");
System.out.println(s1.compareTo(s2));// -8 (J=74 R=82)
System.out.println(s3.compareTo(s1));// 14 (A=65 O=79)
System.out.println(s1.compareTo(s4));// 0
}
}
Text processing

Text processing

  • 1.
  • 2.
    String class  Representscharacter strings.  String is a sequence of Characters.  Strings are Immutable.
  • 3.
    Creation of Strings Stringword1 = “Java”; String word2 = “Java”; String word1 = new String(“Java”); String word2 = new String(“Java”); word1 String Constant Pool Heap Memory “Java” “Java” “Java” word2 word1 word2 Using String Literal Using new keyword
  • 4.
    Immutability  Once created,a string cannot be changed. Example: String hostName = ''localhost''; hostName.concat(''host''); System.out.println(hostName); // localhost (immutable)
  • 5.
    Disadvantage Of Immutability •Need to create new String even for small change. Example: String school = ''hogwarts''; char ch = Character.toUpperCase(school.charAt (0)); school = ch + school.substring(1); // Hogwarts school “Hogwarts” “hogwarts”
  • 6.
    StringBuffer Class  Mutable,Synchronized  toString() method is used to convert StringBuffer to String Example: StringBuffer name = new StringBuffer("Hello"); name.append(" World"); System.out.println(name); // Hello World
  • 7.
    StringBuilder Class • StringBuilderis the same as the StringBuffer class. • Not synchronized. • High Performance.
  • 8.
    String Class Methods  intlength()  char chatAt(int index)  String concat(String str)  String substring(int beginIndex, int endIndex)  int indexOf(char) / int indexOf(char, int)  String trim()  String replace(char oldChar, char newChar)  String toUpperCase()  String toLowerCase()
  • 9.
    StringBuffer Methods • intcapacity() • StringBuffer append() • void setCharAt(int index, char ch) • StringBuffer delete(int start, int end) • StringBuffer reverse()
  • 10.
    Comparison of Strings == operator  boolean equals(Object obj)  int compareTo(String str)
  • 11.
    == operator  ==operator compares references not values. Example: class Test { public static void main(String args[]) { String s1 = "Wizard"; String s2 = "Wizard"; String s3 = new String("Wizard"); System.out.println(s1 == s2);// true System.out.println(s1 == s3);// false } }
  • 12.
    equals() Example: String s1 ="java"; String s2 = "java"; String s3 = new String("java"); String s4 = new String("java"); System.out.println(s1.equals(s2)); // true System.out.println(s1.equals(s3)); // true System.out.println(s3.equals(s4)); // true StringBuffer sb1 = new StringBuffer("string"); StringBuffer sb2 = new StringBuffer("string"); System.out.println(sb1.equals(sb2)); // false equals() method compares the content of the string.
  • 13.
    CompareTo() compareTo() method comparesvalues lexicographically and returns an integer value. Example: class CompareTo { public static void main(String args[]) { String s1 = "Jack"; String s2 = "Rose"; String s3 = "Jordan"; String s4 = new String("Rose"); System.out.println(s1.compareTo(s2));// -8 (J=74 R=82) System.out.println(s3.compareTo(s1));// 14 (A=65 O=79) System.out.println(s1.compareTo(s4));// 0 } }