Vous êtes sur la page 1sur 7

JAVA - STRINGS

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:

St r i ng gr eet i ng = " Hel l o wor l d! " ;

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.

publ i c c l ass St r i ngDemo{

publ i c st at i c v oi d mai n( St r i ng ar gs[ ] ) {


c har [ ] hel l oAr r ay = { ' h' , ' e' , ' l ' , ' l ' , ' o' , ' . ' } ;
St r i ng hel l oSt r i ng = new St r i ng( hel l oAr r ay ) ;
Sy st em. out . pr i nt l n( hel l oSt r i ng ) ;
}
}

This would pr oduce the following result:

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.

Below given program is an exam ple of l en g t h , m etho d Str ing class.

publ i c c l ass St r i ngDemo {

publ i c st at i c v oi d mai n( St r i ng ar gs[ ] ) {


St r i ng pal i ndr ome = " Dot saw I was Tod" ;
i nt l en = pal i ndr ome. l engt h( ) ;
Sy st em. out . pr i nt l n( " St r i ng Lengt h i s : " + l en ) ;
}
}

This would pr oduce the following result:

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:

" My name i s " . c onc at ( " Zar a" ) ;

Strings are m or e com m only concatenated with the + operator, as in:

" Hel l o, " + " wor l d" + " ! "

which r esults in:

" Hel l o, wor l d! "

Let us look at the following exam ple:

publ i c c l ass St r i ngDemo {

publ i c st at i c v oi d mai n( St r i ng ar gs[ ] ) {


St r i ng st r i ng1 = " saw I was " ;
Sy st em. out . pr i nt l n( " Dot " + st r i ng1 + " Tod" ) ;
}
}

This would pr oduce the following result:

Dot saw I was Tod

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:

Sy st em. out . pr i nt f ( " 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 ) ;

you can write:

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

Com par es this Str ing to another Object.

3
int com par eToStringanotherString

Com par es two strings lexicogr aphically.

4
int com par eToIgnor eCaseStringstr

Com par es two strings lexicogr aphically, ignorin g case differences.

5
Str ing concatStringstr

Concatenates the specified str ing to the end of t his string.

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

Tests if this str ing ends with the specified suffix.

10
boolean equalsObjectanObject

Com par es this string to the specified object.

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

Return s a hash code for this string.

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

Return s a canonical repr esentation for the strin g object.

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

Return s the length of this string.

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

Tests if two str ing r egions ar e equal.

28
boolean r egionMatchesinttoffset, Stringother, intooffset, intlen

Tests if two str ing r egions ar e equal

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.


35
boolean star tsWithStringprefix, inttoffset

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

Return s a new string that is a substr ing of this string.

38
Str ing substr ingintbeginIndex, intendIndex

Return s a new string that is a substr ing of this string.

39
char [] toChar Ar ray

Converts this str ing to a new char acter 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

This object whichisalreadyastring! is itself returned.

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.

Loading [MathJax ]/jax /output/HTML-CSS/jax .js

Vous aimerez peut-être aussi