0% found this document useful (0 votes)
1 views29 pages

Chapter 4 - Strings

The document provides an overview of string handling in Java, explaining that strings are objects and detailing various methods available in the String class for creating and manipulating strings. Key topics include creating strings using literals and the new keyword, as well as methods for string manipulation such as length(), concat(), toUpperCase(), and substring(). The document also covers comparison methods like equals() and compareTo(), along with other functionalities like trimming and replacing characters.

Uploaded by

paarthsneh
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)
1 views29 pages

Chapter 4 - Strings

The document provides an overview of string handling in Java, explaining that strings are objects and detailing various methods available in the String class for creating and manipulating strings. Key topics include creating strings using literals and the new keyword, as well as methods for string manipulation such as length(), concat(), toUpperCase(), and substring(). The document also covers comparison methods like equals() and compareTo(), along with other functionalities like trimming and replacing characters.

Uploaded by

paarthsneh
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

Blue Ridge Public School

Blue Ridge Public School


Standard – X
Computer Applications – Chapter 4 – Strings
1
Blue Ridge Public School
Topics
• String Handling

• Methods in String Class

2
Blue Ridge Public School
String Handling
• Generally, a string is a sequence of characters
• But in Java, a string is an object that represents a
sequence of characters
• The Java platform provides the String class to
create and manipulate strings
• In Java, every string that you create is actually an
object of type String
• The [Link] class provide numerous
methods to work with strings
• Implementing strings as built-in objects allows 3
Java to provide many features for string handling
Blue Ridge Public School
String Handling – cont.
• String objects are stored in a special memory area known as
string constant pool
• 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.
• String Handling provides a lot of concepts that can be
performed on a string such as concatenation of string,
comparison of string, find sub string etc.

4
Blue Ridge Public School
Creating Strings
• String can be created in number of ways, here are a few ways
of creating string object:
• Using a String literal
• The most direct way to create a string is to write the string
• String str1=“Hello World”;
• In this case, "Hello World" is a string literal—a series of characters in
your code that is enclosed in double quotes
• Whenever it encounters a string literal in your code, the compiler
creates a String object with its value in this case, "Hello World”

5
Blue Ridge Public School
Creating Strings – cont.
• Each time you create a string literal, the JVM
(Java Virtual Machine) 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

6
Blue Ridge Public School
Creating Strings – cont.
• Using new Keyword
• You can also create String objects as you would create any other
Java object, using the new keyword and a constructor
• String str2 = new String("Java");

• Using another String object


• String str3 = new String(str2);

• Using + operator (Concatenation)


• String str4 = str1 + str2;
or,
• String str5 = "hello"+"Java";

7
Blue Ridge Public School
Methods in String Class
• One of the primary functions of modern computer science, is
processing human language
• Similar to how numbers are important to math, language
symbols are important to understanding and decision making
• Although it may not be visible to computer users, computers
process language in the background as precisely and
accurately as a calculator
• Help dialogs provide instructions, Menus provide choices and
data display shows status, errors, and real-time changes
• As a Java programmer, one of your main tools for storing and
processing language is going to be the String class
8
Blue Ridge Public School
Methods in String Class – cont.
• The class String includes methods for examining individual
characters

• Methods used to obtain information about an object are


known as accessor methods

• One accessor method that you can use with strings is the
length() method, which returns the number of characters
contained in the string object

9
Blue Ridge Public School
Methods in String Class – cont.
• The following program is an example of length(), method
String class
public class StringDemo {

public static void main(String args[]) {


String palindrome = "Dot saw I was Tod";
int len = [Link]();
[Link]( "String Length is : " + len );
}
}
• This will produce the following result −
• String Length is : 17 10
Blue Ridge Public School
Methods in String Class – cont.
• Concatenating Strings
• The String class includes a method for concatenating two strings

• [Link](string2);
• This returns a new string that is string1 with string2 added to it at
the end
• Strings are more commonly concatenated with the + operator, as
in −
• "Hello," + " world" + "!“
• which results in −
• "Hello, world!"

11
Blue Ridge Public School
Methods in String Class – cont.
• toUpperCase()
• I want my entire String to be shown in upper case
• toUpperCase(): This method is use to convert lower case string
into upper case
class StringHandling
{
public static void main(String arg[])
{
String s="Java";
[Link]("String: "+[Link]());
}
}
• Output
• String: JAVA 12
Methods in String Class – cont.

Blue Ridge Public School


• toLowerCase()
• I want my entire String to be shown in lower case
• toLowerCase(): This method is used to convert lower case string
into upper case
class StringHandling
{
public static void main(String arg[])
{
String s="JAVA";
[Link]("String: "+[Link]());
}
}
• Output
• String: java 13
Blue Ridge Public School
Methods in String Class – cont.
• trim()
• trim(): This method remove space which are available before
starting of string and after ending of string
class StringHandling
{
public static void main(String arg[])
{
String s=" Java is programming language ";
[Link]([Link]());
}
}
• Output
• Java is programming language 14
Blue Ridge Public School
Methods in String Class – cont.
• charAt(index)
• Given the index, how do you know the character at that
location?
• charAt(): This method is used to get the character at a
given index value
class StringHandling
{
public static void main(String arg[])
{
char c;
String s=new String("Java");
c=[Link](2);
[Link]("Character: "+c);
}
}
• Output
• Character: v 15
Blue Ridge Public School
Methods in String Class – cont.
• indexOf()
• If you know the length, how would you find which
character is at which position? In short, how will you find
the index of a character
• indexOf(): This method is used find the index value of
given string. It always gives starting index value of first
occurrence of string
class StringHandling
{
public static void main(String arg[])
{
String s="Java is programming language";
[Link]([Link]("programming"));
}
}
• Output
• 8 16
Blue Ridge Public School
Methods in String Class – cont.
Method Return Argument Description
type
indexOf() int char Returns index position of the character
passed as argument starting from zero
index
int char, int Returns index position of the character
passed as argument starting from the
index number specified as second
parameter
int String Returns index position of the String
passed as argument starting from zero
index
int String, int Returns index position of the String
passed as argument starting from the 17
index number specified as second
parameter
Blue Ridge Public School
Methods in String Class – cont.
• lastIndexOf()

• This method is used find the index of the last


occurrence of a character in the string

18
Blue Ridge Public School
Methods in String Class – cont.
• equals()
• This method is used to compare two strings, It return true if
strings are same otherwise return false. It is case sensitive
method
class StringHandling
{
public static void main(String arg[])
{
String s1="Hitesh";
String s2="Raddy";
String s3="Hitesh";
[Link]("Compare String: "+[Link](s2));
[Link]("Compare String: "+[Link](s3));
}
}
• Output
• Compare String: false 19

• Compare String: true


Methods in String Class – cont.

Blue Ridge Public School


• equalsIgnoreCase()
• This method is case insensitive method, It returns true if the
contents of both strings are same otherwise it returns false
class StringHandling
{
public static void main(String arg[])
{
String s1="Hitesh";
String s2="HITESH";
String s3="Raddy";
[Link]("Compare String: "+[Link](s2));
[Link]("Compare String: "+[Link](s3));
}
}
• Output
20
• Compare String: true
• Compare String: false
Blue Ridge Public School
Methods in String Class – cont.
• compareTo(), compareToIgnoreCase()
• This method is used to compare two strings by taking
unicode values, It returns 0 if the string are equal
• If the calling string is greater than the string in the
argument then the function returns any positive value
• If the calling string is less than the string in the
argument then the function returns any negative value

21
Methods in String Class – cont.

Blue Ridge Public School


class StringHandling
{
public static void main(String arg[])
{
String s1="Hitesh";
String s2="Raddy";
int i;
i=[Link](s2);
if(i==0)
{
[Link]("Strings are same");
}
else
{
[Link]("Strings are not same");
}}}
• Output 22

• Strings are not same


Methods in String Class – cont.

Blue Ridge Public School


class StringHandling
{
public static void main(String arg[])
{
String s1=“hitesh";
String s2=“HITESH";
int i;
i=[Link](s2);
if(i==0)
{
[Link]("Strings are same");
}
else
{
[Link]("Strings are not same");
}}}
• Output 23

• Strings are same


Blue Ridge Public School
Methods in String Class – cont.
• 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";
[Link]([Link](8)); // 8 is starting index
}
}
• Output
• programming language
24
Blue Ridge Public School
Methods in String Class – cont.
Method Return Argument Description
type
substring() String int Returns part of string from the specified
start index position to the last index
position

String int, int Returns part of string from the specified


start index position (first parameter) to
the end (second parameter) excluding
the character at the end index

25
Blue Ridge Public School
Methods in String Class – cont.
Method Return Argument Description
type
valueOf() String int or float Returns string after converting the
or double numeric values which are passed as
argument to the double string

Method Return type Argument Description

startsWith() boolean String It checks the given string that


begins with a specified string . It
returns either true or false
endsWith() boolean String It checks the given string that ends
with a specified string . It returns
either true or false 26
Methods in String Class – cont.

Blue Ridge Public School


• replace()
• replace(): This method is used to return a duplicate string by
replacing old character with new character
class StringHandling
{
public static void main(String arg[])
{
String s1="java";
String s2=[Link]('j', 'k');
[Link](s2);
}
}
• Output
• kava
• Note: In this method data of original string will 27

never be modified
Blue Ridge Public School
Methods in String Class – cont.
• startsWith()
• This method returns true if a string starts with a given prefix,
otherwise it returns false
class StringHandling
{
public static void main(String arg[])
{
String s="Java is programming language";
[Link]([Link]("Java"));
}
}
• Output
• true 28
Thank You!!

29

Blue Ridge Public School

You might also like