You are on page 1of 18

Program: B.

Tech, CSE, 3rdsem, 2nd year

CS 306: Java Lab


Unit-1
Part II-String handling in Java

July-Dec, 2021 / Lecture No. 13

Ravi Parashar
Assistant Professor, Computer Science & Engineering
Outlines
• Prerequisite
• Objective
• String constructors
• Methods of String class
• Learning Outcome
• References
Prerequisite
• Basics understanding of C/C++ language.
• Logic building skills.

• Experience writing code in a any programming language.


Objective
• To acquire Basic understanding in Java

• To develop the skill of working in Java

•To able to understand string of Java


String Constructors
1.String()- it is default constructor ,used to create empty string.
Syntax:-
String x=new String();
Here x is an object.
2.String(string):- it is parameterized constructor.
Syntax:- String x= new String(“Gwalior”);
3.String(char[],int p,int n):- In this p is position from where
characters are read,n is the number of characters to be read.

CS 306 SELO: 1,2 Reference No.: R1 5


Program for parameterized constructor
class check
{
public static void main(String ar[])
{
char x={‘G’,’W’,’A’,’L’,’I’,’O’,’R’};
String x=new String(x,2,3);
System.out.println (x);
}
CS 306 } SELO: 1,2 Reference No.: R1 6
Output
ALI
note : - if we write the string as
String s=“GWALIOR”;
Here s,indicates the base address of string.

CS 306 SELO: 1,2 Reference No.: R1 7


Various methods of String class
1. int length()- this method returns the number of characters on
string.
Ex. Class check
{
public static void main (String ar[])
{
String x= new String(“Gwalior”);
System.out. println(x.length());
}}
CS 306 SELO: 1,2 Reference No.: R1 8
Output:-
7

CS 306 SELO: 1,2 Reference No.: R1 9


2. toUpperCase():- It converts all rhe characters in a string
from lowercase to uppercase.
3.toLowerCase():- It converts all characters from uppercase to
lowercase.
Ex. class check
{
public static void main (String ar[])
{
String z= new String (“ gwalior”);
CS 306 SELO: 1,2 Reference No.: R1 10
String p=z.toUpperCase();
System.out.println (p);
System.out.println (z);
}}
Output:-
GWALIOR
gwalior

CS 306 SELO: 1,2 Reference No.: R1 11


4. boolean equals(String)- If we want to compare string
values.If both strings are equal then this method returns true
otherwise returns false.
Ex. class check
{
public static void main(String ar[])
{
String x=new String(“Gwalior”);
String y=new String (“Gwalior”);
CS 306 SELO: 1,2 Reference No.: R1 12
if(x.equals(y))
{
System.out.println (“equal”);
}
else
System.out.println (“not equal”);
}
Output: equal

CS 306 SELO: 1,2 Reference No.: R1 13


5. boolean equalsIgnoreCase(String):this method ignores the
cases and compares the strings.
6.int compareTo(String):- It is used to compare two strings.
-If string1 is greater than String2 this method returns + ve
value
-If string1 is lesser than string2 then this method returns –ve
value.
-If string1==string2 this function returns zero.

CS 306 SELO: 1,2 Reference No.: R1 14


Ex. if((x.compareTo(y))>0)
{
System.out.println (“string1 is greater”);
}
else if(x.compareTo(y))<0)
System.out.println (“String2 is greater”);
else
System.out.println (“both strings are equals”);
}
CS 306 SELO: 1,2 Reference No.: R1 15
Learning Outcomes

• Understand the basic concept in Java


• To understand used of Java

• Be able to work in Java


References
1. Herbert schildt ,”The complete Reference Java” ,TMH
publications.

You might also like