SlideShare a Scribd company logo
Strings
•Zero or more characters put within
double quotes("")
•Examples:
i. "2"
ii. " "
iii. ""
iv. "dgsdgs"
v. "23 kjkg ,,..."
String is the name of a class present
inside java.lang package.
String s; or String s="";
s=“Good Morning!";
How to declare & initialise a string variable?
String s=“Good Morning!";
String s=new String(“Good Morning!";);
String p=“Good Morning!";
String s= new String(p);
String s="";
s=sc.next(); - to accept a word from
the user
s=sc.nextLine(); - to accept more than
one word from the user
String s= "Good begets Good";
Good begets Good
0 6
7
8
9
10
11
2 4
31 5 13 15
1412
length() – returns the no: of characters in the string.
Example:
String s=“Good begets Good”;
System.out.println(s.length());
Output:
Please Note:
We always say the positions in terms of index. Index always
starts from Zero, just like arrays.
Return type is intlength()
16
indexOf() Return type is int
indexOf() – returns the index of the first occurrence
of a particular character.
Examples:
s.indexOf('b') = 5
s.indexOf('g') = 7
s.indexOf('G') = ?
s.indexOf('e') = ?
String s= "Good begets Good";
s.indexOf('G') = 0
s.indexOf('e') = 6
s.indexOf('z')=?
s.indexOf('z')=-1;
Please Note:
If the character is not present in
the string, it always returns -1.
lastIndexOf() Return type is int
lastIndexOf() – returns the index of the last
occurrence of a particular character.
Examples:
String s= "Good begets Good";
s.lastIndexOf('b') = 5
s. lastIndexOf('g') = 7
s. lastIndexOf('G') = 12
s. lastIndexOf('e') = 8
Examples:
Examples:
1.System.out.println(s.charAt(9));
Output:
2.System.out.printl(“p”+s.charAt(4) +”q”);
Output:
Return type is charcharAt()
charAt() – returns the character at the given index.
t
p q
substring()– returns a part of the string.
substring() Return type is String
s.substring(i) - returns the string from
the index 'i' onwards.
Example:
System.out.println(s.substring(9));
Output: ts Good
Remember that substring() does not
take the character specified by the last
index.
Example:
System.out.println(s.substring(5,10));
Output:
Here 's' is not included because the last index 10 is
ignored.
s.substring(i,j) - returns the string from
index 'i' to 'j-1‘.
beget
trim()
String s=“ Welcome to Java “;
System.out.println(“Hello”+s.trim()+”World”);
Output:
String s=“ Welcome to Java “;
System.out.println(“Hello”+s+”World”);
Output:
trim() – removes all spaces at the beginning and end
of the string.
Hello Welcome to Java World
HelloWelcome to JavaWorld
Return type is String
toLowerCase()
Please Note:
Simply writing s.toLowerCase() will not change the original string. If
you want to change the original string, the statement is:
s = s.toLowerCase();
String s=“GooD MorninG“;
System.out.println(s.toLowerCase());
Output:
toLowerCase() – converts the characters in the
string into lowercase letters.
good morning
Return type is String
toUpperCase()
Please Note:
Simply writing s.toUpperCase() will not change the original string. If
you want to change the original string, the statement is:
s = s.toUpperCase();
String s=“GooD MorninG“;
System.out.println(s.toUpperCase());
Output:
toUpperCase() – converts the characters in the
string into uppercase letters.
GOOD MORNING
Return type is String
concat()
Please Note:
String s3= s1+s2;
The above statement also joins the two strings.
System.out.println(s3);
Output: GoodMorning
String s1=“Good”;
String s2=“Morning”;
System.out.println(s1.concat(s2));
Output:
concat() – joins two strings.
GoodMorning
Return type is String
replace()
Please Note:
s=s.replace(‘a’,’z’);
String s=“abcabcabcabcabcabc”;
System.out.println(s.replace(‘a’,’z’));
Output:
replace() – replaces all the occurrences of a
particular character with another character.
zbczbczbczbczbczbc
Return type is String
startsWith()
Please Note:
In the above example, C and c are different in case. So
the output is false.
String s1=“Computer”;
String s2=“com”;
System.out.println(s1.startsWith(s2));
OR
System.out.println(“Computer”.startsWith(“computer”));
Output:
startsWith() – checks whether the string starts with
a particular string. If yes, it returns true; otherwise
it returns false.
false
Return type is boolean
endsWith()
Please Note:
In the above example, C and c are different in case. So
the output is false.
String s1=“Computer”;
String s2=“ter”;
System.out.println(s1.endsWith(s2));
Output:
endsWith() – checks whether the string ends with a
particular string. If yes, it returns true; otherwise it
returns false.
true
Return type is boolean
valueOf ()
Please Note:
Using valueOf(), we can convert int, long, boolean,
float, double etc. (all types) to string.
int x=10;
String s=String.valueOf(10);
System.out.println(x+9);
System.out.println(s+9);
Output:
valueOf() – converts different types of values into
string.
19
109
Return type is String
equals()
Please Note:
equals() is case sensitive.
String s1=“Computer”;
String s2=“ComputerR”;
System.out.println(s1.equals(s2));
OR
System.out.println(“Computer”.equals(“ComputeR”));
Output:
eauals() – checks whether two strings are same or
not. If yes, it returns true; otherwise it returns false.
false
Return type is boolean
equalsIgnoreCase()
String s1=“Computer”;
String s2=“ComputerR”;
System.out.println(s1.equals(s2));
Output:
eaualsIgnoreCase() – checks whether two strings are
same or not, irrespective of cases. If yes, it returns
true; otherwise it returns false.
true
Return type is boolean
compareTo()
 compareTo() – compares two strings
lexicographically.
 Each character of both the strings is converted
into a Unicode value for comparison.
 If both strings are equal, it returns 0; else it
returns a positive or negative value.
Return type is int
String s1=“abc”;
String s2=“abc”;
String s1=“abc”;
String s2=“abc”;
1. First it checks 97-97. It is zero.
2. Now it checks 98-98. It is zero.
3. Now it checks 98-98. It is zero.
4. Since all the values are zero, the strings are
equal. Hence it returns 0.
String s1=“abd”;
String s2=“abc”;
String s1=“abd”;
String s2=“abc”;
1. First it checks 97-97. It is zero.
2. Now it checks 98-98. It is zero.
3. Now it checks 100-99. It is +1.
4. Since we get +1, we can say that the first string is
lexicographically greater than the second string.
Non-zero value indicates that the two strings are
not the same.
String s1=“abc”;
String s2=“abd”;
String s1=“abc”;
String s2=“abd”;
1. First it checks 97-97. It is zero.
2. Now it checks 98-98. It is zero.
3. Now it checks 99-100. It is -1.
4. Since we get +1, we can say that the second
string is lexicographically greater than the first
string. Non-zero value indicates that the two
strings are not the same.
String s1=“abc”;
String s2=“agd”;
String s1=“abc”;
String s2=“agd”;
1. First it checks 97-97. It is zero.
2. Now it checks 98-103. It is -5.
3. If the result is not zero, it stops further
comparison and returns the first difference got.
Here it return -5.
String s1=“Abc”;
String s2=“abc”;
String s1=“Abc”;
String s2=“abc”;
1. First it checks 65-97. It is -32.
2. Since it is not zero, it stops further comaprison.
3. compareTo() is case sensitive.
String s1=“abcdef”;
String s2=“abc”;
String s1=“abcdef”;
String s2=“abc”;
1. First it checks 97-97. It is zero.
2. Now it checks 98-98. It is zero.
3. Now it checks 98-98. It is zero.
4. The second string ends, but 3 more characters left in the first
string.
5. It returns the number of extra characters . Here it is +3 because
the first string is having more characters.
String s1=“abc”;
String s2=“abcdef”;
String s1=“abcd”;
String s2=“abcdef”;
1. First it checks 97-97. It is zero.
2. Now it checks 98-98. It is zero.
3. Now it checks 98-98. It is zero.
4. The second string ends, but 3 more characters left in the second
string.
5. It returns the number of extra characters . Here it is -3 because
the second string is having more characters.
compareToIgnoreCase()
 compareToIgnoreCase() – compares two strings
lexicographically, irrespective of the cases.
Return type is int
String s1=“Abc”;
String s2=“abc”;
String s1=“Abc”;
String s2=“abc”;
1. s1.compareTo() returns -32 as the value.
2. s1.compareToIgnoreCase() returns 0 as the value.
3. To find s1.compareToIgnoreCase(), we can either take
the Unicode value of lower case letters for both
strings or upper case letters for both strings .
4. In the above example, either 65-65 or 97-97 can be
taken.
String functions

More Related Content

PPTX
Java strings
PDF
Rustlabs Quick Start
PPTX
PPTX
L13 string handling(string class)
PDF
Introducing Swift v2.1
PDF
Ruby Gotchas
PPT
PPTX
L14 string handling(string buffer class)
Java strings
Rustlabs Quick Start
L13 string handling(string class)
Introducing Swift v2.1
Ruby Gotchas
L14 string handling(string buffer class)

What's hot (19)

PPTX
Operator precedance parsing
PDF
Python strings
PDF
Strings in python
PPT
Regular Expression
PPTX
Chapter 14 strings
PPT
strings
PDF
Strings in Python
PDF
Quick python reference
PPTX
Ruby Basics
PPT
Chapter 7 String
PPT
Javascript
PPT
Strings
PDF
Think sharp, write swift
PDF
3 kotlin vs. java- what kotlin has that java does not
PPSX
Java String class
PDF
non-strict functions, bottom and scala by-name parameters
PPTX
Computer programming 2 Lesson 10
PDF
Swift Study #3
PPT
Strings Arrays
Operator precedance parsing
Python strings
Strings in python
Regular Expression
Chapter 14 strings
strings
Strings in Python
Quick python reference
Ruby Basics
Chapter 7 String
Javascript
Strings
Think sharp, write swift
3 kotlin vs. java- what kotlin has that java does not
Java String class
non-strict functions, bottom and scala by-name parameters
Computer programming 2 Lesson 10
Swift Study #3
Strings Arrays
Ad

Similar to String functions (20)

PPT
Strings.ppt
PPTX
10619416141061941614.106194161fff4..pptx
PPTX
13 Strings and Text Processing
PPT
07slide
PPT
Strings in javamnjn ijnjun oinoin oinoi .ppt
PDF
stringsinpython-181122100212.pdf
PPTX
Strings CPU GTU
PDF
C Strings oops with c++ ppt or pdf can be done.pdf
PPS
String and string buffer
PPTX
String in c programming
PPTX
Slide -231, Math-1151, lecture-15,Chapter, Chapter 2.2,2.3, 5.1,5.2,5.6.pptx
PPTX
Java String Handling
PDF
Learn Java Part 6
PPT
C++ Strings.ppt
PPTX
L-13 part2-string handling.pptx
PDF
Java script objects 2
 
PPTX
Parenthesization.pptx
PPTX
Detailed description of Strings in Python
PPTX
Java string handling
Strings.ppt
10619416141061941614.106194161fff4..pptx
13 Strings and Text Processing
07slide
Strings in javamnjn ijnjun oinoin oinoi .ppt
stringsinpython-181122100212.pdf
Strings CPU GTU
C Strings oops with c++ ppt or pdf can be done.pdf
String and string buffer
String in c programming
Slide -231, Math-1151, lecture-15,Chapter, Chapter 2.2,2.3, 5.1,5.2,5.6.pptx
Java String Handling
Learn Java Part 6
C++ Strings.ppt
L-13 part2-string handling.pptx
Java script objects 2
 
Parenthesization.pptx
Detailed description of Strings in Python
Java string handling
Ad

Recently uploaded (20)

PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Cell Types and Its function , kingdom of life
PPTX
Lesson notes of climatology university.
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
DOC
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
What if we spent less time fighting change, and more time building what’s rig...
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
RMMM.pdf make it easy to upload and study
PDF
LDMMIA Reiki Yoga Finals Review Spring Summer
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PDF
Yogi Goddess Pres Conference Studio Updates
PDF
Classroom Observation Tools for Teachers
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
01-Introduction-to-Information-Management.pdf
PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
STATICS OF THE RIGID BODIES Hibbelers.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Cell Types and Its function , kingdom of life
Lesson notes of climatology university.
Final Presentation General Medicine 03-08-2024.pptx
2.FourierTransform-ShortQuestionswithAnswers.pdf
Soft-furnishing-By-Architect-A.F.M.Mohiuddin-Akhand.doc
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
UV-Visible spectroscopy..pptx UV-Visible Spectroscopy – Electronic Transition...
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
What if we spent less time fighting change, and more time building what’s rig...
A systematic review of self-coping strategies used by university students to ...
RMMM.pdf make it easy to upload and study
LDMMIA Reiki Yoga Finals Review Spring Summer
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Yogi Goddess Pres Conference Studio Updates
Classroom Observation Tools for Teachers
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
01-Introduction-to-Information-Management.pdf
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE

String functions