String
String is a group of characters. They are objects of type String in
[Link] 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”;
[Link]([Link]());
Question: What is the output of the following:
1. String s=“”; [Link]([Link]());
[Link]([Link]());
2. String s=“ “; [Link]([Link]());
[Link]([Link]());
String Methods – concat()
String s1=“Hello”;
String s2=“World”
[Link](s1+s2); //or
[Link]([Link](s2));
Questions – Predict the output
1. [Link](“Hai”+2+3);
2. [Link](2+3);
3. [Link](“Hai”+(2+3));
charAt()
• Method returns the character at the specified index. An index ranges from 0 to length() – 1
String name=“Varun”
[Link]([Link](0));
//will print character by character
for(int i=0;i<[Link]();i++)
[Link]([Link](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";
[Link]([Link](s2));//0 because both are equal
[Link]([Link](s3));//-5 because "h" is 5 times lower than "m"
[Link]([Link](s4));//-1 because "l" is 1 times lower than "m"
[Link]([Link](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=[Link](‘s’); //3
•int index2=[Link](“is");//2
•[Link](index1+" "+index2);
•int index3=[Link]("is",4);
•[Link](index3);
•int index4=[Link]('s');
•[Link](index4);
•}
•}
lastIndexOf()
public class LastIndexOfExample{
public static void main(String args[]){
String s1="this is index of example";
int index1=[Link]('s');
Predict the [Link](index1);//6
output int index = [Link]('s',5);
[Link](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";
[Link]([Link]("ja")); //true
[Link]([Link]("java string")); //true
[Link]([Link](“tion”)); //true
[Link]([Link](“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”;
[Link]([Link]()); # HELLO WORLD
[Link]([Link]()); #hello world
substring()
[Link] String substring(int startIndex): This
method returns new String object containing the
substring of the given string from specified startIndex
(inclusive).
[Link] 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";
[Link]([Link](6));//Tendulkar
[Link]([Link](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 ");
[Link]("Java");//now original string is changed
[Link](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 ");
[Link](1,"Java");//now original string is changed
[Link](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");
[Link](1,3);
[Link](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");
[Link](1,3,"Java");
[Link](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");
[Link]();
[Link](sb);//prints olleH
}
}