String: String Is A Group of Characters. They Are Objects of Type String in Java - Lang Package
String: String Is A Group of Characters. They Are Objects of Type String in Java - Lang Package
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());
String name=“Varun”
System.out.println(name.charAt(0));
• 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
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.
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
}
}