Program: B.Tech, CSE, 3rdsem, 2nd year
CS 306: Java Lab
Unit-1
Ravi Parashar
Assistant Professor, Computer Science & Engineering
July-Dec, 2021 / Lecture No. 13
Part II-String handling in Java
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.
5
CS 306 Reference No.: R1
SELO: 1,2
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);
}
} 6
CS 306 Reference No.: R1
SELO: 1,2
Output
ALI
note : - if we write the string as
String s=“GWALIOR”;
Here s,indicates the base address of string.
7
CS 306 Reference No.: R1
SELO: 1,2
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());
}}
8
CS 306 Reference No.: R1
SELO: 1,2
Output:-
7
9
CS 306 Reference No.: R1
SELO: 1,2
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”);
10
CS 306 Reference No.: R1
SELO: 1,2
String p=z.toUpperCase();
System.out.println (p);
System.out.println (z);
} }
Output:-
GWALIOR
gwalior
11
CS 306 Reference No.: R1
SELO: 1,2
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”);
12
CS 306 Reference No.: R1
SELO: 1,2
if(x.equals(y))
{
System.out.println (“equal”);
}
else
System.out.println (“not equal”);
}
Output: equal
13
CS 306 Reference No.: R1
SELO: 1,2
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.
14
CS 306 Reference No.: R1
SELO: 1,2
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”);
}
15
CS 306 Reference No.: R1
SELO: 1,2
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.
L-13 part2-string handling.pptx

L-13 part2-string handling.pptx

  • 1.
    Program: B.Tech, CSE,3rdsem, 2nd year CS 306: Java Lab Unit-1 Ravi Parashar Assistant Professor, Computer Science & Engineering July-Dec, 2021 / Lecture No. 13 Part II-String handling in Java
  • 2.
    Outlines • Prerequisite • Objective •String constructors • Methods of String class • Learning Outcome • References
  • 3.
    Prerequisite • Basics understandingof C/C++ language. • Logic building skills. • Experience writing code in a any programming language.
  • 4.
    Objective • To acquireBasic understanding in Java • To develop the skill of working in Java •To able to understand string of Java
  • 5.
    String Constructors 1.String()- itis 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. 5 CS 306 Reference No.: R1 SELO: 1,2
  • 6.
    Program for parameterizedconstructor 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); } } 6 CS 306 Reference No.: R1 SELO: 1,2
  • 7.
    Output ALI note : -if we write the string as String s=“GWALIOR”; Here s,indicates the base address of string. 7 CS 306 Reference No.: R1 SELO: 1,2
  • 8.
    Various methods ofString 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()); }} 8 CS 306 Reference No.: R1 SELO: 1,2
  • 9.
  • 10.
    2. toUpperCase():- Itconverts 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”); 10 CS 306 Reference No.: R1 SELO: 1,2
  • 11.
    String p=z.toUpperCase(); System.out.println (p); System.out.println(z); } } Output:- GWALIOR gwalior 11 CS 306 Reference No.: R1 SELO: 1,2
  • 12.
    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”); 12 CS 306 Reference No.: R1 SELO: 1,2
  • 13.
    if(x.equals(y)) { System.out.println (“equal”); } else System.out.println (“notequal”); } Output: equal 13 CS 306 Reference No.: R1 SELO: 1,2
  • 14.
    5. boolean equalsIgnoreCase(String):thismethod 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. 14 CS 306 Reference No.: R1 SELO: 1,2
  • 15.
    Ex. if((x.compareTo(y))>0) { System.out.println (“string1is greater”); } else if(x.compareTo(y))<0) System.out.println (“String2 is greater”); else System.out.println (“both strings are equals”); } 15 CS 306 Reference No.: R1 SELO: 1,2
  • 16.
    Learning Outcomes • Understandthe basic concept in Java • To understand used of Java • Be able to work in Java
  • 17.
    References 1. Herbert schildt,”The complete Reference Java” ,TMH publications.