Icse String Notes
Icse String Notes
Icse String Notes
MARUTI
COMPUTER
CLASSES
- Vivek Sir
1
Click to edit Master title style
2 2
Click to edit Master title style MARUTI COMPUTER CLASSES
Java – ICSE
reference
3 3
MARUTI COMPUTER CLASSES
String
Click to edit Master title style
4 4
MARUTI COMPUTER CLASSES
Click to edit
Methods of String
Masterclass
title style
length() chartAt():
• This function is used to return length of a • This function is used to return a character at
string i.e., number of characters present in the the specified index passed as argument. It
string. It returns an integer(int) value. returns character(char) type of value.
• Syntax: int variable=string variable.length(); • Syntax: char variable=string
variable.charAt(indexno);
• For e.g., String str=“COMPUTER IS FUN”;
• For e.g., String str=“COMPUTER IS FUN”;
• int len=str.length();
• char ch=str.charAt(3);
• Here the value of len will be 15.
• Here the value of ch will be ‘P’.
5 5
MARUTI COMPUTER CLASSES
Click to edit
Methods of String
Masterclass
title style
indexOf(): lastIndexOf();
• This function returns the index of first • This function returns the index of last
occurrence of character passed as argument. occurrence of character passed as argument.
It returns -1 if character is not present in the It returns -1 if character is not present in the
string. It returns an integer(int) value. string. It returns an integer(int) value.
• Syntax: int variable = string • Syntax: int variable = string
variable.indexOf(character); variable.lastIndexOf(character);
• For e.g., String str=“MALAYALAM”; • For e.g., String str=“MALAYALAM”;
• int p=str.indexOf(‘A’); • int p=str.lastIndexOf(‘A’);
• Here the value of p will be 1. • Here the value of p will be 7.
6 6
MARUTI COMPUTER CLASSES
Click to edit
Methods of String
Masterclass
title style
substring(): replace():
• This function is used to extract the part of string. It accepts • This function is used to replace a character by another
two index character or s String
• numbers as arguments and returns the strings between the • with another String at all its occurrences in the given
index numbers. It returns String. It returns String type of
• String type of value. • value.
• Syntax: String • String
variable=stringvariable.substring(indexno,indexno); variable=stringvariable.replace(character,character);
• For e.g., String str=“INTERNATIONAL”; • String variable=stringvariable.replace(String,String);
• String p=str.substring(5,11);
• For e.g., String str=“MXLXYXLXM”;
• It returns the part of string starting from index number 5 till
10 excluding the character at 11th • String p=str.replace(‘X’,‘A’);
• index. Here the value of p will be NATION. • Here the value of p will be MALAYALAM.
• String str=“INTERNATIONAL”; • String str=“ROSE IS RED”;
• String p=str.substring(5); • String p=str.replace(“RED”,“WHITE”);
• Here the value of p will be NATIONAL. • Here the value of p will be ROSE IS WHITE.
7 7
MARUTI COMPUTER CLASSES
Click to edit
Methods of String
Masterclass
title style
concat(): compareTo():
• This function is used to compare two strings. It not only checks for
• This function is used to concatenate(join) two
• equality but also checks whether a string is smaller or bigger than the other.
strings together. It returns String It returns an
• integer(int) value. The value returned is =0 if both strings are equal
• type of value.
• >0 if first string is greater than second string
• Syntax: String • <0 if first string is less than second string
variable=stringvariable.concate(String); • This function performs the comparison like dictionary sequence. It results in
difference of
• For e.g., String str1=“INTER”; • the ASCII codes of the corresponding characters where they differ at the first
occurrence.
• String str2=“NATIONAL”; • Syntax: int variable=stringvariable.compareTo(String);
• For e.g., String str1=“COMPUTER”;
• String p=str1.concat(str2); • String str2=“COMPUTER”;
8 8
MARUTI COMPUTER CLASSES
Click to edit
Methods of String
Masterclass
title style
toUppercase(); toLowercase():
• This function returns the string in upper case. • This function returns the string in lower case. If
If any character is already any character is already
• in upper case or character is special character • in lower case or character is special character
then it remains same. It returns String type then it remains same. It returns String type
• of value. • of value.
• Syntax: String • Syntax: String
variable=stringvariable.toUpperCase() variable=stringvariable.toLowerCase()
• For e.g., String str=“CoMPuter”; • For e.g., String str=“CoMPuter”;
• String p=str.toUppercase(); • String p=str.to Lowercase();
• Here the value of p will be COMPUTER. • Here the value of p will be computer
9 9
MARUTI COMPUTER CLASSES
Click to edit
Methods of String
Masterclass
title style
equals(): equalsIgnoreCase():
• This function is used to compare two strings and check whether • This function is used to compare two strings and
they are equal
check whether
• or not. It returns true if the strings are identical, false otherwise.
This function treats • they are equal or not after ignoring the
• lower and upper case characters differently. It returns boolean case(upper case and lower case are treated
type of value. same).
• Syntax: boolean variable=stringvariable.equals(String);
• It returns true if the strings are equal, false
• For e.g., String str1=“COMPUTER”; otherwise. It returns boolean type of value.
• String str2=“COMPUTER”;
• Syntax: boolean
• boolean p=str1.equals(str2);
variable=stringvariable.equalsIgnoreCase(String);
• Here the value of p will be true.
• For e.g., String str1=“computer”;
• String str1=“computer”;
• String str2=“COMPUTER”; • String str2=“COMPUTER”;
• boolean p=str1.equals(str2); • boolean p=str1.equalsIgnoreCase(str2);
• Here the value of p will be false.
• Here the value of p will be true
10 10
MARUTI COMPUTER CLASSES
Click to edit
Methods of String
Masterclass
title style
trim(): endswith():
• This function is used to remove leading and • This function returns true if the string ends with
trailing blanks from the string. Other a particular string and false
• blanks, which are available in between the • otherwise. It returns boolean type of value.
words will remain unchanged. It returns
• Syntax: boolean
• String type of value. variable=stringvariable.endswith(String);
• Syntax: String variable=stringvariable.trim(); • For e.g., String str1=“COMPUTER IS FUN”;
• For e.g., String str =“ COMPUTER ”; • String str2=“FUN”;
• String p=str.trim(); • boolean p=str1.endswith(str2);
• Here the value of p will be true.
11 11
MARUTI COMPUTER CLASSES
Click to edit
Methods of String
Masterclass
title style
startswith():
12 12
MARUTI COMPUTER CLASSES
Click to editfrom
Conversion Master
String
titletostyle
primitive types:
String to Integer data type:
Name of method: parseInt() or valueOf()
int n; String s= “24”;
n=Integer.parseInt(s);
or
n=Integer.valueOf(s);
13 13
MARUTI COMPUTER CLASSES
Click to editfrom
Conversion Master
String
titletostyle
primitive types:
String to float data type:
Name of method: parseFloat() or valueOf()
float n; String s= “24.35”;
n=Float.parseFloat(s);
or
n=Float.valueOf(s);
String to double data type:
Name of method: parseDouble() or valueOf()
double n; String s= “24.35”;
n=Double.parseDouble(s);
or
n=Double.valueOf(s);
14 14
MARUTI COMPUTER CLASSES
Click to editfrom
Conversion Master
Primitive
title style
data type to String:
Integer data type to String type:
Name of method: toString()
int n=24; String s;
s=Integer.toString(n);
long data type to String type:
Name of method: toString()
long n=24; String s;
s=Long.toString(n);
float data type to String type:
Name of method: toString()
float n=24.35f; String s;
s=Float.toString(n);
double data type to String type:
Name of method: toString()
double n=24.35; String s;
s=Double.toString(n);
15 15
MARUTI COMPUTER CLASSES
Click to edit
Character Functions:
Master title style
Character.isLetter(): Character.isDigit():
• This function is used to check whether a given • This function is used to check whether a given
argument is argument is
• letter(alphabet) or not. It returns true if it is • digit or not. It returns true if it is digit and false
letter and false otherwise. It returns boolean otherwise. It returns boolean type of
• type of value. • value.
• Syntax: boolean • Syntax: boolean
variable=Character.isLetter(character); variable=Character.isDigit(character);
• For e.g., boolean p=Character.isLetter(‘C’); • For e.g., boolean p=Character.isDigit(‘1’);
• The value of p will be true. • The value of p will be true.
• boolean p=Character.isLetter(‘*’); • boolean p=Character.isDigit(‘*’);
• The value of p will be false. • The value of p will be false.
16 16
MARUTI COMPUTER CLASSES
Click to edit
Character Functions:
Master title style
Character.isLetterOrDigit(): Character.isWhitespace():
• This function is used to check whether a given • This function is used to check whether a given
argument argument is
• is letter or digit. It returns true if it is letter or digit • white space(blank) or not. It returns true if it is
and false otherwise. It returns boolean
white space(blank) and false otherwise. It
• type of value.
• returns boolean type of value.
• Syntax: boolean
variable=Character.isLetterOrDigit(character); • Syntax: boolean
• For e.g., boolean p=Character.isLetterOrDigit(‘1’); variable=Character.isWhitespace(character);
• The value of p will be true. • For e.g., boolean p=Character.isWhitespace(‘
• boolean p=Character.isLetterOrDigit(‘a’); ’);
• The value of p will be true. • The value of p will be true.
• boolean p=Character.isLetterOrDigit(‘*’); • boolean p=Character.isWhitespace(‘*’);
• The value of p will be false. • The value of p will be false.
17 17
MARUTI COMPUTER CLASSES
Click to edit
Character Functions:
Master title style
Character.isUpperCase(): Character.isLowerCase():
• This function is used to check whether a given • This function is used to check whether a given
argument is argument is
• an upper case letter or not. It returns true if it is • an lower case letter or not. It returns true if it is
upper case letter and false otherwise. It upper case letter and false otherwise. It
• returns boolean type of value. • returns boolean type of value.
• Syntax: boolean • Syntax: boolean
variable=Character.isUpperCase(character); variable=Character.isLowerCase(character);
• For e.g., boolean p=Character.isUpperCase(‘ • For e.g., boolean p=Character.isLowerCase(‘
A’); a’);
• The value of p will be true. • The value of p will be true. boolean
p=Character.isLowerCase(‘A’);
• boolean p=Character.isUpperCase(‘a’);
• The value of p will be false..
• The value of p will be false.
18 18
MARUTI COMPUTER CLASSES
Click to edit Master title style
Thank You
19