5Java Strings-WPS Office
5Java Strings-WPS Office
aSt
ri
ngs
I
nthi
stutori
al,
wewil
ll
earnaboutJavast
ri
ngs,
howt
ocr
eat
ethem,
andv
ari
ousmet
hodsoft
he
St
ri
ngclasswitht
hehel
pofexamples.
InJava,ast
ri
ngisasequenceofcharacter
s.Forexampl
e,"
hel
l
o"i
sast
ri
ngcont
aini
nga
sequenceofchar
act
ers'
h','
e'
,'
l'
,'
l'
,and'o'
.
Weusedoubl
equot
est
orepr
esentast
ri
ngi
nJav
a.Forexampl
e,
/
/cr
eat
east
ri
ng
St
ri
ngt
ype="
Jav
apr
ogr
ammi
ng"
;
Her
e,wehavecr
eat
edast
ri
ngv
ari
abl
enamedt
ype.Thev
ari
abl
eisi
nit
ial
i
zedwi
tht
hest
ri
ng
Jav
aProgr
amming.
Exampl
e:Cr
eat
eaSt
ri
ngi
nJav
a
cl
assMai
n{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
/
/cr
eat
est
ri
ngs
St
ri
ngf
ir
st="
Jav
a";
St
ri
ngsecond="
Pyt
hon"
;
St
ri
ngt
hir
d="
Jav
aScr
ipt
";
/
/pr
intst
ri
ngs
Sy
stem.
out
.pr
int
ln(
fi
rst
);/
/pr
intJav
a
Sy
stem.
out
.pr
int
ln(
second)
;//pr
intPy
thon
Sy
stem.
out
.pr
int
ln(
thi
rd)
;//pr
intJav
aScr
ipt
RunCode
I
ntheaboveexample,
wehavecr
eatedthr
eest
ri
ngsnamedf
ir
st,
second,
andt
hir
d.Her
e,wear
e
di
rect
lycr
eat
ingst
ri
ngsli
kepr
imi
ti
vetypes.
However,
therei
sanot
herwayofcr
eat
ingJav
ast
ri
ngs(
usi
ngt
henewkey
wor
d).Wewi
l
llear
n
aboutt
hatlat
eri
nthi
stut
ori
al.
Not
e:St
ri
ngsinJav
aarenotpri
miti
vet
ypes(
li
kei
nt,
char
,et
c).I
nst
ead,
all
str
ingsar
eobj
ect
sof
apr
edef
inedcl
assnamedStr
ing.
And,
all
str
ingv
ari
abl
esar
einst
ancesoft
heSt
ri
ngcl
ass.
Jav
aSt
ri
ngOper
ati
ons
JavaSt
ri
ngpr
ovi
desv
ari
ousmet hodstoper
for
m di
ffer
entoper
ati
onsonst
ri
ngs.Wewi
l
llook
i
ntosomeoft
hecommonlyusedstri
ngoper
ati
ons.
1.Getl
engt
hofaSt
ri
ng
Tof
indt
hel
engt
hofast
ri
ng,
weuset
hel
engt
h()met
hodoft
heSt
ri
ng.Forexampl
e,
cl
assMai
n{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
/
/cr
eat
east
ri
ng
St
ri
nggr
eet="
Hel
l
o!Wor
ld"
;
Sy
stem.
out
.pr
int
ln(
"St
ri
ng:
"+gr
eet
);
/
/gett
hel
engt
hofgr
eet
i
ntl
engt
h=gr
eet
.l
engt
h()
;
Sy
stem.
out
.pr
int
ln(
"Lengt
h:"+l
engt
h);
RunCode
Out
put
St
ri
ng:
Hel
l
o!Wor
ld
Lengt
h:12
Intheabov
eexample,thelengt
h()methodcalcul
atesthetot
alnumberofchar
act
ersi
nthe
str
ingandret
urnsi
t.Tolearnmore,v
isitJav
aSt r
inglengt
h()
.
2.Joi
nTwoJav
aSt
ri
ngs
Wecanj
oint
wost
ri
ngsi
nJav
ausi
ngt
heconcat
()met
hod.Forexampl
e,
cl
assMai
n{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
/
/cr
eat
efi
rstst
ri
ng
St
ri
ngf
ir
st="
Jav
a";
Sy
stem.
out
.pr
int
ln(
"Fi
rstSt
ri
ng:
"+f
ir
st)
;
/
/cr
eat
esecond
St
ri
ngsecond="
Progr
ammi
ng"
;
Sy
stem.
out
.pr
int
ln(
"SecondSt
ri
ng:
"+second)
;
/
/joi
ntwost
ri
ngs
St
ri
ngj
oinedSt
ri
ng=f
ir
st.
concat
(second)
;
Sy
stem.
out
.pr
int
ln(
"Joi
nedSt
ri
ng:
"+j
oinedSt
ri
ng)
;
RunCode
Out
put
Fi
rstSt
ri
ng:
Jav
a
SecondSt
ri
ng:
Progr
ammi
ng
Joi
nedSt
ri
ng:
Jav
aPr
ogr
ammi
ng
Intheaboveexampl
e,wehav
ecr
eat
edt
wost
ri
ngsnamedf
ir
standsecond.Not
icet
he
statement
,
St
ri
ngj
oinedSt
ri
ng=f
ir
st.
concat
(second)
;
Here,t
heconcat
()met
hodj
oinst
hesecondst
ri
ngt
othef
ir
stst
ri
ngandassi
gnsi
ttot
he
j
oinedStr
ingv
ari
abl
e.
Wecanal soj
oint
wost
ri
ngsusi
ngt
he+oper
atori
nJav
a.Tol
ear
nmor
e,v
isi
tJav
aSt
ri
ng
concat
().
3.Compar
etwoSt
ri
ngs
InJav
a, wecanmakecompar
isonsbet
weent
wost
ri
ngsusi
ngt
heequal
s()met
hod.For
example,
cl
assMai
n{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
/
/cr
eat
e3st
ri
ngs
St
ri
ngf
ir
st="
jav
apr
ogr
ammi
ng"
;
St
ri
ngsecond="
jav
apr
ogr
ammi
ng"
;
St
ri
ngt
hir
d="
pyt
honpr
ogr
ammi
ng"
;
/
/compar
efi
rstandsecondst
ri
ngs
bool
eanr
esul
t1=f
ir
st.
equal
s(second)
;
Sy
stem.
out
.pr
int
ln(
"St
ri
ngsf
ir
standsecondar
eequal
:"+r
esul
t1)
;
/
/compar
efi
rstandt
hir
dst
ri
ngs
bool
eanr
esul
t2=f
ir
st.
equal
s(t
hir
d);
Sy
stem.
out
.pr
int
ln(
"St
ri
ngsf
ir
standt
hir
dar
eequal
:"+r
esul
t2)
;
RunCode
Out
put
St
ri
ngsf
ir
standsecondar
eequal
:tr
ue
St
ri
ngsf
ir
standt
hir
dar
eequal
:fal
se
I
ntheaboveexample,wehavecreated3st
ri
ngsnamedf i
rst
,second,
andt
hir
d.Her
e,wear
e
usi
ngtheequal
()methodtocheckifonest
ri
ngisequal
toanother.
Theequals(
)methodcheckst
hecont
entofst
ri
ngswhi
l
ecompar
ingt
hem.Tol
ear
nmor
e,v
isi
t
JavaSt
ringequal
s()
.
Note:
Wecanalsocomparet
wost ri
ngsusingt
he==oper at
ori
nJava.However,
thi
sappr
oachi
s
di
ff
erentt
hant
heequal
s()method.Tolear
nmor e,
visi
tJavaSt
ri
ng==v sequal
s()
.
Escapechar
act
eri
nJav
aSt
ri
ngs
Theescapechar
act
eri
susedt
oescapesomeoft
hechar
act
erspr
esenti
nsi
deast
ri
ng.
Supposeweneedt
oincl
udedoubl
equot
esi
nsi
deast
ri
ng.
/
/incl
udedoubl
equot
e
St
ri
ngexampl
e="
Thi
sist
he"
Str
ing"cl
ass"
;
Si
ncestri
ngsar
erepresentedbydoubl
equot
es,
thecompi
l
erwi
l
ltr
eat"
Thi
sist
he"ast
hest
ri
ng.
Hence,
theabovecodewi l
lcauseaner
ror
.
Tosol
vet
hisi
ssue,
weuset
heescapechar
act
er\i
nJav
a.Forexampl
e,
/
/uset
heescapechar
act
er
St
ri
ngexampl
e="
Thi
sist
he\
"St
ri
ng\
"cl
ass.
";
Nowescapechar
act
erst
ell
thecompi
l
ert
oescapedoubl
equot
esandr
eadt
hewhol
etext
.
Jav
aSt
ri
ngsar
eImmut
abl
e
InJava,st
ri
ngsar
eimmut
abl
e.Thi
smeans,
oncewecr
eat
east
ri
ng,
wecannotchanget
hat
str
ing.
Tounder
standi
tmor
edeepl
y,consi
deranexampl
e:
/
/cr
eat
east
ri
ng
St
ri
ngexampl
e="
Hel
l
o!"
;
Her
e,wehav
ecr
eat
edast
ri
ngv
ari
abl
enamedexampl
e.Thev
ari
abl
ehol
dst
hest
ri
ng"
Hel
l
o!"
.
Nowsupposewewantt
ochanget
hest
ri
ng.
/
/addanot
herst
ri
ng"
Wor
ld"
/
/tot
hepr
evi
oust
ri
ngexampl
e
exampl
e=exampl
e.concat
("Wor
ld"
);
Her
e,wear
eusi
ngt
heconcat
()met
hodt
oaddanot
herst
ri
ngWor
ldt
othepr
evi
ousst
ri
ng.
I
tlooksl
i
kewear
eabl
etochanget
hev
alueoft
hepr
evi
ousst
ri
ng.Howev
er,
thi
sisnott
rue.
Let
'sseewhathashappenedher
e,
JVM t
akest
hef
ir
stst
ri
ng"
Hel
l
o!"
cr
eat
esanewst
ri
ngbyaddi
ng"
Wor
ld"t
othef
ir
stst
ri
ng
assi
gnt
henewst
ri
ng"
Hel
l
o!Wor
ld"t
otheexampl
evar
iabl
e
t
hef
ir
stst
ri
ng"
Hel
l
o!"r
emai
nsunchanged
Cr
eat
ingst
ri
ngsusi
ngt
henewkey
wor
d
Sof
arwehav
ecr
eat
edst
ri
ngsl
i
kepr
imi
ti
vet
ypesi
nJav
a.
Sincestr
ingsi
nJav
aar
eobj
ect
s,wecancr
eat
est
ri
ngsusi
ngt
henewkey
wor
daswel
l
.For
example,
/
/cr
eat
east
ri
ngusi
ngt
henewkey
wor
d
St
ri
ngname=newSt
ri
ng(
"Jav
aSt
ri
ng"
);
I
ntheabov
eexampl
e,wehav
ecr
eat
edast
ri
ngnameusi
ngt
henewkey
wor
d.
Here,
whenwecr eat
eastri
ngobject
,theSt
ri
ng(
)const
ruct
ori
sinv
oked.Tol
ear
nmor
eabout
const
ruct
or,
visi
tJavaConst
ruct
or.
Note:
TheStri
ngcl assprov
idesv
ari
ousotherconst
ruct
orst
ocr
eat
est
ri
ngs.Tol
ear
nmor
e,v
isi
t
Jav
aSt r
ing(
offi
cialJavadocument
ati
on)
.
Exampl
e:Cr
eat
eJav
aSt
ri
ngsusi
ngt
henewkey
wor
d
cl
assMai
n{
publ
i
cst
ati
cvoi
dmai
n(St
ri
ng[
]ar
gs){
/
/cr
eat
east
ri
ngusi
ngnew
St
ri
ngname=newSt
ri
ng(
"Jav
aSt
ri
ng"
);
Sy
stem.
out
.pr
int
ln(
name)
;//pr
intJav
aSt
ri
ng
RunCode
Cr
eat
eSt
ri
ngusi
ngl
i
ter
alsv
snewkey
wor
d
Nowthatweknowhowst ri
ngsarecr
eat
edusi
ngst
ri
ngl
i
ter
alsandt
henewkey
wor
d,l
et'
ssee
whati
sthemajordi
ff
erencebet
weenthem.
I
nJava,
theJVM maintai
nsast r
ingpool
tost
oreal
lofi
tsst
ri
ngsi
nsi
det
hememor
y.Thest
ri
ng
pool
hel
psinreusi
ngthestr
ings.
1.Whi
l
ecr
eat
ingst
ri
ngsusi
ngst
ri
ngl
i
ter
als,
St
ri
ngexampl
e="
Jav
a";
Her
e,wearedi
rect
lypr
ovi
dingthev
alueofthestr
ing(
Jav
a).Hence,
thecompi
l
erf
ir
stchecks
t
hestr
ingpool
toseeift
hestri
ngal
readyexist
s.
I
fthest
ri
ngalr
eadyexi
sts,t
henewstri
ngisnotcr
eat
ed.I
nst
ead,
thenewr
efer
ence,
exampl
e
poi
ntst
othealr
eadyexi
stedst
ri
ng(Java)
.
I
fthest
ri
ngdoesn'
texi
st,
thenewst
ri
ng(
Jav
aiscr
eat
ed.
2.Whi
l
ecr
eat
ingst
ri
ngsusi
ngt
henewkey
wor
d,
St
ri
ngexampl
e=newSt
ri
ng(
"Jav
a")
;
Her
e,t
hev al
ueoft
hest r
ingisnotdi
rect
lypr
ovi
ded.Hence,
anew"
Jav
a"st
ri
ngi
scr
eat
edev
en
t
hough"Java"i
sal
readypresenti
nsi
dethememor ypool
.
Met
hodsofJav
aSt
ri
ng
Besi
dest
hosementi
onedabov
e,t
her
ear
evar
iousst
ri
ngmet
hodspr
esenti
nJav
a.Her
ear
e
someoft
hosemethods:
Met
hods Descr
ipt
ion
cont
ains(
) checkswhet
hert
hest
ri
ngcont
ainsasubst
ri
ng
subst
ri
ng(
) r
etur
nst
hesubst
ri
ngoft
hest
ri
ng
j
oin(
)joi
nthegi
venst
ri
ngsusi
ngt
hedel
i
mit
er
r
epl
ace(
) r
epl
acest
hespeci
fi
edol
dchar
act
erwi
tht
hespeci
fi
ednewchar
act
er
r
epl
aceAl
l
() r
epl
acesal
lsubst
ri
ngsmat
chi
ngt
her
egexpat
ter
n
r
epl
aceFi
rst
()r
epl
acet
hef
ir
stmat
chi
ngsubst
ri
ng
char
At(
) r
etur
nst
hechar
act
erpr
esenti
nthespeci
fi
edl
ocat
ion
get
Byt
es(
) conv
ert
sthest
ri
ngt
oanar
rayofby
tes
i
ndexOf
() r
etur
nst
heposi
ti
onoft
hespeci
fi
edchar
act
eri
nthest
ri
ng
compar
eTo(
)compar
est
wost
ri
ngsi
nthedi
cti
onar
yor
der
compar
eToI
gnor
eCase(
) compar
est
wost
ri
ngsi
gnor
ingcasedi
ff
erences
t
ri
m()r
emov
esanyl
eadi
ngandt
rai
l
ingwhi
tespaces
f
ormat
() r
etur
nsaf
ormat
tedst
ri
ng
spl
i
t()br
eakst
hest
ri
ngi
ntoanar
rayofst
ri
ngs
t
oLower
Case(
) conv
ert
sthest
ri
ngt
olower
case
t
oUpper
Case(
)conv
ert
sthest
ri
ngt
oupper
case
v
alueOf
() r
etur
nst
hest
ri
ngr
epr
esent
ati
onoft
hespeci
fi
edar
gument
t
oChar
Arr
ay(
)conv
ert
sthest
ri
ngt
oacharar
ray
mat
ches(
) checkswhet
hert
hest
ri
ngmat
chest
hegi
venr
egex
st
art
sWi
th(
) checksi
fthest
ri
ngbegi
nswi
tht
hegi
venst
ri
ng
endsWi
th(
) checksi
fthest
ri
ngendswi
tht
hegi
venst
ri
ng
i
sEmpt
y() checkswhet
herast
ri
ngi
sempt
yofnot
i
nter
n() r
etur
nst
hecanoni
cal
repr
esent
ati
onoft
hest
ri
ng
cont
ent
Equal
s() checkswhet
hert
hest
ri
ngi
sequal
tochar
Sequence
hashCode(
) r
etur
nsahashcodef
ort
hest
ri
ng
subSequence(
) r
etur
nsasubsequencef
rom t
hest
ri
ng