KEMBAR78
String Handling | PDF | String (Computer Science) | Java Virtual Machine
0% found this document useful (0 votes)
26 views23 pages

String Handling

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views23 pages

String Handling

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

String Handling

By: Vijay Kumar Soni(E8720)


Java String
• In java, string is basically an object that represents sequence of char
values. An array of characters works same as java string.
For example:
The most direct way to create a string is to write:
• String greeting = "Hello world!";
OR
• char[] ch={'j','a','v','a‘};
• String s=new String(ch);

• Note: The String class is immutable, so that once it is created


a String object cannot be changed
How to create String object?

• There are two ways to create String object:


• By string literal
• By new keyword
String Literal
• Java String literal is created by using double quotes. For Example:
• String s="welcome";
• Each time you create a string literal, the JVM checks the string constant pool
first. If the string already exists in the pool, a reference to the pooled
instance is returned. If string doesn't exist in the pool, a new string instance
is created and placed in the pool.
• For example:
• String s1="Welcome";
• String s2="Welcome";
• //will not create new instance
By new keyword
• String s=new String("Welcome");
//creates two objects and one reference variable

• In such case, JVM will create a new string object in normal(non pool)
heap memory and the literal "Welcome" will be placed in the string
constant pool. The variable s will refer to the object in heap(non pool).
• Java String class provides a lot of methods to perform operations on
string such as compare(), concat(), equals(), split(), length(), replace(),
compareTo(), intern(), substring() etc.
• The java.lang.String
class ,implements Serializable, Comparable and CharSequence interfaces
.
CharSequence Interface
• The CharSequence interface is used to represent sequence
of characters. It is implemented by String, StringBuffer and
StringBuilder classes. It means, we can create string in java
by using these 3 classes.
Java String class methods
Java String class methods
Java String compare

There are three ways to compare string in java:


• By equals() method
• By = = operator
• By compareTo() method
Ex:
• class Teststringcomparison1{
• public static void main(String args[]){
• String s1="Sachin";
• String s2="Sachin";
• String s3=new String("Sachin");
• String s4="Saurav";
• System.out.println(s1.equals(s2));//true
• System.out.println(s1.equals(s3));//true
• System.out.println(s1.equals(s4));//false
• }
• }
Ex:
• class Teststringcomparison2{
• public static void main(String args[]){
• String s1="Sachin";
• String s2="SACHIN";

• System.out.println(s1.equals(s2));//false
• System.out.println(s1.equalsIgnoreCase(s2));//true
• }
• }
Ex:
• class Teststringcomparison3{
• public static void main(String args[]){
• String s1="Sachin";
• String s2="Sachin";
• String s3=new String("Sachin");
• System.out.println(s1==s2);//
true (because both refer to same instance)
• System.out.println(s1==s3);//
false(because s3 refers to instance created in nonpool)
• }
•}
String compare by compareTo() method

• Suppose s1 and s2 are two string variables. If:


• s1 == s2 :0
• s1 > s2 :positive value
• s1 < s2 :negative value
Ex:
• class Teststringcomparison4{
• 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 )
• }
• }
String Concatenation in Java

• In java, string concatenation forms a new string that is the


combination of multiple strings. There are two ways to
concat string in java:
• By + (string concatenation) operator
• By concat() method
Ex:
• class TestStringConcatenation1{
• public static void main(String args[]){
• String s="Sachin"+" Tendulkar";
• System.out.println(s);//Sachin Tendulkar
• }
•}
Ex:
• class TestStringConcatenation2{
• public static void main(String args[]){
• String s=50+30+"Sachin"+40+40;
• System.out.println(s);//80Sachin4040
• }
•}
Ex:
Syntax:
• public String concat(String another)

• class TestStringConcatenation3{
• public static void main(String args[]){
• String s1="Sachin ";
• String s2="Tendulkar";
• String s3=s1.concat(s2);
• System.out.println(s3);//Sachin Tendulkar
• }
• }
Substring in Java
• A part of string is called substring. In other words, substring
is a subset of another string. In case of substring start Index
is inclusive and end Index is exclusive.
• public String substring(int startIndex).
• public String substring(int startIndex, int endIndex).

• Ex:
• String s="hello";
• System.out.println(s.substring(0,2));//he
Java StringBuffer class

• Java StringBuffer class is used to create mutable


(modifiable) string. The StringBuffer class in java is same as
String class except it is mutable i.e. it can be changed.
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

• 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
• }
• }

You might also like