0% found this document useful (0 votes)
2 views39 pages

Java String Basics and Methods Guide

The document provides an overview of Strings in Java, covering topics such as string basics, built-in methods, immutability, and the StringBuilder class. It includes various questions and examples related to string manipulation, such as counting vowels, checking for palindromes, and finding the most frequent character. Additionally, it discusses string interning, concatenation, and methods for substring extraction.

Uploaded by

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

Java String Basics and Methods Guide

The document provides an overview of Strings in Java, covering topics such as string basics, built-in methods, immutability, and the StringBuilder class. It includes various questions and examples related to string manipulation, such as counting vowels, checking for palindromes, and finding the most frequent character. Additionally, it discusses string interning, concatenation, and methods for substring extraction.

Uploaded by

gkr149062
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1) Pace 4

2) No Backlogs

Strings
In Java
Contents
1. String Basics
2. Built-In Methods
3. Immutability
4. StringBuilder
5. Questions
character Array

char1] arr =
I'v ,
'a ,
gl ,
'na'v's ;

I
sout(arr[3]) ;

strings

String as a data type


String Data Type
charAt() & length()
0
12345678910112131419
and Shruti" ;
String s = "Chaitanya

sout(s charAt(1)) ;
.
// S(4]

int u = s .
length() ;
Ques: Count the number of vowels in a
-

given string d

·a ,
'E ,
'i' ,
10 'n
Ques: Palindrome String

mom
on)
dad

racecar

malayalam racecar
i j
naman

markram

nitin

abucba
indexOf() & compareTo()

I
-
eexographically smaller

(s2) ;
S1 .

compareTo raghar < sucha

↓ harmeet > harsh


integer har shit
harsh

Raghavaditya
contains() & startsWith()
toLowerCase() & concat()
[
S1 concat(S2) ;
.
Ques: Change the string
String + int/char/String
sout(“raghav”+10+20) left to right

↓ I ~
raghar1028
ragharzo

sout 110 + 20 +
"raghar") ;
sout (10 +
"raghar" + 20) ;
Ques: Take integer input and convert it
into a string
Ques: Digits in a given number
substring(i) & substring(i,j)
Ques: Print all substrings Array to
string

S : "gopi"

g , go , gop , gopi

o , op , opi
P , pi
i
HW: Sum of all substrings of a number
Interning & new keyword
11

Strings "raghar";
7)
=
raghav
S

= "Harsh" ; "Harsh"

Things
"Guddn" =

se"Guddu"
:

string t = "Guddn" ; > "End"


--
t = "End" "Guddn"
a-

string a = new
String(" Guddu") ;
Interning & new keyword

"Karan" ; "Karan"
String =
S

S += "Saini" ; "KaranSaini"

""
t = ;

t += 'r' ,

t += /a ;

t +
=
'g
String immutability in Java
↓ -
Security purpose
cannot change individual characters

01234S

S = "Bishal" ; S = s .
substring (0 ,
3) + 'm + s .

substring (1) ;


M
[Link](s2)

String S2 =
"Raghar" ;
String 2 = new String ("Raghar") ;

sout (S1 == S2) ;

L
false
StringBuilder
setCharAt()
Ques: Toggle Characters
[Link](s2)
insert() & deleteCharAt()
reverse()
Ques: Reverse each word in a given
string

I
i
s "Archit is star" ;
boy" ;
=

= "Bishal is a bad


j

ans "tihcra si rats" ;
yob" ;
I

ane =
"
Lahsib si a dab
Ques: Anagram
fired fried
latent talent
anagram nagaram
aces
--acer
race care

Listen silent
-

C 1 -

i -
1

S -
1
1
t -

e -
1

n + 1
Ques: Most frequent character
C-2
"character"
String s = ;
a - 2

v - 2

Method-1 : Brute Force


-

↑c .
= 0 (2) "Nested Loop"

method-2
-
:
Sorting T C
.
=
Onlogn)
·

S -
1/
accehrut "i
I
sliding window
Ques: Most frequent character
i
S = "aacce At t 2222
"
maxFreg = 2 BY
agyz
-

J ans=
Ques: Most frequent character
Method-3
-
: Frequency away.

S = character

2324 IS

1000 ,
- -
.
...

free :


d
A
b C
Y

121122
120
979899

A S .
. = 0(1) T C
.
. =
O(u)
HW: Non repeating character
Ques: String Compression Fleetcode]
i "
aaabb 22 t
"
S =
RK aa
j


ans =
11
a3b222tR2a2"
HW: Easy String
THANKYOU
Cuties

You might also like