SlideShare a Scribd company logo
java.lang.String class
In Java a String is an object of class java.lang.String and we can make the string objects by calling String
constructors. The constructors of java.lang.String classes are overloaded some of them are

Creating an empty String
String s = new String();
Creating a string with character array
String fromCharArray = new String(char[] arr);
Creating a string from character array with specified index and number of characters
String fromCharArray = new String(char[], int startIndex, int numberOfCharacters);
Creating a string from another string
String copy = new String(String s);
Methods of java.lang.String class



length()
The method length is used to find the length of a String object.
Declaration
public int length();
Usage
String s = “Alex Vipul Verma”;
int len = s.length();
toString()
The method toString() is an instance method of Object class and overridden by most of the classes
in Java API. It is used to convert any reference data type into String for readable from
Declaration
public String toString();
Usage
Object ob = new Object();
String s = ob.toString();
getBytes()
The method getBytes() is used to create a byte array from the String with the ASCII values of
individual characters
Declaration
public byte[] getBytes();
Usage
String s = “ABCD”;
byte[] arr = new byte[s.length()];
arr = s.getBytes();
Here the array arr is generated as
byte arr = {65, 66, 67, 68};
toCharArray()
The method toCharArray() is similar to getBytes(). The method getBytes() generates the byte type
array and char Array generates char type array
Declaration
public char[] toCharArray();
Usage
String s = “Hello!”;
char[] arr = new char[s.length];
arr = s.toCharArray();
Here the character array arr is generated as
char arr = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘!’};
equals() and equalsIgnoreCase()
The method equals() is used to check the equality of two given strings. It returns true if the
strings are equal. equalsIgnoreCase() is similar to equals() but it ignore the cases of strings
Declaration
public boolean equals(String s);
public boolean equalsIgnoreCase(String s);
Usage
boolean result1 = “Alex”.equals(“Alex”);
boolean result2 = “Hello”.equals(“HELLO”);
startsWith() and endsWith()
These two methods are overloaded. They are used to check whether a String is starting (or ending)
with specified character or NOT
Declaration
public boolean startsWith(char ch);
public boolean startsWith(String s);
public boolean endsWith(char ch);
public boolean startsWith(String s);


charAt(index)
The method charAt() is used to find the character at the specified index from a String
Declaration
public char charAt(int index);
Usage
String s = “Alex Vipul Verma”;
char ch = s.charAt(10);
indexOf() and lastIndexOf()
These two methods are also overloaded to take string and character type parameter. They are used
to get the index of specified character or string into the existing string. They return -1 if the
argument did NOT found into the calling String.
Declaration
public int indexOf(char ch);
public int indexOf(String s);
public int lastIndexOf(char ch);
public int lastIndexOf(String s);
substring()
substring() is an overloaded method. Used to create a substring from another string. It is helpful
to obtain a copy of string with the specified number of characters and starting index
Declaration
public String substring(int startIndex);
public String substring(int startIndex, endIndex);
trim()
The method trim() is used to remove the unnecessary whitespaces from a given string.
Declaration
public String trim();
toUpperCase() and toLowerCase()
These two methods are used to convert all the characters of a string to uppercase or lowercase.
The String objects are immutable so they do NOT modify the existing String but they return a copy
of modified String.
Declaration
public String toUpperCase();
public String toLowerCase();
java.lang.String Class
Ad

More Related Content

What's hot (20)

String Handling
String HandlingString Handling
String Handling
Bharat17485
 
Java 8 Lambda Expressions
Java 8 Lambda ExpressionsJava 8 Lambda Expressions
Java 8 Lambda Expressions
Hyderabad Scalability Meetup
 
Pattern Matching - at a glance
Pattern Matching - at a glancePattern Matching - at a glance
Pattern Matching - at a glance
Knoldus Inc.
 
Covariance, contravariance 觀念分享
Covariance, contravariance 觀念分享Covariance, contravariance 觀念分享
Covariance, contravariance 觀念分享
LearningTech
 
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
 
Python dictionary
Python   dictionaryPython   dictionary
Python dictionary
Mohammed Sikander
 
Methods common to all objects
Methods common to all objectsMethods common to all objects
Methods common to all objects
Sandeep Chawla
 
String, string builder, string buffer
String, string builder, string bufferString, string builder, string buffer
String, string builder, string buffer
SSN College of Engineering, Kalavakkam
 
Chapter 14 strings
Chapter 14 stringsChapter 14 strings
Chapter 14 strings
Praveen M Jigajinni
 
Dictionaries
DictionariesDictionaries
Dictionaries
mansoura University
 
String handling session 5
String handling session 5String handling session 5
String handling session 5
Raja Sekhar
 
Java 8 streams
Java 8 streamsJava 8 streams
Java 8 streams
Manav Prasad
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
Neelkanth Sachdeva
 
Lecture 7
Lecture 7Lecture 7
Lecture 7
Debasish Pratihari
 
Functions & closures
Functions & closuresFunctions & closures
Functions & closures
Knoldus Inc.
 
Module 7 : Arrays
Module 7 : ArraysModule 7 : Arrays
Module 7 : Arrays
Prem Kumar Badri
 
String in python lecture (3)
String in python lecture (3)String in python lecture (3)
String in python lecture (3)
Ali ٍSattar
 
Ruby data types and objects
Ruby   data types and objectsRuby   data types and objects
Ruby data types and objects
Harkamal Singh
 
Arrays
ArraysArrays
Arrays
Faisal Aziz
 
Python strings
Python stringsPython strings
Python strings
Mohammed Sikander
 

Similar to java.lang.String Class (20)

String and string buffer
String and string bufferString and string buffer
String and string buffer
kamal kotecha
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
Eduardo Bergavera
 
M C6java7
M C6java7M C6java7
M C6java7
mbruggen
 
Java căn bản - Chapter9
Java căn bản - Chapter9Java căn bản - Chapter9
Java căn bản - Chapter9
Vince Vo
 
Strings.ppt
Strings.pptStrings.ppt
Strings.ppt
SanthiyaAK
 
07slide
07slide07slide
07slide
Aboudi Sabbah
 
Computer programming 2 Lesson 12
Computer programming 2  Lesson 12Computer programming 2  Lesson 12
Computer programming 2 Lesson 12
MLG College of Learning, Inc
 
package
packagepackage
package
sweetysweety8
 
Module-1 Strings Handling.ppt.pdf
Module-1 Strings Handling.ppt.pdfModule-1 Strings Handling.ppt.pdf
Module-1 Strings Handling.ppt.pdf
learnEnglish51
 
3.1 STRINGS (1) java jksdbkjdbsjsef.pptx
3.1 STRINGS (1) java jksdbkjdbsjsef.pptx3.1 STRINGS (1) java jksdbkjdbsjsef.pptx
3.1 STRINGS (1) java jksdbkjdbsjsef.pptx
mohithn2004
 
DOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptx
DOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptxDOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptx
DOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptx
meganath16032003
 
String in java, string constructors and operations
String in java, string constructors and operationsString in java, string constructors and operations
String in java, string constructors and operations
manjeshbngowda
 
Java string , string buffer and wrapper class
Java string , string buffer and wrapper classJava string , string buffer and wrapper class
Java string , string buffer and wrapper class
SimoniShah6
 
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
 
String handling
String handlingString handling
String handling
ssuser20c32b
 
String notes
String notesString notes
String notes
Prasadu Peddi
 
10619416141061941614.106194161fff4..pptx
10619416141061941614.106194161fff4..pptx10619416141061941614.106194161fff4..pptx
10619416141061941614.106194161fff4..pptx
Shwetamaurya36
 
Strings
StringsStrings
Strings
Imad Ali
 
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
 
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVASTRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
pkavithascs
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
kamal kotecha
 
Chapter 9 - Characters and Strings
Chapter 9 - Characters and StringsChapter 9 - Characters and Strings
Chapter 9 - Characters and Strings
Eduardo Bergavera
 
Java căn bản - Chapter9
Java căn bản - Chapter9Java căn bản - Chapter9
Java căn bản - Chapter9
Vince Vo
 
Module-1 Strings Handling.ppt.pdf
Module-1 Strings Handling.ppt.pdfModule-1 Strings Handling.ppt.pdf
Module-1 Strings Handling.ppt.pdf
learnEnglish51
 
3.1 STRINGS (1) java jksdbkjdbsjsef.pptx
3.1 STRINGS (1) java jksdbkjdbsjsef.pptx3.1 STRINGS (1) java jksdbkjdbsjsef.pptx
3.1 STRINGS (1) java jksdbkjdbsjsef.pptx
mohithn2004
 
DOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptx
DOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptxDOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptx
DOC-20240802-WA0004dgcuhfgbjhfucjv6du..pptx
meganath16032003
 
String in java, string constructors and operations
String in java, string constructors and operationsString in java, string constructors and operations
String in java, string constructors and operations
manjeshbngowda
 
Java string , string buffer and wrapper class
Java string , string buffer and wrapper classJava string , string buffer and wrapper class
Java string , string buffer and wrapper class
SimoniShah6
 
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
 
10619416141061941614.106194161fff4..pptx
10619416141061941614.106194161fff4..pptx10619416141061941614.106194161fff4..pptx
10619416141061941614.106194161fff4..pptx
Shwetamaurya36
 
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
 
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVASTRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
STRING CLASS AND STRING BUFFER CLASS CONCEPTS IN JAVA
pkavithascs
 
Ad

Recently uploaded (20)

tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
tecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdftecnologias de las primeras civilizaciones.pdf
tecnologias de las primeras civilizaciones.pdf
fjgm517
 
Rusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond SparkRusty Waters: Elevating Lakehouses Beyond Spark
Rusty Waters: Elevating Lakehouses Beyond Spark
carlyakerly1
 
AI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global TrendsAI and Data Privacy in 2025: Global Trends
AI and Data Privacy in 2025: Global Trends
InData Labs
 
Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025Splunk Security Update | Public Sector Summit Germany 2025
Splunk Security Update | Public Sector Summit Germany 2025
Splunk
 
Cyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of securityCyber Awareness overview for 2025 month of security
Cyber Awareness overview for 2025 month of security
riccardosl1
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep DiveDesigning Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
Designing Low-Latency Systems with Rust and ScyllaDB: An Architectural Deep Dive
ScyllaDB
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Drupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy ConsumptionDrupalcamp Finland – Measuring Front-end Energy Consumption
Drupalcamp Finland – Measuring Front-end Energy Consumption
Exove
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)Into The Box Conference Keynote Day 1 (ITB2025)
Into The Box Conference Keynote Day 1 (ITB2025)
Ortus Solutions, Corp
 
Procurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptxProcurement Insights Cost To Value Guide.pptx
Procurement Insights Cost To Value Guide.pptx
Jon Hansen
 
Quantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur MorganQuantum Computing Quick Research Guide by Arthur Morgan
Quantum Computing Quick Research Guide by Arthur Morgan
Arthur Morgan
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-UmgebungenHCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
HCL Nomad Web – Best Practices und Verwaltung von Multiuser-Umgebungen
panagenda
 
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes Partner Innovation Updates for May 2025
ThousandEyes
 
What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...What is Model Context Protocol(MCP) - The new technology for communication bw...
What is Model Context Protocol(MCP) - The new technology for communication bw...
Vishnu Singh Chundawat
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
Ad

java.lang.String Class

  • 1. java.lang.String class In Java a String is an object of class java.lang.String and we can make the string objects by calling String constructors. The constructors of java.lang.String classes are overloaded some of them are Creating an empty String String s = new String(); Creating a string with character array String fromCharArray = new String(char[] arr); Creating a string from character array with specified index and number of characters String fromCharArray = new String(char[], int startIndex, int numberOfCharacters); Creating a string from another string String copy = new String(String s);
  • 2. Methods of java.lang.String class length() The method length is used to find the length of a String object. Declaration public int length(); Usage String s = “Alex Vipul Verma”; int len = s.length(); toString() The method toString() is an instance method of Object class and overridden by most of the classes in Java API. It is used to convert any reference data type into String for readable from Declaration public String toString(); Usage Object ob = new Object(); String s = ob.toString();
  • 3. getBytes() The method getBytes() is used to create a byte array from the String with the ASCII values of individual characters Declaration public byte[] getBytes(); Usage String s = “ABCD”; byte[] arr = new byte[s.length()]; arr = s.getBytes(); Here the array arr is generated as byte arr = {65, 66, 67, 68}; toCharArray() The method toCharArray() is similar to getBytes(). The method getBytes() generates the byte type array and char Array generates char type array Declaration public char[] toCharArray(); Usage String s = “Hello!”; char[] arr = new char[s.length]; arr = s.toCharArray(); Here the character array arr is generated as char arr = {‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘!’};
  • 4. equals() and equalsIgnoreCase() The method equals() is used to check the equality of two given strings. It returns true if the strings are equal. equalsIgnoreCase() is similar to equals() but it ignore the cases of strings Declaration public boolean equals(String s); public boolean equalsIgnoreCase(String s); Usage boolean result1 = “Alex”.equals(“Alex”); boolean result2 = “Hello”.equals(“HELLO”); startsWith() and endsWith() These two methods are overloaded. They are used to check whether a String is starting (or ending) with specified character or NOT Declaration public boolean startsWith(char ch); public boolean startsWith(String s); public boolean endsWith(char ch); public boolean startsWith(String s);
  • 5. 
 charAt(index) The method charAt() is used to find the character at the specified index from a String Declaration public char charAt(int index); Usage String s = “Alex Vipul Verma”; char ch = s.charAt(10); indexOf() and lastIndexOf() These two methods are also overloaded to take string and character type parameter. They are used to get the index of specified character or string into the existing string. They return -1 if the argument did NOT found into the calling String. Declaration public int indexOf(char ch); public int indexOf(String s); public int lastIndexOf(char ch); public int lastIndexOf(String s);
  • 6. substring() substring() is an overloaded method. Used to create a substring from another string. It is helpful to obtain a copy of string with the specified number of characters and starting index Declaration public String substring(int startIndex); public String substring(int startIndex, endIndex); trim() The method trim() is used to remove the unnecessary whitespaces from a given string. Declaration public String trim(); toUpperCase() and toLowerCase() These two methods are used to convert all the characters of a string to uppercase or lowercase. The String objects are immutable so they do NOT modify the existing String but they return a copy of modified String. Declaration public String toUpperCase(); public String toLowerCase();