SlideShare a Scribd company logo
Java Strings  methods and operations.ppt
● This module introduces the participants to the string
object.
● Importance of String Objects and How to create them.
● Varioius Constructors that are available to create
Strings.
● Working with methods that are available with strings.
● Understanding Necessity of StringBuffer class.
● Working with methods of StringBuffer class.
● Introduction to StringBuilder class.
● Working with methods of StringBuilder class.
Module Overview
2
 Uderstand the concept of Strings and create string
objects.
 Understand advantages and disadvantages of Strings.
 Declare and create Strings using different types of
constructors.
 Working with Strings and its different types of
methods.
 Determine the usage of String Buffer and
StringBuilder.
Objectives
3
String: Strings are a sequence of characters, which are
implemented as objects.
Charecteristics of Strings:
• The String class is immutable,meaning once created
a String object cannot be changed.
• If any modifications is done to the existing string
object a new object is created internally.
• String objects are created by using the new keyword
and a constructor. The String class has eleven
constructors that allow you to provide the initial
value of the string using different sources, such as an
array of characters.
String
4
String Declaration
String Declaration: The most direct way to create a
string is as follows;
String greeting = "Hello world!";//String literal
Other Common Declarations:
String helloString = new String(helloArray);
char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};
Example of String Class.
public class StringDemo{
public static void main(String args[]){
char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};
String helloString = new String(helloArray);
String helloString2 = new String(“hello!!!”);
System.out.println( helloString );
}
}
Output:hello.
Methods Of String
 Accessor: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.
Syntax:
<StringOBject>.length();
Example:
String name = “java”;
int length = name.length();
Methods of String(Cont…)
Concatenating Strings:One String can be
concatenated to another string.This will produce
a new string object joining two strings.
Strings are more commonly concatenated with the
+ operator and concat method.
Syntax:
string1.concat(string2);
string1+string2
Methods of String(Cont…)
 String substring(int beginIndex)
Returns a new string that is a substring of this string.
 char[] toCharArray()
Converts this string to a new character array.
 String toLowerCase()
Converts all of the characters in this String to lower
case using the rules of the default locale.
 String replace(char oldChar, char newChar)
Returns a new string resulting from replacing all
occurrences of oldChar in this string with newChar.
 String[] split(String regex)
Splits this string around matches of the given regular
expression.
 String toString()
This object (which is already a string!) is itself returned.
 String toUpperCase()
Converts all of the characters in this String to upper case
using the rules of the default locale.
 String trim()
Returns a copy of the string, with leading and trailing
whitespace omitted.
 boolean startsWith(String prefix)
Tests if this string starts with the specified prefix.
 boolean contentEquals(StringBuffer sb)
Returns true if and only if this String represents the same
sequence of characters as the specified StringBuffer.
Methods of String(Cont…)
Working with methods using Strings
 Like other values to (parameters)methods, Strings
also can be passed to methods.
Ex:
public String printString(String s) {
System.out.println(s);
return “modified”+s;
}
String newString =printString(new
String(“Java”));//invoking an array
Necessity of StringBuffer
 String objects are immutable,meaning
modifications to the string object cannot be done
once created. If done a new String Object will be
created in memory.
 The StringBuffer and StringBuilder classes are
used when there is a necessity to make alot of
modifications to Strings of characters.
 StringBuffer and Stringbuilder can be modified
over and over again with out leaving behind alot
of new unused objects.
String Buffers Methods
 public StringBuffer append(String s)
Updates the value of the object that invoked the
method. The method takes boolean, char, int,
long, Strings etc.
 public StringBuffer reverse()
The method reverses the value of the
StringBuffer object that invoked the method.
 public delete(int start, int end)
Deletes the string starting from start index until
end index.
String Buffers Methods
 void setLength(int newLength)
Sets the length of this String buffer.
 int length()
Returns the length (character count) of this string
buffer.
 void ensureCapacity(int minimumCapacity)
Ensures that the capacity of the buffer is at least
equal to the specified minimum.
 int capacity()
Returns the current capacity of the String buffer.
String Builder Methods
 int length()
Returns the length (character count) of this string
buffer.
 void ensureCapacity(int minimumCapacity)
Ensures that the capacity of the buffer is at least
equal to the specified minimum.
 int capacity()
Returns the current capacity of the String buffer.
 reverse() :Causes this character sequence to be
replaced by the reverse of the sequence.
StringBuilder vs StringBuffer
 Below is a table that contains differences between
StringBuilder and StringBuffer.
StringBuffer StringBuilder
It is mutable It is mutable
It is
synchronized,provides
ThreadSafety.
It is non
synchronized.Provides no
ThreadSafety.
Capacity increases
automatically .
Capacity increases
automatically .
Excercise
 Create an application that will convert a given
character array to a string.
 Creat an application that will create a reverse of
a string.
 Create an application that uses StringBuffer to
concatinate the user input.
 Create an application that uses a StringBuilder
and String Buffer and insert one in another.
 Thank You
Ad

More Related Content

Similar to Java Strings methods and operations.ppt (20)

3.7_StringBuilder.pdf
3.7_StringBuilder.pdf3.7_StringBuilder.pdf
3.7_StringBuilder.pdf
Ananthi68
 
In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...
Indu32
 
StringBuffer.pptx
StringBuffer.pptxStringBuffer.pptx
StringBuffer.pptx
meenakshi pareek
 
L14 string handling(string buffer class)
L14 string handling(string buffer class)L14 string handling(string buffer class)
L14 string handling(string buffer class)
teach4uin
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita Ganesan
Kavita Ganesan
 
Strings
StringsStrings
Strings
naslin prestilda
 
Day_5.1.pptx
Day_5.1.pptxDay_5.1.pptx
Day_5.1.pptx
ishasharma835109
 
package
packagepackage
package
sweetysweety8
 
Strings power point in detail with examples
Strings power point in detail with examplesStrings power point in detail with examples
Strings power point in detail with examples
rabiyanaseer1
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
phanleson
 
Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)
Danial Virk
 
String.ppt
String.pptString.ppt
String.ppt
ajeela mushtaq
 
Fileoperations.pptx
Fileoperations.pptxFileoperations.pptx
Fileoperations.pptx
VeenaNaik23
 
Module 6 - String Manipulation.pdf
Module 6 - String Manipulation.pdfModule 6 - String Manipulation.pdf
Module 6 - String Manipulation.pdf
MegMeg17
 
DAY_1.3.pptx
DAY_1.3.pptxDAY_1.3.pptx
DAY_1.3.pptx
ishasharma835109
 
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
21CS642 Module 3 Strings PPT.pptx VI SEM CSE21CS642 Module 3 Strings PPT.pptx VI SEM CSE
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
VENKATESHBHAT25
 
String handling
String handlingString handling
String handling
IyeTech - Pakistan
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
Debasish Pratihari
 
JAVA CONCEPTS
JAVA CONCEPTS JAVA CONCEPTS
JAVA CONCEPTS
Shivam Singh
 
M C6java7
M C6java7M C6java7
M C6java7
mbruggen
 
3.7_StringBuilder.pdf
3.7_StringBuilder.pdf3.7_StringBuilder.pdf
3.7_StringBuilder.pdf
Ananthi68
 
In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...In the given example only one object will be created. Firstly JVM will not fi...
In the given example only one object will be created. Firstly JVM will not fi...
Indu32
 
L14 string handling(string buffer class)
L14 string handling(string buffer class)L14 string handling(string buffer class)
L14 string handling(string buffer class)
teach4uin
 
Introduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita GanesanIntroduction to Java Strings, By Kavita Ganesan
Introduction to Java Strings, By Kavita Ganesan
Kavita Ganesan
 
Strings power point in detail with examples
Strings power point in detail with examplesStrings power point in detail with examples
Strings power point in detail with examples
rabiyanaseer1
 
Strings Arrays
Strings ArraysStrings Arrays
Strings Arrays
phanleson
 
Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)Strings In OOP(Object oriented programming)
Strings In OOP(Object oriented programming)
Danial Virk
 
Fileoperations.pptx
Fileoperations.pptxFileoperations.pptx
Fileoperations.pptx
VeenaNaik23
 
Module 6 - String Manipulation.pdf
Module 6 - String Manipulation.pdfModule 6 - String Manipulation.pdf
Module 6 - String Manipulation.pdf
MegMeg17
 
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
21CS642 Module 3 Strings PPT.pptx VI SEM CSE21CS642 Module 3 Strings PPT.pptx VI SEM CSE
21CS642 Module 3 Strings PPT.pptx VI SEM CSE
VENKATESHBHAT25
 

More from JyothiAmpally (19)

node.js interview questions and answers.
node.js interview questions and answers.node.js interview questions and answers.
node.js interview questions and answers.
JyothiAmpally
 
Exception and ErrorHandling in Java .ppt
Exception and ErrorHandling in Java .pptException and ErrorHandling in Java .ppt
Exception and ErrorHandling in Java .ppt
JyothiAmpally
 
Arrays Basicfundamentaldatastructure.ppt
Arrays Basicfundamentaldatastructure.pptArrays Basicfundamentaldatastructure.ppt
Arrays Basicfundamentaldatastructure.ppt
JyothiAmpally
 
_Java__Expressions__and__FlowControl.ppt
_Java__Expressions__and__FlowControl.ppt_Java__Expressions__and__FlowControl.ppt
_Java__Expressions__and__FlowControl.ppt
JyothiAmpally
 
Java-Variables_about_different_Scope.ppt
Java-Variables_about_different_Scope.pptJava-Variables_about_different_Scope.ppt
Java-Variables_about_different_Scope.ppt
JyothiAmpally
 
Java Operators explained _in __brief.ppt
Java Operators explained _in __brief.pptJava Operators explained _in __brief.ppt
Java Operators explained _in __brief.ppt
JyothiAmpally
 
UML to Object Oriented Mapping java .ppt
UML to Object Oriented Mapping java .pptUML to Object Oriented Mapping java .ppt
UML to Object Oriented Mapping java .ppt
JyothiAmpally
 
OOPS_AbstractClasses_explained__java.ppt
OOPS_AbstractClasses_explained__java.pptOOPS_AbstractClasses_explained__java.ppt
OOPS_AbstractClasses_explained__java.ppt
JyothiAmpally
 
Java_Identifiers_keywords_data_types.ppt
Java_Identifiers_keywords_data_types.pptJava_Identifiers_keywords_data_types.ppt
Java_Identifiers_keywords_data_types.ppt
JyothiAmpally
 
Java_gui_with_AWT_and_its_components.ppt
Java_gui_with_AWT_and_its_components.pptJava_gui_with_AWT_and_its_components.ppt
Java_gui_with_AWT_and_its_components.ppt
JyothiAmpally
 
1. Introduction to HTML5.ppt
1. Introduction to HTML5.ppt1. Introduction to HTML5.ppt
1. Introduction to HTML5.ppt
JyothiAmpally
 
01-basics-functions.ppt
01-basics-functions.ppt01-basics-functions.ppt
01-basics-functions.ppt
JyothiAmpally
 
03_A-OOPs_Interfaces.ppt
03_A-OOPs_Interfaces.ppt03_A-OOPs_Interfaces.ppt
03_A-OOPs_Interfaces.ppt
JyothiAmpally
 
25-functions.ppt
25-functions.ppt25-functions.ppt
25-functions.ppt
JyothiAmpally
 
03_A-OOPs_Interfaces.ppt
03_A-OOPs_Interfaces.ppt03_A-OOPs_Interfaces.ppt
03_A-OOPs_Interfaces.ppt
JyothiAmpally
 
02-Java Technology Details.ppt
02-Java Technology Details.ppt02-Java Technology Details.ppt
02-Java Technology Details.ppt
JyothiAmpally
 
01-Java Introduction.ppt
01-Java Introduction.ppt01-Java Introduction.ppt
01-Java Introduction.ppt
JyothiAmpally
 
01_What is Java.ppt
01_What is Java.ppt01_What is Java.ppt
01_What is Java.ppt
JyothiAmpally
 
Spring security
Spring securitySpring security
Spring security
JyothiAmpally
 
node.js interview questions and answers.
node.js interview questions and answers.node.js interview questions and answers.
node.js interview questions and answers.
JyothiAmpally
 
Exception and ErrorHandling in Java .ppt
Exception and ErrorHandling in Java .pptException and ErrorHandling in Java .ppt
Exception and ErrorHandling in Java .ppt
JyothiAmpally
 
Arrays Basicfundamentaldatastructure.ppt
Arrays Basicfundamentaldatastructure.pptArrays Basicfundamentaldatastructure.ppt
Arrays Basicfundamentaldatastructure.ppt
JyothiAmpally
 
_Java__Expressions__and__FlowControl.ppt
_Java__Expressions__and__FlowControl.ppt_Java__Expressions__and__FlowControl.ppt
_Java__Expressions__and__FlowControl.ppt
JyothiAmpally
 
Java-Variables_about_different_Scope.ppt
Java-Variables_about_different_Scope.pptJava-Variables_about_different_Scope.ppt
Java-Variables_about_different_Scope.ppt
JyothiAmpally
 
Java Operators explained _in __brief.ppt
Java Operators explained _in __brief.pptJava Operators explained _in __brief.ppt
Java Operators explained _in __brief.ppt
JyothiAmpally
 
UML to Object Oriented Mapping java .ppt
UML to Object Oriented Mapping java .pptUML to Object Oriented Mapping java .ppt
UML to Object Oriented Mapping java .ppt
JyothiAmpally
 
OOPS_AbstractClasses_explained__java.ppt
OOPS_AbstractClasses_explained__java.pptOOPS_AbstractClasses_explained__java.ppt
OOPS_AbstractClasses_explained__java.ppt
JyothiAmpally
 
Java_Identifiers_keywords_data_types.ppt
Java_Identifiers_keywords_data_types.pptJava_Identifiers_keywords_data_types.ppt
Java_Identifiers_keywords_data_types.ppt
JyothiAmpally
 
Java_gui_with_AWT_and_its_components.ppt
Java_gui_with_AWT_and_its_components.pptJava_gui_with_AWT_and_its_components.ppt
Java_gui_with_AWT_and_its_components.ppt
JyothiAmpally
 
1. Introduction to HTML5.ppt
1. Introduction to HTML5.ppt1. Introduction to HTML5.ppt
1. Introduction to HTML5.ppt
JyothiAmpally
 
01-basics-functions.ppt
01-basics-functions.ppt01-basics-functions.ppt
01-basics-functions.ppt
JyothiAmpally
 
03_A-OOPs_Interfaces.ppt
03_A-OOPs_Interfaces.ppt03_A-OOPs_Interfaces.ppt
03_A-OOPs_Interfaces.ppt
JyothiAmpally
 
03_A-OOPs_Interfaces.ppt
03_A-OOPs_Interfaces.ppt03_A-OOPs_Interfaces.ppt
03_A-OOPs_Interfaces.ppt
JyothiAmpally
 
02-Java Technology Details.ppt
02-Java Technology Details.ppt02-Java Technology Details.ppt
02-Java Technology Details.ppt
JyothiAmpally
 
01-Java Introduction.ppt
01-Java Introduction.ppt01-Java Introduction.ppt
01-Java Introduction.ppt
JyothiAmpally
 
Ad

Recently uploaded (20)

Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdfBiophysics Chapter 3 Methods of Studying Macromolecules.pdf
Biophysics Chapter 3 Methods of Studying Macromolecules.pdf
PKLI-Institute of Nursing and Allied Health Sciences Lahore , Pakistan.
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...The ever evoilving world of science /7th class science curiosity /samyans aca...
The ever evoilving world of science /7th class science curiosity /samyans aca...
Sandeep Swamy
 
To study Digestive system of insect.pptx
To study Digestive system of insect.pptxTo study Digestive system of insect.pptx
To study Digestive system of insect.pptx
Arshad Shaikh
 
How to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 WebsiteHow to Subscribe Newsletter From Odoo 18 Website
How to Subscribe Newsletter From Odoo 18 Website
Celine George
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdfExploring-Substances-Acidic-Basic-and-Neutral.pdf
Exploring-Substances-Acidic-Basic-and-Neutral.pdf
Sandeep Swamy
 
2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx2541William_McCollough_DigitalDetox.docx
2541William_McCollough_DigitalDetox.docx
contactwilliamm2546
 
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptxSCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
SCI BIZ TECH QUIZ (OPEN) PRELIMS XTASY 2025.pptx
Ronisha Das
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam SuccessUltimate VMware 2V0-11.25 Exam Dumps for Exam Success
Ultimate VMware 2V0-11.25 Exam Dumps for Exam Success
Mark Soia
 
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACYUNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
UNIT 3 NATIONAL HEALTH PROGRAMMEE. SOCIAL AND PREVENTIVE PHARMACY
DR.PRISCILLA MARY J
 
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - WorksheetCBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
CBSE - Grade 8 - Science - Chemistry - Metals and Non Metals - Worksheet
Sritoma Majumder
 
Ad

Java Strings methods and operations.ppt

  • 2. ● This module introduces the participants to the string object. ● Importance of String Objects and How to create them. ● Varioius Constructors that are available to create Strings. ● Working with methods that are available with strings. ● Understanding Necessity of StringBuffer class. ● Working with methods of StringBuffer class. ● Introduction to StringBuilder class. ● Working with methods of StringBuilder class. Module Overview 2
  • 3.  Uderstand the concept of Strings and create string objects.  Understand advantages and disadvantages of Strings.  Declare and create Strings using different types of constructors.  Working with Strings and its different types of methods.  Determine the usage of String Buffer and StringBuilder. Objectives 3
  • 4. String: Strings are a sequence of characters, which are implemented as objects. Charecteristics of Strings: • The String class is immutable,meaning once created a String object cannot be changed. • If any modifications is done to the existing string object a new object is created internally. • String objects are created by using the new keyword and a constructor. The String class has eleven constructors that allow you to provide the initial value of the string using different sources, such as an array of characters. String 4
  • 5. String Declaration String Declaration: The most direct way to create a string is as follows; String greeting = "Hello world!";//String literal Other Common Declarations: String helloString = new String(helloArray); char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'};
  • 6. Example of String Class. public class StringDemo{ public static void main(String args[]){ char[] helloArray = { 'h', 'e', 'l', 'l', 'o', '.'}; String helloString = new String(helloArray); String helloString2 = new String(“hello!!!”); System.out.println( helloString ); } } Output:hello.
  • 7. Methods Of String  Accessor: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. Syntax: <StringOBject>.length(); Example: String name = “java”; int length = name.length();
  • 8. Methods of String(Cont…) Concatenating Strings:One String can be concatenated to another string.This will produce a new string object joining two strings. Strings are more commonly concatenated with the + operator and concat method. Syntax: string1.concat(string2); string1+string2
  • 9. Methods of String(Cont…)  String substring(int beginIndex) Returns a new string that is a substring of this string.  char[] toCharArray() Converts this string to a new character array.  String toLowerCase() Converts all of the characters in this String to lower case using the rules of the default locale.  String replace(char oldChar, char newChar) Returns a new string resulting from replacing all occurrences of oldChar in this string with newChar.  String[] split(String regex) Splits this string around matches of the given regular expression.
  • 10.  String toString() This object (which is already a string!) is itself returned.  String toUpperCase() Converts all of the characters in this String to upper case using the rules of the default locale.  String trim() Returns a copy of the string, with leading and trailing whitespace omitted.  boolean startsWith(String prefix) Tests if this string starts with the specified prefix.  boolean contentEquals(StringBuffer sb) Returns true if and only if this String represents the same sequence of characters as the specified StringBuffer. Methods of String(Cont…)
  • 11. Working with methods using Strings  Like other values to (parameters)methods, Strings also can be passed to methods. Ex: public String printString(String s) { System.out.println(s); return “modified”+s; } String newString =printString(new String(“Java”));//invoking an array
  • 12. Necessity of StringBuffer  String objects are immutable,meaning modifications to the string object cannot be done once created. If done a new String Object will be created in memory.  The StringBuffer and StringBuilder classes are used when there is a necessity to make alot of modifications to Strings of characters.  StringBuffer and Stringbuilder can be modified over and over again with out leaving behind alot of new unused objects.
  • 13. String Buffers Methods  public StringBuffer append(String s) Updates the value of the object that invoked the method. The method takes boolean, char, int, long, Strings etc.  public StringBuffer reverse() The method reverses the value of the StringBuffer object that invoked the method.  public delete(int start, int end) Deletes the string starting from start index until end index.
  • 14. String Buffers Methods  void setLength(int newLength) Sets the length of this String buffer.  int length() Returns the length (character count) of this string buffer.  void ensureCapacity(int minimumCapacity) Ensures that the capacity of the buffer is at least equal to the specified minimum.  int capacity() Returns the current capacity of the String buffer.
  • 15. String Builder Methods  int length() Returns the length (character count) of this string buffer.  void ensureCapacity(int minimumCapacity) Ensures that the capacity of the buffer is at least equal to the specified minimum.  int capacity() Returns the current capacity of the String buffer.  reverse() :Causes this character sequence to be replaced by the reverse of the sequence.
  • 16. StringBuilder vs StringBuffer  Below is a table that contains differences between StringBuilder and StringBuffer. StringBuffer StringBuilder It is mutable It is mutable It is synchronized,provides ThreadSafety. It is non synchronized.Provides no ThreadSafety. Capacity increases automatically . Capacity increases automatically .
  • 17. Excercise  Create an application that will convert a given character array to a string.  Creat an application that will create a reverse of a string.  Create an application that uses StringBuffer to concatinate the user input.  Create an application that uses a StringBuilder and String Buffer and insert one in another.

Editor's Notes

  • #2: Instructor Notes:
  • #3: Instructor Notes: By the end of this module, participants should be able to: <Obj1> <Obj 2>
  • #4: Instructor Notes: