C++ and Java
C++ and Java
C++ and Java
STRING HANDLING
OUTLINE OF MODULE
String methods
String Buffer
Utility classes
Strings in Java
String Handling
String Handling provides a lot of concepts that can be
performed on a string such as concatenating string,
comparing string, substring etc.
Once string object is created its data or state can't be changed but a
new string object is created.
class Simple
{
public static void main(String args[])
{
String s="Sachin";
s.concat(" Tendulkar);
System.out.println(s);
}
}
ADVANTAGES OF IMMUTABILITY
By string literal
By new keyword
Creating String
Direct way to create a string
Variable
name
length()
concat()
charAt()
compareTo()
compareToIgnoreCase()
endsWith()
startsWith()
Equals()
Methods in String Class
equalsIgnoreCase()
indexOf(int ch)
indexOf(String str)
substring(int beginIndex)
toLowerCase()
toUpperCase()
trim()
STRING COMPARE
We can compare string in java on the basis of content and
reference.
By = = operator
By compareTo() method
STRING COMPARE BY EQUALS() METHOD
Two methods:
class Test
{
public static void main(String args[])
{
String s1=“Dibya";
String s2=“DIBYA";
System.out.println(s1.equals(s2));//false
System.out.println(s1.equalsIgnoreCase(s3));//true
}
}
STRING COMPARE BY == OPERATOR
The = = operator compares references not values.
class Test
{
public static void main(String args[])
{
String s1=“Dibya";
String s2=“Dibya";
String s3=new String(“Dibya");
System.out.println(s1==s2);//true (both refer to same instance)
System.out.println(s1==s3);//false( s3 refers to instance)
}
}
COMPARETO() METHOD
The String compareTo() method compares values and returns
an integer value.
Suppose s1 and s2 are two string variables. If:
s1 == s2 :0
s1 > s2 :positive value
s1 < s2 :negative value
class Test
{
public static void main(String args[])
{
String s1="Sachin";
String s2="Sachin";
String s3="Ratan";
System.out.println(s1.compareTo(s2)); //0
System.out.println(s1.compareTo(s3)); //1(because s1>s3)
System.out.println(s3.compareTo(s1)); //-1(because s3 < s1 )
}
}
CHARAT()
charAt() method returns a char value at the given index
number. The index number starts value is 0.
Syntax:
String.replace(char oldChar, char newChar)
class Replace
{
public static void main(String args[])
{
String s1=" java";
String replaceString=s1.replace('a','e');//replaces all occur of 'a' to 'e'
System.out.println(replaceString);
}}
TRIM()
The trim() method in java removes the spaces and returns
the omitted string.
class StringTrim
{
public static void main(String args[])
{
String s1=" Hello ";
System.out.println(s1.trim()+" Tech ");//with trim()
}
Output:
} Hello Tech
INDEXOF()
indexOf() method returns index of given character value or
substring. If it is not found, it returns -1.
1 int indexOf(int ch) returns index position for the given char value
3 int indexOf(String substring) returns index position for the given substring
System.out.println(index1+" "+index2);//2 8
}
}
TOLOWERCASE(),
toLowerCase() method converts all characters of the string
into lower case letter.
2
}
9
TOUPPERCASE()
toUpperCase() method converts all characters of the string into
upper case letter
3
1
VALUEOF()
3
2
STARTSWITH()
It returns true if this string starts with given prefix else returns
false.
3
4
ENDSWITH()
endsWith() method checks if this string ends with given
suffix.
Syntax: endsWith(String suffix);
replace()-Replace one set of characters with another set inside a StringBuffer object
1. APPEND() METHOD
The append() method concatenates the given new string at
the end of the calling StringBuffer.
Syntax:
Buffer_Name.append(String);
Example:
class A
Output
{
public static void main(String args[]) Hello Java
{
StringBuffer sb = new StringBuffer("Hello ");
sb.append("Java"); // now original string is changed
System.out.println(sb);
}
}
2. LENGTH() METHOD
Returns the length of the string
Syntax:
Buffer_Name.length(String);
Example:
import java.io.*;
class Main
{
public static void main(String args[])
{
StringBuffer sb= new StringBuffer("Java");
System.out.println(“The length String is:"+sb.length());
} OUPUT
}
The length String is: 4
SETLENGTH()
Syntax:
Buffer_Name.setLength();
import java.io.*;
class Main
{
public static void main(String args[])
Syntax:
Buffer_Name.capacity();
EXAMPE:
Output
import java.io.*; 16
class Main 16
{ 34
public static void main(String args[])
{
StringBuffer sb = new StringBuffer();
System.out.println(sb.capacity()); // default 16
sb.append("Hello");
System.out.println(sb.capacity()); // now 16
import java.io.*;
class Main
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("JAVA");
sb.setCharAt(2,'H');
System.out.println(sb); Output
}
JAHA
}
INSERT() METHOD
The insert() method inserts the given string with this string at the given
position.
Syntax:
Buffer_Name.insert(start_ index,String);
Example:
import java.io.*;
class A
Output
{
public static void main(String args[]) Hello Java
{
StringBuffer sb = new StringBuffer(“ Java");
sb.insert(0, “Hello");
System.out.println(sb);
}
}
DELETE() METHOD
The deletes the string from the specified begin Index to endIndex-1.
Syntax:
Buffer_Name.delete(start_ index,end_index);
Example:
import java.io.*;
class Main Output
{
public static void main(String args[]) Hlo
{
StringBuffer sb = new StringBuffer("Hello");
sb.delete(1, 3);
System.out.println(sb);
}
}
REVERSE() METHOD
The reverse() method used to reverses the string.
Syntax:
Buffer_Name.reverse ();
import java.io.* ;
class Main
Output
{
public static void main(String args[]) olleH
{
StringBuffer sb = new StringBuffer("Hello");
System.out.println(sb.reverse());
}
}
REPLACE() METHOD
The replace() method used to replace given string from specified
starting index and ending Index-1.
Syntax:
Buffer_Name.replace (start_index,end_index,string);
Example:
import java.io.*;
class A
{
public static void main(String args[])
{
StringBuffer sb = new StringBuffer("Hello");
sb.replace(1, 3, "Java");
System.out.println(sb);
}
}
sb.replace(0,5,"HAI");
import java.lang.*; System.out.println(sb);
class Main
{ sb.delete(0,3);
public static void main(String args[]) System.out.println(sb);
{
StringBuffer sb=new StringBuffer("HELLO "); sb.reverse();
System.out.println(sb);
System.out.println(sb.length());
sb.reverse();
System.out.println(sb.capacity()); System.out.println(sb);
}}
System.out.println(sb);
OUTPUT
sb.append(" JAVA"); 6
22
System.out.println(sb);
HELLO
HELLO JAVA
sb.insert(6,"PROGRAMMING"); HELLO PROGRAMMING JAVA
System.out.println(sb); HAI PROGRAMMING JAVA
PROGRAMMING JAVA
AVAJ GNIMMARGORP
PROGRAMMING JAVA