Académique Documents
Professionnel Documents
Culture Documents
STRINGS CLASS
h t t p ://w w w .t u t orialsp oin t .com /java/java_st rin g s.h t m Copy r ight © tutor ialspoint.com
Strings, which are widely used in Java program m ing, are a sequence of characters. In the Java
program m ing language, strings are objects.
The Java platform provides the String class to create and m anipulate strings.
Cr e at i n g St r i n g s:
The m ost dire ct way to cr eate a string is to wr ite:
Whenever it encounter s a str ing literal in your code, th e com piler cr eates a Str ing object with its
value in this case, "Hello world! '.
As with any other object, you can cr eate String objects by using the new keyword and a
constructor. The Str ing class has eleven constr uctor s that allow you to provide the initial value of
the string using differ ent sour ces, such as an arr ay of characters.
hel l o.
N o t e: The Str ing class is im m utable, so that once it is created a Str ing object cannot be changed.
If ther e is a necessity to m ake a lot of m odifications to Str ings of characters, then you should use
String Buffer & String Builder Classes.
St r i n g Le n g t h :
Methods used to obtain inform ation about an object ar e known as accessor m ethods. One
accessor m ethod that you can use with str ings is the le ngth m ethod, which r etur ns the num ber of
characters contained in the str ing object.
St r i ng Lengt h i s : 17
Co n cat e n a t i n g St r i n g s:
st r i ng1. c onc at ( st r i ng2) ;
This returns a new str ing that is string1 with str ing2 ad ded to it at the end. You can also use the
concat m etho d with string liter als, as in:
Cr e at i n g F o r m at St r i n g s:
You have prin tf and for m at m ethods to pr int output wit h for m atted num bers. The String class has
an equivalent class m ethod, form at, that r etur ns a Str ing object r ather than a Pr intStr eam object.
Using Str ing's static form at m ethod allows you to cr eat e a fo rm atted string that you can r euse, as
opposed to a one-tim e pr int statem ent. For exam ple, instead of:
St r i ng f s;
f s = St r i ng. f or mat ( " The v al ue of t he f l oat v ar i abl e i s " +
" %f , whi l e t he v al ue of t he i nt eger " +
" v ar i abl e i s %d, and t he st r i ng " +
" i s %s" , f l oat Var , i nt Var , st r i ngVar ) ;
Sy st em. out . pr i nt l n( f s) ;
St r i n g M e t h o d s:
Her e is the list of m ethods supported by Str ing class:
SN M et h o d s w i t h D escr i p t i o n
1
char char Atintindex
2
int com par eToObjecto
3
int com par eToStringanotherString
4
int com par eToIgnor eCaseStringstr
5
Str ing concatStringstr
6
boolean contentEqualsStringBuffersb
Return s true if and only if this String r epr esents the sam e sequence of char acter s as the
specified StringBuffer .
7
static String copyValueOfchar[]data
Return s a String that represents the character sequence in the ar ray specified.
8
static String copyValueOfchar[]data, intoffset, intcount
Return s a String that represents the character sequence in the ar ray specified.
9
boolean endsWithStringsuffix
10
boolean equalsObjectanObject
11
boolean equalsIgnor eCaseStringanotherString
Com par es this Str ing to another String, ignor ing case consider ations.
12
byte getBytes
Encodes this Str ing into a sequence of bytes using the platform 's default char set, stor ing
the r esult into a new byte ar ray.
13
byte[] getBytes(String char setNam e
Encodes this Str ing into a sequence of bytes using the nam ed charset, stor ing the result
into a new byte arr ay.
14
void ge tCharsintsrcBegin, intsrcEnd, char[]dst, intdstBegin
Copies character s from this string into the destin ation character arr ay.
15
int hashCode
16
int indexOfintch
Return s the index within this str ing of the fir st occur rence of the specified character .
17
int indexOfintch, intfromIndex
Return s the index within this str ing of the fir st occur rence of the specified character ,
starting the search at the specified index.
18
int indexOfStringstr
Return s the index within this str ing of the fir st occur rence of the specified substring.
19
int indexOfStringstr, intfromIndex
Return s the index within this str ing of the fir st occur rence of the specified substring,
starting at the specified index
20
Str ing intern
21
int lastIndexOfintch
Return s the index within this str ing of the last occurr ence of the specified character.
22
int lastIndexOfintch, intfromIndex
Return s the index within this str ing of the last occurr ence of the specified character,
search ing backwar d star ting at the specified ind ex.
23
int lastIndexOfStringstr
Return s the index within this str ing of the r ightm ost occur rence of the specified substring.
24
int lastIndexOfStringstr, intfromIndex
Return s the index within this str ing of the last occurr ence of the specified substr ing,
search ing backwar d star ting at the specified ind ex.
25
int length
26
boolean m atchesStringregex
Tells whether or not this string m atches the give n regular expr ession.
27
boolean r egionMatchesbooleanignoreCase, inttoffset, Stringother, intooffset, intlen
28
boolean r egionMatchesinttoffset, Stringother, intooffset, intlen
29
Str ing r eplacecharoldChar, charnewChar
Return s a new string r esulting fr om r eplacing all occur rences of oldChar in this str ing with
newCh ar.
30
Str ing r eplaceAll(String r egex, String replacem ent
Replaces each substring of this string that m atches the given r egular expr ession with the
given r eplacem ent.
31
Str ing r eplaceFir stStringregex, Stringreplacement
Replaces the first substr ing of this string that m atches the given regular expr ession with
the given replacem ent.
32
Str ing[] splitStringregex
Splits this str ing ar ound m atches of the given r egular expression.
33
Str ing[] splitStringregex, intlimit
Splits this str ing ar ound m atches of the given r egular expression.
34
boolean star tsWithStringprefix
Tests if this str ing star ts with the specified prefix beginning a specified index.
36
CharSe quence subSequenceintbeginIndex, intendIndex
Return s a new char acter sequence that is a sub sequence of this sequence.
37
Str ing substr ingintbeginIndex
38
Str ing substr ingintbeginIndex, intendIndex
39
char [] toChar Ar ray
40
Str ing toLowerCase
Converts all of the character s in this Str ing to lo wer case using the r ules of the default
locale.
41
Str ing toLowerCaseLocalelocale
Converts all of the character s in this Str ing to lo wer case using the r ules of the given
Locale.
42
Str ing toString
43
Str ing toUpperCase
Converts all of the character s in this Str ing to upper case using the rules of the default
locale.
44
Str ing toUpperCaseLocalelocale
Converts all of the character s in this Str ing to upper case using the rules of the given
Locale.
45
Str ing tr im
Return s a copy of the string, with leading and tra iling whitespace om itted.
46
static String valueOfprimitivedatatypex
Return s the string representation of the passed data type argum ent.