0% found this document useful (0 votes)
11 views

String Handling in Java

Uploaded by

shreyd1010
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

String Handling in Java

Uploaded by

shreyd1010
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

String Handling in Java

What is String?
⚫ Java String contains an immutable sequence of Unicode
characters.

⚫ Immutable class means that once an object is created, we


cannot change its content

⚫ Java String is different from string in C or C++, where (in


C or C++) string is simply an array of char.

⚫ String class is encapsulated under java.lang package.

2 Prof.A.v.Chechare
⚫ The basic aim of String Handling concept is storing the
string data in the main memory (RAM), manipulating the
data of the String, retrieving the part of the String etc

3 Prof.A.v.Chechare
Char & String
⚫ Character
It is an identifier enclosed within single quotes (' ').
Example: 'A', '$', 'p'

⚫ String:
String is a sequence of characters enclosed within
double quotes (" ") is known as String.
Example: "Java Programming".

4 Prof.A.v.Chechare
String In Details
⚫ In java programming to store the character data we have a
fundamental datatype called char.

⚫ Similarly to store the string data and to perform various


operation on String data, we have two predefined classes
they are:
1. String
2. StringBuffer

5 Prof.A.v.Chechare
1.String
⚫ String class object is immutable that means when we
create an object of String class it never changes in the
existing object.
Example:
class StringHandling
{
public static void main(String arg[])
{
String s=new String("java");
System.out.println(s);
}
}
6 Prof.A.v.Chechare
Methods of String class

1.length()
This method is used to get the number of character of
any string.
2. charAt(index)
This method is used to get the character at a given
index value.

7 Prof.A.v.Chechare
class StringHandling
{
public static void main(String arg[])
{
int l;
char c;
String s=new String("Java");

l=s.length();
System.out.println("Length: "+l);

c=s.charAt(2);
System.out.println("Character: "+c);

}
}
Que.Write down a program in java to accept two string from user and compare if they are of
same length or different
Que.Write down a program in java to accept three strings from user and print the strings having
‘i’
At index 2
8 Prof.A.v.Chechare
3. toUpperCase()
This method is use to convert lower case string into
upper case.
4. toLowerCase()
This method is used to convert upper case string into
lower case.

9 Prof.A.v.Chechare
class StringHandling
{
public static void main(String arg[])
{
String s="Java";
System.out.println("String:"+s.toUpperCase());
System.out.println("String:"+s.toLowerCase());
}
}

10 Prof.A.v.Chechare
⚫ 5. concat()
This method is used to combine two strings.

class StringHandling
{
public static void main(String arg[])
{
String s1="Hitesh";
String s2="Raddy";
System.out.println("Combined String: "+s1.concat(s2));
}
}
11 Prof.A.v.Chechare
6 .equals(): This method is used to compare two strings,
It return true if strings are same
otherwise return false.
It is case sensitive method.

7. equalsIgnoreCase(): This method is used to compare two


strings
This method is case insensitive method,
It return true if the contents of both strings are same
otherwise false.

12 Prof.A.v.Chechare
class StringHandling
{
public static void main(String arg[])
{
String s1="Hitesh";
String s2="HITESH";
System.out.println("Compare String: "+s1.equals(s2));

System.out.println("Compare String: “ +s1.equalsIgnoreCase(s2));


}
}

13 Prof.A.v.Chechare
8. startsWith():
This method return true if string is start with given another
string, otherwise it returns false.

9. endsWith(): This method return true if string is end with


given another string, otherwise it returns false.

14 Prof.A.v.Chechare
class StringHandling
{
public static void main(String arg[])
{
String s="Java is programming language";
System.out.println(s.startsWith("Java"));
System.out.println(s.endsWith("language"));
}
}

15 Prof.A.v.Chechare
10.subString(): This method is used to get the part of given
string.

class StringHandling
{
public static void main(String arg[])
{
String s="Java is programming language";
System.out.println(s.substring(8));
}
}

16 Prof.A.v.Chechare
class StringHandling
{
public static void main(String arg[])
{
String s="Java is programming language";
System.out.println(s.substring(8, 12));
}
}

17 Prof.A.v.Chechare
11.split(): This method is used to divide the given string into
number of parts based on delimiter (special symbols like @
space , ).
class StringHandling
{
public static void main(String arg[])
{
String s="[email protected]";
String[] s1=s.split("@");
for(String c:s1)
{
System.out.println(c);
}
}
}
18 Prof.A.v.Chechare
12.replace(): This method is used to return a duplicate
string by replacing old character with new character.
Note: In this method data of original string will never be
modify.
class StringHandling
{
public static void main(String arg[])
{
String s1="java";
String s2=s1.replace('j', 'k');
System.out.println(s2);
}
}
19 Prof.A.v.Chechare
Exercise Questions on String
1 WAP in Java to Display “This is java Programming it
is also known as object oriented programming” and
replace space “ “ with “*” asteric
2. WAP in java to input email id from user and identify
the e mail box used
3.

20 Prof.A.v.Chechare
2.StringBuffer
⚫ It is a predefined class in java.lang package can be used to
handle the String, whose object is mutable that means content
can be modify.

21 Prof.A.v.Chechare
1.reverse(): This method is used to reverse the given
string and also the new value is replaced by the old
string.
class StringHandling
{
public static void main(String arg[])
{
StringBuffer sb=new StringBuffer("java code");
System.out.println(sb.reverse());
}
}

22 Prof.A.v.Chechare
2.insert(): This method is used to insert either string or
character or integer or real constant or boolean value
at a specific index value of given string.
class StringHandling
{
public static void main(String arg[])
{
StringBuffer sb=new StringBuffer("this is my java code");
System.out.println(sb.insert(11, "first "));
}
}

23 Prof.A.v.Chechare
3.append(): This method is used to add the new string at the
end of original string.
class StringHandling
{
public static void main(String arg[])
{
StringBuffer sb=new StringBuffer("java is easy");
System.out.println(sb.append(" to learn"));
}
}

24 Prof.A.v.Chechare
4. replace() This method is used to replace any old string
with new string based on index value.
class StringHandling
{
public static void main(String arg[])
{
StringBuffer sb=new StringBuffer("This is my code");
System.out.println(sb.replace(8, 10, "java"));
}
}

25 Prof.A.v.Chechare
5. deleteCharAt(): This method is used to delete a
character at given index value.
class StringHandling
{
public static void main(String arg[])
{
StringBuffer sb=new StringBuffer("java");
System.out.println(sb.deleteCharAt(3));
}
}

26 Prof.A.v.Chechare
6. delete(): This method is used to delete string form
given string based on index value.
class StringHandling
{
public static void main(String arg[])
{
StringBuffer sb=new StringBuffer("java is easy to learn");
StringBuffer s; s=sb.delete(8, 13); System.out.println(sb);
}
}

27 Prof.A.v.Chechare
Difference between equals() method and ==
operator
⚫ equals() method always used to comparing contents of both
source and destination String.
⚫ == Operator is always used for comparing references of both
source and destination objects but not their contents.

28 Prof.A.v.Chechare

You might also like