String
String is a group of characters. They are objects of type String in
java.lang package.
String vs StringBuffer
String StringBuffer
Immutable Mutable
Declaring a String object
String s=new String()
Or
String s=“abc”;
String Methods – length()
• Returns length of the string
• String name = “Varun”; //StringBuffer name=“Varun”;
System.out.println(name.length());
Question: What is the output of the following:
1. String s=“”; System.out.println(s.length());
System.out.println(s.isEmpty());
2. String s=“ “; System.out.println(s.length());
System.out.println(s.isEmpty());
String Methods – concat()
String s1=“Hello”;
String s2=“World”
System.out.println(s1+s2); //or
System.out.println(s1.concat(s2));
Questions – Predict the output
1. System.out.println(“Hai”+2+3);
2. System.out.println(2+3);
3. System.out.println(“Hai”+(2+3));
charAt()
• Method returns the character at the specified index. An index ranges from 0 to length() – 1
String name=“Varun”
System.out.println(name.charAt(0));
//will print character by character
for(int i=0;i<name.length();i++)
System.out.println(name.charAt(i));
equals() and equalsIgnoreCase()
• equals() Method- case sensitive comparison
• equalsIgnoreCase() Method – case insensitive
Remember comparison
public class CompareToExample{
public static void main(String args[]){
String s1="hello";
String s2="hello";
String s3="meklo";
String s4="hemlo";
String s5="flag";
System.out.println(s1.compareTo(s2));//0 because both are equal
System.out.println(s1.compareTo(s3));//-5 because "h" is 5 times lower than "m"
System.out.println(s1.compareTo(s4));//-1 because "l" is 1 times lower than "m"
System.out.println(s1.compareTo(s5));//2 because "h" is 2 times greater than "f"
}}
• equals() and equalsIgnoreCase() return Boolean
• compareTo() and compareToIgnoreCase() return int
Quick Recap
indexOf() – Overloaded methods
• int indexOf(int ch)- It searches for the character represented by ch within this
string and returns the index of first occurrence of this character
• int indexOf(String str) - It searches for the substring specified by str within this
string and returns the index of first occurrence of this substring
• int indexOf(int ch, int fromIndex)- It searches for the character
represented by ch within this string and returns the index of first occurrence of this
character starting from the position specified by fromIndex
• int indexOf(String str, int fromIndex) – Returns the index within
this string of the first occurrence of the specified substring, starting at the specified
index.
•public static void main(String args[])
•{
•String s1="this is index of example";
•int index1=s1.indexOf(‘s’); //3
•int index2=s1.indexOf(“is");//2
•System.out.println(index1+" "+index2);
•int index3=s1.indexOf("is",4);
•System.out.println(index3);
•int index4=s1.indexOf('s');
•System.out.println(index4);
•}
•}
lastIndexOf()
public class LastIndexOfExample{
public static void main(String args[]){
String s1="this is index of example";
int index1=s1.lastIndexOf('s');
Predict the System.out.println(index1);//6
output int index = s1.lastIndexOf('s',5);
System.out.println(index);//5
}
}
Remember
• indexOf() and lastIndexOf() return -1 when the specified
char or substring is not found.
• startsWith() – Tests if this string starts with the specified prefix
• endsWith() - Tests if this string ends with the specified suffix.
String s1="java string demonstration";
System.out.println(s1.startsWith("ja")); //true
System.out.println(s1.startsWith("java string")); //true
System.out.println(s1.endsWith(“tion”)); //true
System.out.println(s1.endsWith(“fusion”)); //false
Case Conversion methods
• toLowerCase(): Method converts all of the characters in a String to lower case
• toUpperCase(): Method converts all of the characters in a String to upper case
String s=“Hello World”;
System.out.println(s.toUpperCase()); # HELLO WORLD
System.out.println(s.toLowerCase()); #hello world
substring()
1.public String substring(int startIndex): This
method returns new String object containing the
substring of the given string from specified startIndex
(inclusive).
2.public String substring(int startIndex, int
endIndex): This method returns new String object
containing the substring of the given string from
specified startIndex to endIndex(exclusive)
Predict the output
public class TestSubstring{
public static void main(String args[]){
String s="SachinTendulkar";
System.out.println(s.substring(6));//Tendulkar
System.out.println(s.substring(0,6));//Sachin
}
}
trim()
• trim() - Returns a copy of the string, with leading and trailing whitespace omitted.
public String trim()
String s = “ Hi Mom! “.trim();
S = “Hi Mom!”
Declaring StringBuffer object
• StringBuffer s=new StringBuffer();
• StringBuffer s=new StringBuffer(“abc”);
Methods
• insert()
• append()
• delete()
• replace()
• reverse()
• length() - same as String
StringBuffer – append() method
class StringBufferExample{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello ");
sb.append("Java");//now original string is changed
System.out.println(sb);//prints Hello Java
}
}
StringBuffer – insert() method
• The insert() method inserts the given string with this string at
the given position.
class StringBufferExample2{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello ");
sb.insert(1,"Java");//now original string is changed
System.out.println(sb);//prints HJavaello
}
}
StringBuffer – delete() method
• The delete() method of StringBuffer class deletes the string from
the specified beginIndex to endIndex.
class StringBufferExample4{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
sb.delete(1,3);
System.out.println(sb);//prints Hlo
}
}
StringBuffer – replace()
• The replace() method replaces the given string from the
specified beginIndex and endIndex.
class StringBufferExample3{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
sb.replace(1,3,"Java");
System.out.println(sb);//prints HJavalo
}
}
StringBuffer – reverse()
• The reverse() method of StringBuilder class reverses the current
string.
class StringBufferExample5{
public static void main(String args[]){
StringBuffer sb=new StringBuffer("Hello");
sb.reverse();
System.out.println(sb);//prints olleH
}
}