0% found this document useful (0 votes)
17 views8 pages

Strings

The document provides an overview of the String class in Java, detailing its definition, creation methods, and various string handling methods. It explains how to create strings using both direct assignment and the 'new' keyword, and lists methods such as length(), concat(), compareTo(), and others for manipulating strings. Additionally, it covers string indexing, case sensitivity in comparisons, and methods for trimming and converting case.

Uploaded by

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

Strings

The document provides an overview of the String class in Java, detailing its definition, creation methods, and various string handling methods. It explains how to create strings using both direct assignment and the 'new' keyword, and lists methods such as length(), concat(), compareTo(), and others for manipulating strings. Additionally, it covers string indexing, case sensitivity in comparisons, and methods for trimming and converting case.

Uploaded by

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

String

->A string can be defined as group of charactors or sequence of charactors.


-->in java strings are represented using a class called "String".
-->String is a pre-defined class present in java.lang package.
-->String class is used to represent strings in java language.
-> String class provides various methods to performe operations on strings.
-->in java string values are represented with pair of double quotes.

creating strings (creating object of String class)

there are 2 ways to create a string

1.by creating reference for String class


if we create reference for String class then JVM internally,automatically creates
object for String class.
String s="hai";
when JVM executes above statement,it internally creates object for String class
in that object it stores "hai" and that object is referenced by refreence variable s.
so if we write System.out.println(s);
o/p hai

2.by using new keyword


We can create String class object usinh new keyword

String s=new String("hai");


System.out.println(s);
o/p hai

String Handling Method


String is a pre-defined class present in java.lang package.
String class provides various methods which are used to performe operations on
strings such methods are "String Handling Methods".
the following are methods present in String class

1.int length()
this method returns number of characters in the given string.
Ex:
String s="hai";
int k=s.length();
System.out.println(k);
o/p 3
2. String concat(String s)
this method joins the given 2 strings.
Ex:
String s1="me";
String s2="dha";
String k=s1.concat(s2);
System.out.println(k);
o/p medha

3.int compareTo(String s)
this method is used to compare the given 2 strings.
->this method considers cases while comparing the given 2 strings.
-->this method returns integer value.
-->it returns 0 if s1=s2
-->it returns non-zero if s1!=s2
Ex:
String s1="hai";
String s2="Hai";
int k=s1.compareTo(s2);
if(k==0)
System.out.println("both are same");
else
System.out.println("not same");

o/p : not same


4.int compareToIgnoreCase(String s)
this method is used to compare the given 2 strings.
-->this method ignores cases while comparing the given 2 strings.
-->this method returns integer value.
-->it returns 0 if s1=s2
-->it returns non-zero if s1!=s2
Ex:
String s1="hai";
String s2="Hai";
int k=s1.compareToIgnoreCase(s2);
if(k==0)
System.out.println("both are same");
else
System.out.println("not same");

o/p : both are same

5.boolean equals(String s)
this method compares the given 2 strings.
this method considers cases while comparing given 2 strings.
this method returns either true or false.
it returns true if the given 2 strings are same
it returns false if the given 2 strings are not same
Ex:
String s1="hai";
String s2="Hai";
boolean k=s1.equals(s2);
\if(k)
System.out.println("both are same");
else
System.out.println("both are not same");

o/p: both are not same


6.boolean equalsIgnoreCase(String s)
this method compares the given 2 strings.
this method ignores cases while comparing given 2 strings.
this method returns either true or false.
it returns true if the given 2 strings are same
it returns false if the given 2 strings are not same
Ex:
String s1="hai";
String s2="Hai";
boolean k=s1.equalsIgnoreCase(s2);
if(k)
System.out.println("both are same");
else
System.out.println("both are not same");

o/p both are same.

Note
====
the charactors present in string are represented with indexes

-->string index starts at 0

String s="hai";

charactors indexes
---------- --------
h------------------0
a------------------1
i------------------2
7.int indexOf(String s)
this method returns first occurence of the given string in the main string.
if the given string is not found inside the main string then this method returns -1.
Ex:
String s="this is book";
int k=s.indexOf("is");
System.out.println(k);
o/p 2

is---2,3--->2
is---5,6--->5

t---0
h---1
i---2
s---3
space---4
i---5
s---6
space---7
b---8
o---9
o---10
k----11

8.int lastIndexOf(String s)
this method returns last occurence of the given string in the main string.
if the given string is not found inside the main string then this method returns -1.

Ex:
String s="this is book";
int k=s.lastIndexOf("is");
System.out.println(k);
o/p 5
is---2,3--->2
is---5,6--->5
t---0
h---1
i---2
s---3
space---4
i---5
s---6
space---7
b---8
o---9
o---10
k----11

9.char charAt(int i)
this method returns charactor at the given index/position
Ex:
s="this is book";
t---0
h---1
i---2
s---3
space---4
i---5
s---6
space---7
b---8
o---9
o---10
k----11

char k=s.charAt(1);
System.out.println(k);

o/p h
===

10.String toUpperCase()
this method converts the given string into uppercase letters
Ex:
String s="hai";
String k=s.toUpperCase();
System.out.println(k);

o/p HAI
11.String toLowerCase()
this method converts the given string into lowercase letters.
Ex:
String s="HAI";
String k=s.toLowerCase();
System.out.println(k);

o/p hai
===

12.String substring(int i)
this method extracts the substring from the main string from ith position(index) till
end of the string
Ex:
String s="New Delhi";
String k=s.substring(4);
System.out.println(k);

o/p:Delhi

N---0
e---1
w---2
space--3
D-----4
e---5
l----6
h----7
i----8

13.String substring(int i,int j)


this method extracts substring from the main string from ith index to (j-1)th
index
Ex:
String s="New Delhi";
String k=s.substring(0,3);

System.out.println(k);

o/p New

i=0 j=3

i to j-1
0 to 3-1
0 to 2==>0,1,2

14.String trim()

this method removes unwanted spaces(sapces at begining and spaces and ending)
from the string.

String s=" ravi";


System.out.println(s);
String k=s.trim();
System.out.println(k);

You might also like