SlideShare a Scribd company logo
3
Most read
7
Most read
12
Most read
String Class :
 String represent a sequence of character.
 The String class belongs to the java.lang package, which
does not require an import statement.
 Sequence of character is represented in java is by using
a character array
char charArray[ ] = new char[4]
charArray[0]= ‘J’;
charArray[1]= ‘a’;
charArray[2]= ‘v’;
charArray[3]= ‘a’;
1 RAJESHREE KHANDE
String Class
X[0]
X[1]
X[2]
X[2][2]
X[1][3]
X[0][1]
Variable Size Array
2RAJESHREE KHANDE
String Class
 Character array not good enough to support the range
of operations we may like to perform on the string.
 In java String are class object & implemented using
two classes namely Sting & StringBuffer.
 A java string is an instantiated object of String Class.
 Java string is not character array & is not NULL
terminated.
 Strings are Immutable.
3 RAJESHREE KHANDE
String Class
String stringName = new String(“string”);
e.g.
String name = new String(“Rajeshree”);
To find the length of String length method of
string class is used.
int m = name.length();
4 RAJESHREE KHANDE
String Array
 We can also create & use array that contain
strings.
String itemArray[] = new String[3];
We can assign the element to this array by using
for loop.
5 RAJESHREE KHANDE
Literal Strings
 String literals are anonymous objects of the String class.
 are defined by enclosing text in double quotes. “This is a
literal String”
 don’t have to be constructed.
 can be assigned to String variables.
 can be passed to methods and constructors as
parameters.
6 RAJESHREE KHANDE
Literal String examples
String name = “Robert”;
//calling a method on a literal String
char firstInitial=“Robert”.charAt(0);
//calling a method on a String variable
char firstInitial = name.charAt(0);
7 RAJESHREE KHANDE
Empty Strings
 An empty String has no characters. It’s
length is 0.
 Not the same as an uninitialized String.
String word1 = “ ”;
String word2 = new String();
private String errorMsg; errorMsg
is null
Empty strings
8 RAJESHREE KHANDE
String Methods : Changing Case
 toLowerCase : Convert String to lowercase
 toUpperCase : Convert String to uppercase
String word2 = word1.toUpperCase();
String word3 = word1.toLowerCase();
 returns a new string formed from word1 by converting its
characters to upper (lower) case
String word1 = “HeLLo”;
String word2 = word1.toUpperCase(); //”HELLO”
String word3 = word1.toLowerCase(); //”hello”
//word1 is still “HeLLo“
9 RAJESHREE KHANDE
String Methods : Changing Case
word1
remains
unchanged
 Example: to “convert” word1 to upper case, replace
the reference with a new reference.
 A common bug:
word1 = word1.toUpperCase();
word1.toUpperCase();
10 RAJESHREE KHANDE
String Methods : length, chatAt
 ”Problem".length();
 ”Window".charAt (2);
int length();
char charAt(i);
 Returns the number of characters in
the string
 Returns the char at position i.
7
’n'
Returns:
Character positions in strings are numbered
starting from 0 – just like arrays.
11 RAJESHREE KHANDE
String Methods : substring
television
i k
“lev"
“mutable"
"" (empty string)
”television".substring (2,5);
“immutable".substring (2);
“bob".substring (9);
television
i
 String subs = word.substring (i, k);
 returns the substring of chars in positions
from i to k-1
 String subs = word.substring (i);
 returns the substring from the i-th char to
the end
Returns a new String by copying characters from an existing String.
12 RAJESHREE KHANDE
String Methods : concat
String word1 = “re”, word2 = “think”; word3 = “ing”;
int num = 2;
String result = word1 + word2;
//concatenates word1 and word2 “rethink“
String result = word1.concat (word2);
//the same as word1 + word2 “rethink“
result += word3;
//concatenates word3 to result “rethinking”
result += num; //converts num to String
//and concatenates it to result “rethinking2”
13 RAJESHREE KHANDE
String Methods : indexOf
String name =“President George Washington";
name.indexOf (‘P'); 0
name.indexOf (‘e'); 2
name.indexOf (“George"); 10
name.indexOf (‘e', 3); 6
name.indexOf (“Bob"); -1
name.lastIndexOf (‘e'); 15
0 2 6 10 15
(starts searching
at position 3)
(not found)
Returns:
14 RAJESHREE KHANDE
String Methods : Equality
boolean b = word1.equals(word2);
 returns true if the string word1 is equal to word2
boolean b = word1.equalsIgnoreCase(word2);
 returns true if the string word1 matches word2,
case
b = “Raiders”.equals(“Raiders”);//true
b = “Raiders”.equals(“raiders”);//false
b = “Raiders”.equalsIgnoreCase(“raiders”);//true
if(“team”.equalsIgnoreCase(“raiders”))
System.out.println(“Go You ” + “team”);
15 RAJESHREE KHANDE
String Methods : Comparisons
S1.compareTo(S2) : Return negative if S1<S2, positive if S1>S2,
zero if s1 equal to S2
int diff = word1.compareTo(word2);
int diff = word1.compareToIgnoreCase(word2);
Usually programmers don’t care what the numerical
“difference” of word1 - word2 is, just whether the difference
is negative (word1 comes before word2), zero (word1 and
word2 are equal) or positive (word1 comes after word2).
Often used in conditional statements.
if(word1.compareTo(word2) > 0){
//word1 comes after word2…
}
16 RAJESHREE KHANDE
Comparisons :Eaxmples
//negative differences
diff = “apple”.compareTo(“berry”); //a before b
diff = “Zebra”.compareTo(“apple”); //Z before a
diff = “dig”.compareTo(“dug”); //i before u
diff = “dig”.compareTo(“digs”); //dig is shorter
//zero differences
diff = “apple”.compareTo(“apple”); //equal
diff = “dig”.compareToIgnoreCase(“DIG”);/ /equal
//positive differences
diff = “berry”.compareTo(“apple”); //b after a
diff = “apple”.compareTo(“Apple”); //a after A
diff = “BIT”.compareTo(“BIG”); //T after G
diff = “huge”.compareTo(“hug”); //huge is longer
17 RAJESHREE KHANDE
String Methods : trim
s2 = s1.trim();
Removes white spaces at the beginning & at the end of string s1
 String word2 = word1.trim ();
 returns a new string formed from word1 by removing white
space at both end does not affect whites space in the middle
String word1 = “ Hi Bob ”;
String word2 = word1.trim();
//word2 is “Hi Bob” – no spaces on either end
//word1 is still “ Hi Bob “ – with spaces
18 RAJESHREE KHANDE
String Methods : replace
 String word2 = word1.replace(‘x’, ‘y’);
returns a new string formed from word1 by replacing
all occurrences of x with y
String word1 = “rare“;
String word2 = “rare“.replace(‘r’, ‘d’);
//word2 is “dade”, but word1 is still “rare“
19 RAJESHREE KHANDE
Numbers to Strings
Three ways to convert a number into a string:
1. String s = "" + num;
2. String s = Integer.toString (i);
String s = Double.toString (d);
3. String s = String.valueOf (num);
Integer and Double
are “wrapper” classes
from java.lang that
represent numbers as
objects. They also
provide useful static
methods.
s = String.valueOf(123) ; //”123”
s = “” + 123;//”123”
s = Integer.toString(123);
//”123”
s = Double.toString(3.14);
//”3.14”
20 RAJESHREE KHANDE
StringBuffer class
 StringBuffer is mutable object.
 The StringBuffer class is an alternative to the String
class.
 StringBuffer is more flexible than String. You can add,
insert, or append new contents into a string buffer.
21 RAJESHREE KHANDE
StringBuffer : Methods
Method Description
s1.setCharAt(n, x) Modifies the nth char to x of string s1
s1.append(s2) Append the string s2 to s1 at the end
s1.insert(n,s2) Insert the string s2 at the position n of string s1
s1.setLength(n) Set the length of the string s1 to n
If n < s1,length() s1 is truncated.
If n > s1.length zeros are added to s1
s1.deleteCharAt(n) Delete nth character from string s1
22RAJESHREE KHANDE

More Related Content

PPT
Java static keyword
Lovely Professional University
 
PPTX
Inner classes in java
PhD Research Scholar
 
PPTX
Strings in Java
Abhilash Nair
 
DOCX
Java loops
ricardovigan
 
PDF
Built in exceptions
TharuniDiddekunta
 
PPTX
this keyword in Java.pptx
ParvizMirzayev2
 
PPS
String and string buffer
kamal kotecha
 
PDF
Arrays in Java
Naz Abdalla
 
Java static keyword
Lovely Professional University
 
Inner classes in java
PhD Research Scholar
 
Strings in Java
Abhilash Nair
 
Java loops
ricardovigan
 
Built in exceptions
TharuniDiddekunta
 
this keyword in Java.pptx
ParvizMirzayev2
 
String and string buffer
kamal kotecha
 
Arrays in Java
Naz Abdalla
 

What's hot (20)

PDF
Java I/o streams
Hamid Ghorbani
 
PPTX
Java abstract class & abstract methods
Shubham Dwivedi
 
PPTX
Control Statements in Java
Niloy Saha
 
PPTX
Classes objects in java
Madishetty Prathibha
 
PPTX
Data types in java
HarshitaAshwani
 
PPTX
Inheritance in java
Tech_MX
 
PPT
Java Collections Framework
Sony India Software Center
 
PPT
Generics in java
suraj pandey
 
PPTX
Inheritance In Java
Manish Sahu
 
PPT
Java Input Output and File Handling
Sunil OS
 
PDF
Exception handling in plsql
Arun Sial
 
PPTX
Access Modifier.pptx
Margaret Mary
 
PPTX
Object Oriented Programing JAVA presentaion
Pritom Chaki
 
PDF
Java conditional statements
Kuppusamy P
 
PPT
Method overriding
Azaz Maverick
 
PPTX
Java Data Types
Spotle.ai
 
PPTX
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
PPTX
Java Stack Data Structure.pptx
vishal choudhary
 
PDF
Generics and collections in Java
Gurpreet singh
 
ODP
OOP java
xball977
 
Java I/o streams
Hamid Ghorbani
 
Java abstract class & abstract methods
Shubham Dwivedi
 
Control Statements in Java
Niloy Saha
 
Classes objects in java
Madishetty Prathibha
 
Data types in java
HarshitaAshwani
 
Inheritance in java
Tech_MX
 
Java Collections Framework
Sony India Software Center
 
Generics in java
suraj pandey
 
Inheritance In Java
Manish Sahu
 
Java Input Output and File Handling
Sunil OS
 
Exception handling in plsql
Arun Sial
 
Access Modifier.pptx
Margaret Mary
 
Object Oriented Programing JAVA presentaion
Pritom Chaki
 
Java conditional statements
Kuppusamy P
 
Method overriding
Azaz Maverick
 
Java Data Types
Spotle.ai
 
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
Java Stack Data Structure.pptx
vishal choudhary
 
Generics and collections in Java
Gurpreet singh
 
OOP java
xball977
 
Ad

Similar to Java String class (20)

PPT
Strings in javamnjn ijnjun oinoin oinoi .ppt
ShahidSultan24
 
PPT
Strings.ppt
SanthiyaAK
 
PPTX
L13 string handling(string class)
teach4uin
 
PPTX
String Method.pptx
Dreime Estandarte
 
PPTX
Java string handling
GaneshKumarKanthiah
 
PPTX
Introduction to Java Strings, By Kavita Ganesan
Kavita Ganesan
 
PDF
java.lang.String Class
Vipul Verma
 
PPTX
DOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptx
meganath16032003
 
PDF
Module-1 Strings Handling.ppt.pdf
learnEnglish51
 
PPTX
Java String Handling
Infoviaan Technologies
 
PDF
Strings in java
Kuppusamy P
 
PPT
Charcater and Strings.ppt Charcater and Strings.ppt
mulualem37
 
PPT
Java căn bản - Chapter9
Vince Vo
 
PDF
11-ch04-3-strings.pdf
AndreaBatholomeo
 
PPT
Chapter 9 - Characters and Strings
Eduardo Bergavera
 
PPT
Eo gaddis java_chapter_08_5e
Gina Bullock
 
PPT
Eo gaddis java_chapter_08_5e
Gina Bullock
 
PPT
Java Strings methods and operations.ppt
JyothiAmpally
 
PPTX
String functions
ssuser93a21b
 
PPTX
String.pptxihugyftgrfxdf bnjklihugyfthfgxvhbjihugyfthcgxcgvjhbkipoihougyfctgf...
minifriendofyou
 
Strings in javamnjn ijnjun oinoin oinoi .ppt
ShahidSultan24
 
Strings.ppt
SanthiyaAK
 
L13 string handling(string class)
teach4uin
 
String Method.pptx
Dreime Estandarte
 
Java string handling
GaneshKumarKanthiah
 
Introduction to Java Strings, By Kavita Ganesan
Kavita Ganesan
 
java.lang.String Class
Vipul Verma
 
DOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptx
meganath16032003
 
Module-1 Strings Handling.ppt.pdf
learnEnglish51
 
Java String Handling
Infoviaan Technologies
 
Strings in java
Kuppusamy P
 
Charcater and Strings.ppt Charcater and Strings.ppt
mulualem37
 
Java căn bản - Chapter9
Vince Vo
 
11-ch04-3-strings.pdf
AndreaBatholomeo
 
Chapter 9 - Characters and Strings
Eduardo Bergavera
 
Eo gaddis java_chapter_08_5e
Gina Bullock
 
Eo gaddis java_chapter_08_5e
Gina Bullock
 
Java Strings methods and operations.ppt
JyothiAmpally
 
String functions
ssuser93a21b
 
String.pptxihugyftgrfxdf bnjklihugyfthfgxvhbjihugyfthcgxcgvjhbkipoihougyfctgf...
minifriendofyou
 
Ad

More from DrRajeshreeKhande (20)

PPTX
.NET F# Inheritance and operator overloading
DrRajeshreeKhande
 
PPTX
Exception Handling in .NET F#
DrRajeshreeKhande
 
PPTX
.NET F# Events
DrRajeshreeKhande
 
PPTX
.NET F# Class constructor
DrRajeshreeKhande
 
PPTX
.NET F# Abstract class interface
DrRajeshreeKhande
 
PPTX
.Net F# Generic class
DrRajeshreeKhande
 
PPTX
F# Console class
DrRajeshreeKhande
 
PPTX
.net F# mutable dictionay
DrRajeshreeKhande
 
PPTX
F sharp lists & dictionary
DrRajeshreeKhande
 
PPTX
F# array searching
DrRajeshreeKhande
 
PPTX
Net (f#) array
DrRajeshreeKhande
 
PPTX
.Net (F # ) Records, lists
DrRajeshreeKhande
 
PPT
MS Office for Beginners
DrRajeshreeKhande
 
PPSX
Java Multi-threading programming
DrRajeshreeKhande
 
PPTX
JAVA AWT components
DrRajeshreeKhande
 
PPSX
Dr. Rajeshree Khande :Introduction to Java AWT
DrRajeshreeKhande
 
PPSX
Dr. Rajeshree Khande Java Interactive input
DrRajeshreeKhande
 
PPSX
Dr. Rajeshree Khande :Intoduction to java
DrRajeshreeKhande
 
PPSX
Java Exceptions Handling
DrRajeshreeKhande
 
PPSX
Dr. Rajeshree Khande : Java Basics
DrRajeshreeKhande
 
.NET F# Inheritance and operator overloading
DrRajeshreeKhande
 
Exception Handling in .NET F#
DrRajeshreeKhande
 
.NET F# Events
DrRajeshreeKhande
 
.NET F# Class constructor
DrRajeshreeKhande
 
.NET F# Abstract class interface
DrRajeshreeKhande
 
.Net F# Generic class
DrRajeshreeKhande
 
F# Console class
DrRajeshreeKhande
 
.net F# mutable dictionay
DrRajeshreeKhande
 
F sharp lists & dictionary
DrRajeshreeKhande
 
F# array searching
DrRajeshreeKhande
 
Net (f#) array
DrRajeshreeKhande
 
.Net (F # ) Records, lists
DrRajeshreeKhande
 
MS Office for Beginners
DrRajeshreeKhande
 
Java Multi-threading programming
DrRajeshreeKhande
 
JAVA AWT components
DrRajeshreeKhande
 
Dr. Rajeshree Khande :Introduction to Java AWT
DrRajeshreeKhande
 
Dr. Rajeshree Khande Java Interactive input
DrRajeshreeKhande
 
Dr. Rajeshree Khande :Intoduction to java
DrRajeshreeKhande
 
Java Exceptions Handling
DrRajeshreeKhande
 
Dr. Rajeshree Khande : Java Basics
DrRajeshreeKhande
 

Recently uploaded (20)

DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PDF
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PDF
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
PPTX
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
PPTX
How to Manage Global Discount in Odoo 18 POS
Celine George
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
PDF
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
PPTX
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PPTX
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
PDF
Landforms and landscapes data surprise preview
jpinnuck
 
PPTX
Strengthening open access through collaboration: building connections with OP...
Jisc
 
PDF
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 
PDF
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PPTX
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Types of Literary Text: Poetry and Prose
kaelandreabibit
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
7.Particulate-Nature-of-Matter.ppt/8th class science curiosity/by k sandeep s...
Sandeep Swamy
 
An introduction to Prepositions for beginners.pptx
drsiddhantnagine
 
How to Manage Global Discount in Odoo 18 POS
Celine George
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Measures_of_location_-_Averages_and__percentiles_by_DR SURYA K.pptx
Surya Ganesh
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
ACUTE NASOPHARYNGITIS. pptx
AneetaSharma15
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
vedic maths in python:unleasing ancient wisdom with modern code
mistrymuskan14
 
Landforms and landscapes data surprise preview
jpinnuck
 
Strengthening open access through collaboration: building connections with OP...
Jisc
 
Wings of Fire Book by Dr. A.P.J Abdul Kalam Full PDF
hetalvaishnav93
 
The Minister of Tourism, Culture and Creative Arts, Abla Dzifa Gomashie has e...
nservice241
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
Odoo 18 Sales_ Managing Quotation Validity
Celine George
 

Java String class

  • 1. String Class :  String represent a sequence of character.  The String class belongs to the java.lang package, which does not require an import statement.  Sequence of character is represented in java is by using a character array char charArray[ ] = new char[4] charArray[0]= ‘J’; charArray[1]= ‘a’; charArray[2]= ‘v’; charArray[3]= ‘a’; 1 RAJESHREE KHANDE
  • 3. String Class  Character array not good enough to support the range of operations we may like to perform on the string.  In java String are class object & implemented using two classes namely Sting & StringBuffer.  A java string is an instantiated object of String Class.  Java string is not character array & is not NULL terminated.  Strings are Immutable. 3 RAJESHREE KHANDE
  • 4. String Class String stringName = new String(“string”); e.g. String name = new String(“Rajeshree”); To find the length of String length method of string class is used. int m = name.length(); 4 RAJESHREE KHANDE
  • 5. String Array  We can also create & use array that contain strings. String itemArray[] = new String[3]; We can assign the element to this array by using for loop. 5 RAJESHREE KHANDE
  • 6. Literal Strings  String literals are anonymous objects of the String class.  are defined by enclosing text in double quotes. “This is a literal String”  don’t have to be constructed.  can be assigned to String variables.  can be passed to methods and constructors as parameters. 6 RAJESHREE KHANDE
  • 7. Literal String examples String name = “Robert”; //calling a method on a literal String char firstInitial=“Robert”.charAt(0); //calling a method on a String variable char firstInitial = name.charAt(0); 7 RAJESHREE KHANDE
  • 8. Empty Strings  An empty String has no characters. It’s length is 0.  Not the same as an uninitialized String. String word1 = “ ”; String word2 = new String(); private String errorMsg; errorMsg is null Empty strings 8 RAJESHREE KHANDE
  • 9. String Methods : Changing Case  toLowerCase : Convert String to lowercase  toUpperCase : Convert String to uppercase String word2 = word1.toUpperCase(); String word3 = word1.toLowerCase();  returns a new string formed from word1 by converting its characters to upper (lower) case String word1 = “HeLLo”; String word2 = word1.toUpperCase(); //”HELLO” String word3 = word1.toLowerCase(); //”hello” //word1 is still “HeLLo“ 9 RAJESHREE KHANDE
  • 10. String Methods : Changing Case word1 remains unchanged  Example: to “convert” word1 to upper case, replace the reference with a new reference.  A common bug: word1 = word1.toUpperCase(); word1.toUpperCase(); 10 RAJESHREE KHANDE
  • 11. String Methods : length, chatAt  ”Problem".length();  ”Window".charAt (2); int length(); char charAt(i);  Returns the number of characters in the string  Returns the char at position i. 7 ’n' Returns: Character positions in strings are numbered starting from 0 – just like arrays. 11 RAJESHREE KHANDE
  • 12. String Methods : substring television i k “lev" “mutable" "" (empty string) ”television".substring (2,5); “immutable".substring (2); “bob".substring (9); television i  String subs = word.substring (i, k);  returns the substring of chars in positions from i to k-1  String subs = word.substring (i);  returns the substring from the i-th char to the end Returns a new String by copying characters from an existing String. 12 RAJESHREE KHANDE
  • 13. String Methods : concat String word1 = “re”, word2 = “think”; word3 = “ing”; int num = 2; String result = word1 + word2; //concatenates word1 and word2 “rethink“ String result = word1.concat (word2); //the same as word1 + word2 “rethink“ result += word3; //concatenates word3 to result “rethinking” result += num; //converts num to String //and concatenates it to result “rethinking2” 13 RAJESHREE KHANDE
  • 14. String Methods : indexOf String name =“President George Washington"; name.indexOf (‘P'); 0 name.indexOf (‘e'); 2 name.indexOf (“George"); 10 name.indexOf (‘e', 3); 6 name.indexOf (“Bob"); -1 name.lastIndexOf (‘e'); 15 0 2 6 10 15 (starts searching at position 3) (not found) Returns: 14 RAJESHREE KHANDE
  • 15. String Methods : Equality boolean b = word1.equals(word2);  returns true if the string word1 is equal to word2 boolean b = word1.equalsIgnoreCase(word2);  returns true if the string word1 matches word2, case b = “Raiders”.equals(“Raiders”);//true b = “Raiders”.equals(“raiders”);//false b = “Raiders”.equalsIgnoreCase(“raiders”);//true if(“team”.equalsIgnoreCase(“raiders”)) System.out.println(“Go You ” + “team”); 15 RAJESHREE KHANDE
  • 16. String Methods : Comparisons S1.compareTo(S2) : Return negative if S1<S2, positive if S1>S2, zero if s1 equal to S2 int diff = word1.compareTo(word2); int diff = word1.compareToIgnoreCase(word2); Usually programmers don’t care what the numerical “difference” of word1 - word2 is, just whether the difference is negative (word1 comes before word2), zero (word1 and word2 are equal) or positive (word1 comes after word2). Often used in conditional statements. if(word1.compareTo(word2) > 0){ //word1 comes after word2… } 16 RAJESHREE KHANDE
  • 17. Comparisons :Eaxmples //negative differences diff = “apple”.compareTo(“berry”); //a before b diff = “Zebra”.compareTo(“apple”); //Z before a diff = “dig”.compareTo(“dug”); //i before u diff = “dig”.compareTo(“digs”); //dig is shorter //zero differences diff = “apple”.compareTo(“apple”); //equal diff = “dig”.compareToIgnoreCase(“DIG”);/ /equal //positive differences diff = “berry”.compareTo(“apple”); //b after a diff = “apple”.compareTo(“Apple”); //a after A diff = “BIT”.compareTo(“BIG”); //T after G diff = “huge”.compareTo(“hug”); //huge is longer 17 RAJESHREE KHANDE
  • 18. String Methods : trim s2 = s1.trim(); Removes white spaces at the beginning & at the end of string s1  String word2 = word1.trim ();  returns a new string formed from word1 by removing white space at both end does not affect whites space in the middle String word1 = “ Hi Bob ”; String word2 = word1.trim(); //word2 is “Hi Bob” – no spaces on either end //word1 is still “ Hi Bob “ – with spaces 18 RAJESHREE KHANDE
  • 19. String Methods : replace  String word2 = word1.replace(‘x’, ‘y’); returns a new string formed from word1 by replacing all occurrences of x with y String word1 = “rare“; String word2 = “rare“.replace(‘r’, ‘d’); //word2 is “dade”, but word1 is still “rare“ 19 RAJESHREE KHANDE
  • 20. Numbers to Strings Three ways to convert a number into a string: 1. String s = "" + num; 2. String s = Integer.toString (i); String s = Double.toString (d); 3. String s = String.valueOf (num); Integer and Double are “wrapper” classes from java.lang that represent numbers as objects. They also provide useful static methods. s = String.valueOf(123) ; //”123” s = “” + 123;//”123” s = Integer.toString(123); //”123” s = Double.toString(3.14); //”3.14” 20 RAJESHREE KHANDE
  • 21. StringBuffer class  StringBuffer is mutable object.  The StringBuffer class is an alternative to the String class.  StringBuffer is more flexible than String. You can add, insert, or append new contents into a string buffer. 21 RAJESHREE KHANDE
  • 22. StringBuffer : Methods Method Description s1.setCharAt(n, x) Modifies the nth char to x of string s1 s1.append(s2) Append the string s2 to s1 at the end s1.insert(n,s2) Insert the string s2 at the position n of string s1 s1.setLength(n) Set the length of the string s1 to n If n < s1,length() s1 is truncated. If n > s1.length zeros are added to s1 s1.deleteCharAt(n) Delete nth character from string s1 22RAJESHREE KHANDE