0% found this document useful (0 votes)
3 views2 pages

String Handling GR 10

The document provides an introduction to Strings in Java, explaining that they are immutable sequences of characters and can be created using string literals or the new keyword. It details various Java String methods, including their descriptions, syntax, examples, and explanations, covering functionalities like length, character retrieval, substring extraction, case conversion, and string comparison. Additionally, it discusses methods for replacing characters, trimming spaces, and converting between primitive types and strings.

Uploaded by

vaanydev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views2 pages

String Handling GR 10

The document provides an introduction to Strings in Java, explaining that they are immutable sequences of characters and can be created using string literals or the new keyword. It details various Java String methods, including their descriptions, syntax, examples, and explanations, covering functionalities like length, character retrieval, substring extraction, case conversion, and string comparison. Additionally, it discusses methods for replacing characters, trimming spaces, and converting between primitive types and strings.

Uploaded by

vaanydev
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

✅ 1.

Introduction to Strings

 A String in Java is a sequence of characters.

 Strings are objects of the String class in java.lang package.

 They are immutable: Once created, their values cannot be


changed.

✅ 2. Declaring and Creating Strings

Ways to create strings:

1. String literal

String name = "ICSE";

2. Using new keyword

String name = new String("ICSE");

📘 Java String Methods – With Description, Syntax, Example &


Explanation

Method Description Syntax Example Explanation


Returns number of "Hello".length(
There are 5
length() characters in the str.length() )→5
characters in
string. "Hello".
Returns character "Java".charAt(2
Index 2 refers to
charAt(int
index) at the given index str.charAt(1)
) → 'v'
the 3rd character
(0-based). 'v'.
indexOf(char Finds first index str.indexOf('a' "banana".indexO First 'a' occurs at
ch) of given character. ) f('a') → 1 index 1.
indexOf(char Finds index of Searches 'a'
str.indexOf('a' "banana".indexO
ch, int character from , 2) f('a', 2) → 3
starting from
startIndex) given index. index 2; finds at 3.
Finds last
lastIndexOf(cha str.lastIndexOf "banana".lastIn Last 'a' is at
r ch) occurrence index ('a') dexOf('a') → 5 index 5.
of character.
Extracts substring "Welcome".subst
Extracts from
substring(int
from start index to
str.substring(3
ring(3) →
index 3 to end:
startIndex) ) 'c','o','m','e'
end. "come"
.
Extracts substring "Welcome".subst Characters at
substring(int str.substring(1
start, int end) from start to (end , 4) ring(1,4) → index 1, 2, 3 =
- 1). "elc" 'e','l','c'.
toLowerCase() Converts all str.toLowerCase "ICSE".toLowerC Converts capital
() ase() → "icse" letters to small.
letters to
Method Description Syntax Example Explanation
lowercase.
Converts all
str.toUpperCase "java".toUpperC Converts small
toUpperCase() letters to
() ase() → "JAVA" letters to capital.
uppercase.
Replaces all old "banana".replac
replace(char str.replace('a' All 'a' replaced
old, char new) characters with ,'e') e('a','e') →
"benene" with 'e'.
new ones.
replace(CharSeq Replaces all old "This
uence oldSeq, str.replace("is is".replace("is 'is' is replaced
CharSequence substrings with ", "was") ", "was") → wherever found.
newSeq) new ones. "Thwas was"
"Hello".concat( Appends "
concat(String Joins the given str.concat("
s) " World") → World" to
string to the end. World") "Hello World" "Hello".
Checks if two
equals(String strings are exactly str.equals("Jav "Java".equals(" Case doesn't
s) the same (case- a") java") → false match, so false.
sensitive).
Checks string "Java".equalsIg
equalsIgnoreCas str.equalsIgnor noreCase("java" Ignores case; both
e(String s) equality ignoring eCase("java")
) → true are same.
case.
"banana".compar "banana" comes
compareTo(Strin Lexicographically str.compareTo("
g s) eTo("apple") → after "apple", so
compares strings. apple") 1 result > 0.
Lexicographic str.compareToIg "apple".compare
compareToIgnore Both are equal
Case(String s) compare ignoring noreCase("Apple ToIgnoreCase("A
") pple") → 0 ignoring case.
case.
Removes spaces " Java ".trim() Removes spaces
trim() from start and str.trim()
→ "Java" around "Java".
end.
Checks if string "weekend".endsW
endsWith(String str.endsWith("e String ends with
suffix) ends with the nd") ith("end") →
true "end".
given suffix.
String.valueOf( Converts primitive String.valueOf( String.valueOf( Converts int 123
primitive) to string. 123) 123) → "123" to string "123".
parseXxx(String
s) e.g. Converts string to Integer.parseIn Integer.parseIn Converts "456" to
Integer.parseIn primitive type. t("456") t("456") → 456 int 456.
t(s)

You might also like