string (java platform se 6)

Upload: faisal-setia-putra

Post on 02-Jun-2018

228 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/11/2019 String (Java Platform SE 6)

    1/40

    Overview Package Class Use Tree Deprecated Index Help Java PlatformStandard Ed. 6PREV CLASS NEXT CLASS FRAMES NO FRAMES All Cl asses

    SUMMARY: NESTED | FIELD| CONSTR| METHOD DETAIL: FIELD| CONSTR| METHOD

    java.lang

    Class String

    j ava. l ang. Obj ect

    java.lang.String

    All Implemented Interfaces:

    Serializable, CharSequence, Comparable

    publ i c f i nal cl ass Stringextends Obj ecti mpl ements Ser i al i zabl e, Comparabl e, CharSequence

    The St r i ngclass represents character strings. All string literals in Java programs, such as "abc" , areimplemented as instances of this class.

    Strings are constant; their values cannot be changed after they are created. String buffers support mutablestrings. Because String objects are immutable they can be shared. For example:

    St r i ng str = "abc";

    is equivalent to:

    char dat a[ ] = {' a' , ' b' , ' c' }; St r i ng st r = new St r i ng( dat a) ;

    Here are some more examples of how strings can be used:

    Syst em. out . pr i nt l n( "abc") ; St r i ng cde = "cde"; System. out . pr i nt l n( "abc" + cde) ; St r i ng c = "abc". subst r i ng( 2, 3) ; St r i ng d = cde. subst r i ng( 1, 2) ;

    The class St r i ngincludes methods for examining individual characters of the sequence, for comparingstrings, for searching strings, for extracting substrings, and for creating a copy of a string with allcharacters translated to uppercase or to lowercase. Case mapping is based on the Unicode Standardversion specified by the Char act er class.

    The Java language provides special support for the string concatenation operator ( + ), and for conversionof other objects to strings. String concatenation is implemented through the St r i ngBui l der (orSt r i ngBuf f er ) class and its appendmethod. String conversions are implemented through the methodt oSt r i ng, defined by Obj ect and inherited by all classes in Java. For additional information on string

    concatenation and conversion, see Gosling, Joy, and Steele, The Java Language Specification.

    Unless otherwise noted, passing a nul l argument to a constructor or method in this class will cause aNul l Poi nt er Except i onto be thrown.

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    2/40

    A Str i ngrepresents a string in the UTF-16 format in which supplementary charactersare represented bysurrogate pairs(see the section Unicode Character Representationsin the Char act er class for moreinformation). Index values refer to char code units, so a supplementary character uses two positions in aSt r i ng.

    The St r i ngclass provides methods for dealing with Unicode code points (i.e., characters), in addition tothose for dealing with Unicode code units (i.e., char values).

    Since:

    JDK1.0See Also:

    Obj ect. t oSt r i ng( ) , St r i ngBuf f er , St r i ngBui l der , Char set , Serialized Form

    Field Summarys tat i c Comparat or CASE_INSENSITIVE_ORDER

    A Comparator that orders St r i ngobjects as by

    compareToI gnor eCase.

    Constructor SummaryString( )

    Initializes a newly created St r i ngobject so that it represents an empty character sequence.

    String(byte[] bytes)

    Constructs a new Str i ngby decoding the specified array of bytes using the platform's defaultcharset.

    String( byt e[ ] bytes, Char set charset )

    Constructs a new Str i ngby decoding the specified array of bytes using the specified charset.

    String( byte[] asci i , i nt hi byt e)

    Deprecated.This method does not properly convert bytes into characters. As of JDK 1.1, thepreferred way to do this is via the Stringconstructors that take a Charset, charset name, or that use

    the platform's default charset.

    String( byt e[ ] byt es, i nt of f set , i nt l engt h)

    Constructs a new Str i ngby decoding the specified subarray of bytes using the platform's defaultcharset.

    String( byte[] byt es, i nt of f set , i nt l engt h, Charset charset )

    Constructs a new Str i ngby decoding the specified subarray of bytes using the specified charset.String( byte[] asci i , i nt hi byt e, i nt of f set , i nt count )

    Deprecated.This method does not properly convert bytes into characters. As of JDK 1.1, thepreferred way to do this is via the Stringconstructors that take a Charset, charset name, or that use

    the platform's default charset.

    String( byte[] byt es, i nt of f set , i nt l engt h, Str i ng char set Name)

    Constructs a new Str i ngby decoding the specified subarray of bytes using the specified charset.

    String( byt e[ ] bytes, St r i ng charset Name)

    Constructs a new Str i ngby decoding the specified array of bytes using the specified charset.

    String( char [ ] val ue) Allocates a new St r i ngso that it represents the sequence of characters currently contained in thecharacter array argument.

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    3/40

    String( char [ ] val ue, i nt of f set , i nt count )

    Allocates a new St r i ngthat contains characters from a subarray of the character array argument.

    String( i nt [ ] codePoi nt s, i nt of f set , i nt count )

    Allocates a new St r i ngthat contains characters from a subarray of the Unicode code point arrayargument.

    String( St r i ng or i gi nal )

    Initializes a newly created St r i ngobject so that it represents the same sequence of characters as

    the argument; in other words, the newly created string is a copy of the argument string.String( St r i ngBuf f er buf f er )

    Allocates a new string that contains the sequence of characters currently contained in the stringbuffer argument.

    String( St r i ngBui l der bui l der )

    Allocates a new string that contains the sequence of characters currently contained in the stringbuilder argument.

    Method Summary char charAt( i nt i ndex)

    Returns the char value at the specified index.

    i nt codePointAt( i nt i ndex)

    Returns the character (Unicode code point) at the specified index.

    i nt codePointBefore( i nt i ndex)

    Returns the character (Unicode code point) before the specified index.

    i nt codePointCount( i nt begi nI ndex, i nt endI ndex)

    Returns the number of Unicode code points in the specified text range of thisSt r i ng.

    i nt compareTo( St r i ng anot herSt r i ng)

    Compares two strings lexicographically.

    i nt compareToIgnoreCase( Str i ng s t r )

    Compares two strings lexicographically, ignoring case differences.

    St r i ng concat( St r i ng s t r )

    Concatenates the specified string to the end of this string.

    bool ean contains( CharSequence s)

    Returns true if and only if this string contains the specified sequence of charvalues.

    bool ean contentEquals( CharSequence cs) Compares this string to the specified Char Sequence.

    bool ean contentEquals( St r i ngBuf f er sb)

    Compares this string to the specified St r i ngBuf f er .

    s tat i c St r i ng copyValueOf( char [ ] dat a)

    Returns a String that represents the character sequence in the array specified.

    s tat i c St r i ng copyValueOf( char[ ] dat a, i nt of f set , i nt count )

    Returns a String that represents the character sequence in the array specified.

    bool ean endsWith( St r i ng suf f i x)

    Tests if this string ends with the specified suffix.

    bool ean equals( Obj ect anObj ect )

    Compares this string to the specified object.

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    4/40

    bool ean equalsIgnoreCase( St r i ng anot her St r i ng)

    Compares this St r i ngto another Str i ng, ignoring case considerations.

    s tat i c St r i ng format( Local e l , St r i ng f ormat, Obj ect . . . args)

    Returns a formatted string using the specified locale, format string, and arguments.

    s tat i c St r i ng format( St r i ng f or mat , Obj ect . . . args)

    Returns a formatted string using the specified format string and arguments.

    byte[ ] getBytes( )

    Encodes this St r i nginto a sequence of bytes using the platform's default charset,storing the result into a new byte array.

    byte[ ] getBytes( Char set char set )

    Encodes this St r i nginto a sequence of bytes using the given charset, storing theresult into a new byte array.

    voi d getBytes( i nt srcBegi n, i nt srcEnd, byt e[ ] dst , i nt dst Begi n)

    Deprecated.This method does not properly convert characters into bytes. As ofJDK 1.1, the preferred way to do this is via the getBytes()method, which uses the

    platform's default charset.

    byte[ ] getBytes( St r i ng charset Name) Encodes this St r i nginto a sequence of bytes using the named charset, storing theresult into a new byte array.

    voi d getChars( i nt srcBegi n, i nt srcEnd, char [ ] dst , i nt dst Begi n)

    Copies characters from this string into the destination character array.

    i nt hashCode( )

    Returns a hash code for this string.

    i nt indexOf( i nt ch)

    Returns the index within this string of the first occurrence of the specifiedcharacter.

    i nt indexOf( i nt ch, i nt f r omI ndex)

    Returns the index within this string of the first occurrence of the specifiedcharacter, starting the search at the specified index.

    i nt indexOf( St r i ng s t r )

    Returns the index within this string of the first occurrence of the specifiedsubstring.

    i nt indexOf( St r i ng st r , i nt f r omI ndex)

    Returns the index within this string of the first occurrence of the specifiedsubstring, starting at the specified index.

    St r i ng intern( ) Returns a canonical representation for the string object.

    bool ean isEmpty( )

    Returns trueif, and only if, l engt h( ) is 0.

    i nt lastIndexOf( i nt ch)

    Returns the index within this string of the last occurrence of the specifiedcharacter.

    i nt lastIndexOf( i nt ch, i nt f r omI ndex)

    Returns the index within this string of the last occurrence of the specifiedcharacter, searching backward starting at the specified index.

    i nt lastIndexOf( St r i ng s t r )

    Returns the index within this string of the rightmost occurrence of the specifiedsubstring.

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    5/40

  • 8/11/2019 String (Java Platform SE 6)

    6/40

    St r i ng toLowerCase( Local e l ocal e)

    Converts all of the characters in this Str i ngto lower case using the rules of thegiven Local e.

    St r i ng toString( )

    This object (which is already a string!) is itself returned.

    St r i ng toUpperCase( )

    Converts all of the characters in this Str i ngto upper case using the rules of the

    default locale. St r i ng toUpperCase( Local e l ocal e)

    Converts all of the characters in this Str i ngto upper case using the rules of thegiven Local e.

    St r i ng trim( )

    Returns a copy of the string, with leading and trailing whitespace omitted.

    s tat i c St r i ng valueOf( bool ean b)

    Returns the string representation of the bool eanargument.

    s tat i c St r i ng valueOf( char c)

    Returns the string representation of the char argument.s tat i c St r i ng valueOf( char [ ] dat a)

    Returns the string representation of the char array argument.

    s tat i c St r i ng valueOf( char[ ] dat a, i nt of f set , i nt count )

    Returns the string representation of a specific subarray of the char array argument.

    s tat i c St r i ng valueOf( doubl e d)

    Returns the string representation of the doubl eargument.

    s tat i c St r i ng valueOf( f l oat f )

    Returns the string representation of the f l oat argument.

    s tat i c St r i ng valueOf( i nt i ) Returns the string representation of the i nt argument.

    s tat i c St r i ng valueOf( l ong l )

    Returns the string representation of the l ongargument.

    s tat i c St r i ng valueOf( Obj ect obj )

    Returns the string representation of the Obj ect argument.

    Methods inherited from class java.lang.Object

    cl one, f i nal i ze, getCl ass, not i f y, not i f yAl l , wai t , wai t , wai t

    Field Detail

    CASE_INSENSITIVE_ORDER

    publ i c stat i c f i nal Compar at or CASE_INSENSITIVE_ORDER

    A Comparator that orders St r i ngobjects as by compar eToI gnor eCase. This comparator isserializable.

    Note that this Comparator does nottake locale into account, and will result in an unsatisfactoryordering for certain locales. The java.text package provides Collatorsto allow locale-sensitiveordering.

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    7/40

    Since:

    1.2See Also:

    Col l at or . compar e( St r i ng, St r i ng)

    Constructor Detail

    String

    publ i c String( )

    Initializes a newly created St r i ngobject so that it represents an empty character sequence. Notethat use of this constructor is unnecessary since Strings are immutable.

    String

    publ i c String( Str i ng or i gi nal )

    Initializes a newly created St r i ngobject so that it represents the same sequence of characters as theargument; in other words, the newly created string is a copy of the argument string. Unless anexplicit copy of ori gi nal is needed, use of this constructor is unnecessary since Strings areimmutable.

    Parameters:

    or i gi nal - A Str i ng

    String

    publ i c String( char [ ] val ue)

    Allocates a new St r i ngso that it represents the sequence of characters currently contained in thecharacter array argument. The contents of the character array are copied; subsequent modificationof the character array does not affect the newly created string.

    Parameters:

    val ue- The initial value of the string

    String

    publ i c String( char [ ] val ue, i nt of f set , i nt count )

    Allocates a new St r i ngthat contains characters from a subarray of the character array argument.The of f set argument is the index of the first character of the subarray and the count argumentspecifies the length of the subarray. The contents of the subarray are copied; subsequent

    modification of the character array does not affect the newly created string.

    Parameters:

    val ue- Array that is the source of characters

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    8/40

    of f set - The initial offsetcount - The length

    Throws:

    I ndexOutOf BoundsExcept i on- If the of f set and count arguments index characters outsidethe bounds of the val uearray

    String

    publ i c String( i nt [ ] codePoi nt s, i nt of f set , i nt count )

    Allocates a new St r i ngthat contains characters from a subarray of the Unicode code point arrayargument. The of f set argument is the index of the first code point of the subarray and the countargument specifies the length of the subarray. The contents of the subarray are converted to char s;subsequent modification of the i nt array does not affect the newly created string.

    Parameters:codePoi nt s - Array that is the source of Unicode code pointsof f set - The initial offsetcount - The length

    Throws:

    I l l egal Ar gument Except i on- If any invalid Unicode code point is found in codePoi nt sI ndexOutOf BoundsExcept i on- If the of f set and count arguments index characters outsidethe bounds of the codePoi nt sarray

    Since:

    1.5

    String

    @Depr ecat ed

    publ i c String(byt e[ ] asci i , i nt hi byt e, i nt of f set , i nt count )

    Deprecated.This method does not properly convert bytes into characters. As of JDK 1.1, the

    preferred way to do this is via the Stringconstructors that take a Charset, charset name, or thatuse the platform's default charset.

    Allocates a new St r i ngconstructed from a subarray of an array of 8-bit integer values.

    The of f set argument is the index of the first byte of the subarray, and the count argument specifiesthe length of the subarray.

    Each bytein the subarray is converted to a char as specified in the method above.

    Parameters:

    asci i - The bytes to be converted to charactershi byt e- The top 8 bits of each 16-bit Unicode code unitof f set - The initial offsetcount - The length

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    9/40

    Throws:

    I ndexOutOf BoundsExcept i on- If the of f set or count argument is invalidSee Also:

    St r i ng( byt e[ ] , i nt ) , St r i ng( byt e[ ] , i nt , i nt , j ava. l ang. St r i ng) , St r i ng( byt e[ ] ,i nt , i nt , j ava. ni o. char set . Char set ) , St r i ng( byt e[ ] , i nt , i nt ) , St ri ng( byt e[ ] ,

    j ava. l ang. Str i ng) , St r i ng( byt e[ ] , j ava. ni o. char set . Char set ) , St ri ng( byt e[ ] )

    String

    @Depr ecat ed

    publ i c String(byt e[ ] asci i , i nt hi byt e)

    Deprecated.This method does not properly convert bytes into characters. As of JDK 1.1, thepreferred way to do this is via the Stringconstructors that take a Charset, charset name, or that

    use the platform's default charset.

    Allocates a new St r i ngcontaining characters constructed from an array of 8-bit integer values.Each character cin the resulting string is constructed from the corresponding component bin the

    byte array such that:

    c == ( char ) ( ( ( hi byte & 0xf f )

  • 8/11/2019 String (Java Platform SE 6)

    10/40

    char set Name- The name of a supported charsetThrows:

    Unsupport edEncodi ngExcept i on- If the named charset is not supportedI ndexOutOf BoundsExcept i on- If the of f set and l engt harguments index characters outsidethe bounds of the bytesarray

    Since:

    JDK1.1

    String

    publ i c String(byte[] bytes, i nt of f set , i nt l engt h, Charset char set )

    Constructs a new St r i ngby decoding the specified subarray of bytes using the specified charset.The length of the new St r i ngis a function of the charset, and hence may not be equal to the length

    of the subarray.

    This method always replaces malformed-input and unmappable-character sequences with thischarset's default replacement string. The Charset Decoder class should be used when more controlover the decoding process is required.

    Parameters:

    byt es- The bytes to be decoded into charactersof f set - The index of the first byte to decodel engt h- The number of bytes to decode

    char set - The charsetto be used to decode the byt esThrows:I ndexOutOf BoundsExcept i on- If the of f set and l engt harguments index characters outsidethe bounds of the bytesarray

    Since:

    1.6

    String

    publ i c String(byte[] bytes, Str i ng charset Name) t hr ows Unsupport edEncodi ngExcept i on

    Constructs a new St r i ngby decoding the specified array of bytes using the specified charset. Thelength of the new St r i ngis a function of the charset, and hence may not be equal to the length ofthe byte array.

    The behavior of this constructor when the given bytes are not valid in the given charset isunspecified. The CharsetDecoder class should be used when more control over the decoding

    process is required.

    Parameters:

    byt es- The bytes to be decoded into characterschar set Name- The name of a supported charset

    Throws:

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    11/40

    Unsupport edEncodi ngExcept i on- If the named charset is not supportedSince:

    JDK1.1

    String

    publ i c String(byte[] bytes, Charset char set )

    Constructs a new St r i ngby decoding the specified array of bytes using the specified charset. Thelength of the new St r i ngis a function of the charset, and hence may not be equal to the length ofthe byte array.

    This method always replaces malformed-input and unmappable-character sequences with thischarset's default replacement string. The Charset Decoder class should be used when more controlover the decoding process is required.

    Parameters:byt es- The bytes to be decoded into characterschar set - The charsetto be used to decode the byt es

    Since:

    1.6

    String

    publ i c String(byte[] bytes, i nt of f set , i nt l engt h)

    Constructs a new St r i ngby decoding the specified subarray of bytes using the platform's defaultcharset. The length of the new St r i ngis a function of the charset, and hence may not be equal tothe length of the subarray.

    The behavior of this constructor when the given bytes are not valid in the default charset isunspecified. The CharsetDecoder class should be used when more control over the decoding

    process is required.

    Parameters:byt es- The bytes to be decoded into charactersof f set - The index of the first byte to decodel engt h- The number of bytes to decode

    Throws:

    I ndexOutOf BoundsExcept i on- If the of f set and the l engt harguments index charactersoutside the bounds of the bytesarray

    Since:

    JDK1.1

    String

    publ i c String(byte[] bytes)

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    12/40

    Constructs a new St r i ngby decoding the specified array of bytes using the platform's defaultcharset. The length of the new St r i ngis a function of the charset, and hence may not be equal tothe length of the byte array.

    The behavior of this constructor when the given bytes are not valid in the default charset isunspecified. The CharsetDecoder class should be used when more control over the decoding

    process is required.

    Parameters:

    byt es- The bytes to be decoded into charactersSince:

    JDK1.1

    String

    publ i c String( St r i ngBuf f er buf f er )

    Allocates a new string that contains the sequence of characters currently contained in the stringbuffer argument. The contents of the string buffer are copied; subsequent modification of the stringbuffer does not affect the newly created string.

    Parameters:

    buf f er - A St r i ngBuf f er

    String

    publ i c String( St r i ngBui l der bui l der )

    Allocates a new string that contains the sequence of characters currently contained in the stringbuilder argument. The contents of the string builder are copied; subsequent modification of thestring builder does not affect the newly created string.

    This constructor is provided to ease migration to St r i ngBui l der . Obtaining a string from a stringbuilder via the t oSt r i ngmethod is likely to run faster and is generally preferred.

    Parameters:

    bui l der - A St r i ngBui l derSince:

    1.5

    Method Detail

    length

    publ i c i nt length( )

    Returns the length of this string. The length is equal to the number of Unicode code unitsin thestring.

    Specified by:

    l engt hin interface CharSequence

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    13/40

    Returns:

    the length of the sequence of characters represented by this object.

    isEmpty

    publ i c bool ean isEmpty( )

    Returns trueif, and only if, l engt h( ) is 0.

    Returns:

    trueif l engt h( ) is 0, otherwise f al seSince:

    1.6

    charAt

    publ i c char charAt( i nt i ndex)

    Returns the char value at the specified index. An index ranges from 0to l engt h( ) - 1. The firstchar value of the sequence is at index 0, the next at index 1, and so on, as for array indexing.

    If the char value specified by the index is a surrogate, the surrogate value is returned.

    Specified by:

    char At in interface CharSequenceParameters:

    i ndex- the index of the char value.Returns:the char value at the specified index of this string. The first char value is at index 0.

    Throws:

    I ndexOutOf BoundsExcept i on- if the i ndexargument is negative or not less than the lengthof this string.

    codePointAt

    publ i c i nt codePointAt( i nt i ndex)

    Returns the character (Unicode code point) at the specified index. The index refers to char values(Unicode code units) and ranges from 0to l engt h( ) - 1.

    If the char value specified at the given index is in the high-surrogate range, the following index isless than the length of this Str i ng, and the char value at the following index is in the low-surrogaterange, then the supplementary code point corresponding to this surrogate pair is returned.Otherwise, the char value at the given index is returned.

    Parameters:

    i ndex- the index to the char valuesReturns:

    the code point value of the character at the i ndexThrows:

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    14/40

    I ndexOutOf BoundsExcept i on- if the i ndexargument is negative or not less than the lengthof this string.

    Since:

    1.5

    codePointBefore

    publ i c i nt codePointBefore( i nt i ndex)

    Returns the character (Unicode code point) before the specified index. The index refers to charvalues (Unicode code units) and ranges from 1to l engt h.

    If the char value at ( i ndex - 1) is in the low-surrogate range, ( i ndex - 2) is not negative, andthe char value at ( i ndex - 2) is in the high-surrogate range, then the supplementary code pointvalue of the surrogate pair is returned. If the char value at i ndex - 1is an unpaired low-surrogateor a high-surrogate, the surrogate value is returned.

    Parameters:

    i ndex- the index following the code point that should be returnedReturns:

    the Unicode code point value before the given index.Throws:

    I ndexOutOf BoundsExcept i on- if the i ndexargument is less than 1 or greater than the lengthof this string.

    Since:

    1.5

    codePointCount

    publ i c i nt codePointCount( i nt begi nI ndex, i nt endI ndex)

    Returns the number of Unicode code points in the specified text range of this St r i ng. The textrange begins at the specified begi nI ndexand extends to the char at index endI ndex - 1. Thus thelength (in char s) of the text range is endI ndex- begi nI ndex. Unpaired surrogates within the textrange count as one code point each.

    Parameters:

    begi nI ndex- the index to the first char of the text range.endI ndex- the index after the last char of the text range.

    Returns:

    the number of Unicode code points in the specified text rangeThrows:

    I ndexOutOf BoundsExcept i on- if the begi nI ndexis negative, or endI ndexis larger than thelength of this St r i ng, or begi nI ndexis larger than endI ndex.

    Since:

    1.5

    offsetByCodePoints

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    15/40

    publ i c i nt offsetByCodePoints( i nt i ndex, i nt codePoi nt Of f set )

    Returns the index within this St r i ngthat is offset from the given i ndexby codePoi nt Of f set codepoints. Unpaired surrogates within the text range given by i ndexand codePoi nt Of f set count asone code point each.

    Parameters:

    i ndex- the index to be offsetcodePoi nt Of f set - the offset in code points

    Returns:

    the index within this Str i ngThrows:

    I ndexOutOf BoundsExcept i on- if i ndexis negative or larger then the length of this St r i ng,or if codePoi nt Of f set is positive and the substring starting with i ndexhas fewer thancodePoi nt Of f set code points, or if codePoi nt Of f set is negative and the substring beforei ndexhas fewer than the absolute value of codePoi nt Of f set code points.

    Since:

    1.5

    getChars

    publ i c voi d getChars( i nt srcBegi n, i nt sr cEnd, char [ ] dst , i nt dst Begi n)

    Copies characters from this string into the destination character array.

    The first character to be copied is at index sr cBegi n; the last character to be copied is at indexsr cEnd- 1(thus the total number of characters to be copied is sr cEnd- sr cBegi n). The charactersare copied into the subarray of dst starting at index dst Begi nand ending at index:

    dst begi n + ( sr cEnd- sr cBegi n) - 1

    Parameters:

    sr cBegi n- index of the first character in the string to copy.sr cEnd- index after the last character in the string to copy.

    dst - the destination array.dst Begi n- the start offset in the destination array.

    Throws:

    I ndexOutOf BoundsExcept i on- If any of the following is true:sr cBegi nis negative.sr cBegi nis greater than sr cEndsr cEndis greater than the length of this stringdst Begi nis negativedst Begi n+( sr cEnd- sr cBegi n) is larger than dst . l engt h

    getBytes

    @Depr ecat ed

    publ i c voi d getBytes( i nt srcBegi n,

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    16/40

    i nt sr cEnd, byte[ ] dst , i nt dst Begi n)

    Deprecated.This method does not properly convert characters into bytes. As of JDK 1.1, thepreferred way to do this is via the getBytes()method, which uses the platform's default charset.

    Copies characters from this string into the destination byte array. Each byte receives the 8 low-order

    bits of the corresponding character. The eight high-order bits of each character are not copied anddo not participate in the transfer in any way.

    The first character to be copied is at index sr cBegi n; the last character to be copied is at indexsr cEnd- 1. The total number of characters to be copied is sr cEnd- sr cBegi n. The characters,converted to bytes, are copied into the subarray of dst starting at index dst Begi nand ending atindex:

    dst begi n + ( sr cEnd- sr cBegi n) - 1

    Parameters:sr cBegi n- Index of the first character in the string to copysr cEnd- Index after the last character in the string to copydst - The destination arraydst Begi n- The start offset in the destination array

    Throws:

    I ndexOutOf BoundsExcept i on- If any of the following is true:sr cBegi nis negativesr cBegi nis greater than sr cEndsr cEndis greater than the length of this String

    dst Begi nis negativedst Begi n+( sr cEnd- sr cBegi n) is larger than dst . l engt h

    getBytes

    publ i c byt e[ ] getBytes( St r i ng charset Name) t hrows Unsuppor t edEncodi ngExcept i on

    Encodes this Str i nginto a sequence of bytes using the named charset, storing the result into a newbyte array.

    The behavior of this method when this string cannot be encoded in the given charset is unspecified.The Char set Encoder class should be used when more control over the encoding process isrequired.

    Parameters:

    char set Name- The name of a supported charsetReturns:

    The resultant byte arrayThrows:

    Unsupport edEncodi ngExcept i on- If the named charset is not supportedSince:

    JDK1.1

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    17/40

    getBytes

    publ i c byt e[ ] getBytes( Char set charset )

    Encodes this Str i nginto a sequence of bytes using the given charset, storing the result into a newbyte array.

    This method always replaces malformed-input and unmappable-character sequences with thischarset's default replacement byte array. The Char set Encoder class should be used when morecontrol over the encoding process is required.

    Parameters:

    char set - The Charsetto be used to encode the St r i ngReturns:

    The resultant byte arraySince:

    1.6

    getBytes

    publ i c byt e[ ] getBytes( )

    Encodes this Str i nginto a sequence of bytes using the platform's default charset, storing the resultinto a new byte array.

    The behavior of this method when this string cannot be encoded in the default charset isunspecified. The Char setEncoder class should be used when more control over the encoding

    process is required.

    Returns:

    The resultant byte arraySince:

    JDK1.1

    equals

    publ i c bool ean equals( Obj ect anObj ect )

    Compares this string to the specified object. The result is trueif and only if the argument is notnul l and is a St r i ngobject that represents the same sequence of characters as this object.

    Overrides:

    equal sin class Obj ectParameters:

    anObj ect - The object to compare this Str i ngagainstReturns:

    trueif the given object represents a St r i ngequivalent to this string, f al seotherwise

    See Also:compareTo( St r i ng) , equal sI gnor eCase( St r i ng)

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    18/40

    contentEquals

    publ i c bool ean contentEquals( St r i ngBuf f er sb)

    Compares this string to the specified St r i ngBuf f er . The result is trueif and only if this St r i ngrepresents the same sequence of characters as the specified St r i ngBuf f er .

    Parameters:

    sb- The St r i ngBuf f er to compare this St r i ngagainstReturns:

    trueif this St r i ngrepresents the same sequence of characters as the specifiedSt r i ngBuf f er , f al seotherwise

    Since:

    1.4

    contentEquals

    publ i c bool ean contentEquals( CharSequence cs)

    Compares this string to the specified CharSequence. The result is trueif and only if this St r i ngrepresents the same sequence of char values as the specified sequence.

    Parameters:

    cs - The sequence to compare this St r i ngagainstReturns:

    trueif this St r i ngrepresents the same sequence of char values as the specified sequence,f al seotherwise

    Since:1.5

    equalsIgnoreCase

    publ i c bool ean equalsIgnoreCase( St r i ng anotherSt r i ng)

    Compares this St r i ngto another St r i ng, ignoring case considerations. Two strings are consideredequal ignoring case if they are of the same length and corresponding characters in the two strings

    are equal ignoring case.Two characters c1and c2are considered the same ignoring case if at least one of the following istrue:

    The two characters are the same (as compared by the ==operator)Applying the method Character . t oUpper Case( char) to each character produces the sameresultApplying the method Character . t oLower Case( char) to each character produces the sameresult

    Parameters:anot her St r i ng- The St r i ngto compare this St r i ngagainst

    Returns:

    trueif the argument is not nul l and it represents an equivalent St r i ngignoring case; f al s e

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    19/40

    otherwiseSee Also:

    equal s( Obj ect )

    compareTo

    publ i c i nt compareTo( St r i ng anotherSt r i ng)

    Compares two strings lexicographically. The comparison is based on the Unicode value of eachcharacter in the strings. The character sequence represented by this St r i ngobject is comparedlexicographically to the character sequence represented by the argument string. The result is anegative integer if this St r i ngobject lexicographically precedes the argument string. The result is a

    positive integer if this St r i ngobject lexicographically follows the argument string. The result iszero if the strings are equal; compar eToreturns 0exactly when the equal s( Obj ect ) method wouldreturn true.

    This is the definition of lexicographic ordering. If two strings are different, then either they havedifferent characters at some index that is a valid index for both strings, or their lengths are different,or both. If they have different characters at one or more index positions, let kbe the smallest suchindex; then the string whose character at position khas the smaller value, as determined by usingthe < operator, lexicographically precedes the other string. In this case, compar eToreturns thedifference of the two character values at position kin the two string -- that is, the value:

    t hi s. char At ( k) - anot her St r i ng. char At ( k)

    If there is no index position at which they differ, then the shorter string lexicographically precedes

    the longer string. In this case, compar eToreturns the difference of the lengths of the strings -- thatis, the value:

    t hi s. l engt h( ) - anot her St r i ng. l engt h( )

    Specified by:

    compar eToin interface Comparabl eParameters:

    anot her St r i ng- the St r i ngto be compared.Returns:

    the value 0if the argument string is equal to this string; a value less than 0if this string islexicographically less than the string argument; and a value greater than 0if this string islexicographically greater than the string argument.

    compareToIgnoreCase

    publ i c i nt compareToIgnoreCase( St r i ng s t r )

    Compares two strings lexicographically, ignoring case differences. This method returns an integerwhose sign is that of calling compar eTowith normalized versions of the strings where casedifferences have been eliminated by callingChar act er . t oLower Case(Char act er . t oUpper Case(charact er ) ) on each character.

    Note that this method does nottake locale into account, and will result in an unsatisfactory ordering

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    20/40

    for certain locales. The java.text package provides collatorsto allow locale-sensitive ordering.

    Parameters:

    st r - the St r i ngto be compared.Returns:

    a negative integer, zero, or a positive integer as the specified String is greater than, equal to,or less than this String, ignoring case considerations.

    Since:1.2

    See Also:

    Col l at or . compar e( St r i ng, St r i ng)

    regionMatches

    publ i c bool ean regionMatches( i nt t of f set , St r i ng other, i nt oof f set ,

    i nt l en)

    Tests if two string regions are equal.

    A substring of this St r i ngobject is compared to a substring of the argument other. The result istrue if these substrings represent identical character sequences. The substring of this St r i ngobjectto be compared begins at index toffset and has length l en. The substring of other to be compared

    begins at index oof f set and has length l en. The result is f al seif and only if at least one of thefollowing is true:

    toffset is negative.oof f set is negative.t of f set +l enis greater than the length of this Str i ngobject.oof f set +l enis greater than the length of the other argument.There is some nonnegative integer kless than l ensuch that:thi s . charAt( t of f set +k) ! = ot her . charAt ( oof f set +k)

    Parameters:

    toffset - the starting offset of the subregion in this string.other - the string argument.oof f set - the starting offset of the subregion in the string argument.l en- the number of characters to compare.

    Returns:

    trueif the specified subregion of this string exactly matches the specified subregion of thestring argument; f al seotherwise.

    regionMatches

    publ i c bool ean regionMatches( bool ean i gnor eCase, i nt tof f set ,

    St r i ng other, i nt oof f set , i nt l en)

    Tests if two string regions are equal.

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    21/40

    A substring of this St r i ngobject is compared to a substring of the argument other . The result istrueif these substrings represent character sequences that are the same, ignoring case if and only ifi gnoreCaseis true. The substring of this St r i ngobject to be compared begins at index t of f setand has length l en. The substring of ot her to be compared begins at index oof f set and has lengthl en. The result is f al seif and only if at least one of the following is true:

    toffset is negative.

    oof f set is negative.t of f set +l enis greater than the length of this Str i ngobject.oof f set +l enis greater than the length of the other argument.i gnoreCaseis f al seand there is some nonnegative integer kless than l ensuch that:

    t hi s. char At ( t of f set +k) ! = ot her . char At ( oof f set +k)

    i gnoreCaseis trueand there is some nonnegative integer kless than l ensuch that:

    Char act er . t oLower Case( t hi s. char At ( t of f set +k)) ! = Charact er. t oLowerCase( ot her . char At ( oof f set+k) )

    and:

    Char act er . t oUpper Case( t hi s. char At ( t of f set +k)) ! = Char act er . t oUpper Case( ot her . char At ( oof f set+k) )

    Parameters:

    i gnoreCase- if true, ignore case when comparing characters.toffset - the starting offset of the subregion in this string.

    other - the string argument.oof f set - the starting offset of the subregion in the string argument.l en- the number of characters to compare.

    Returns:

    trueif the specified subregion of this string matches the specified subregion of the stringargument; f al seotherwise. Whether the matching is exact or case insensitive depends on thei gnoreCaseargument.

    startsWith

    publ i c bool ean startsWith( St r i ng pref i x, i nt tof f set )

    Tests if the substring of this string beginning at the specified index starts with the specified prefix.

    Parameters:

    pref i x- the prefix.toffset - where to begin looking in this string.

    Returns:

    trueif the character sequence represented by the argument is a prefix of the substring of this

    object starting at index t of f set ; f al seotherwise. The result is f al seif t of f set is negativeor greater than the length of this St r i ngobject; otherwise the result is the same as the resultof the expression

    thi s . substr i ng( t of f set ) . start sWi th( pr ef i x)

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    22/40

    startsWith

    publ i c bool ean startsWith( St r i ng pref i x)

    Tests if this string starts with the specified prefix.

    Parameters:

    pref i x- the prefix.Returns:

    trueif the character sequence represented by the argument is a prefix of the charactersequence represented by this string; f al s eotherwise. Note also that truewill be returned ifthe argument is an empty string or is equal to this St r i ngobject as determined by theequal s( Obj ect ) method.

    Since:

    1. 0

    endsWith

    publ i c bool ean endsWith( St r i ng suf f i x)

    Tests if this string ends with the specified suffix.

    Parameters:

    suf f i x- the suffix.Returns:

    trueif the character sequence represented by the argument is a suffix of the charactersequence represented by this object; f al seotherwise. Note that the result will be trueif theargument is the empty string or is equal to this St r i ngobject as determined by theequal s( Obj ect ) method.

    hashCode

    publ i c i nt hashCode( )

    Returns a hash code for this string. The hash code for a St r i ngobject is computed as

    s[0] *31 ( n- 1) + s[ 1] *31 ( n- 2) + . . . + s[n-1]

    using i nt arithmetic, where s [ i ] is the ith character of the string, nis the length of the string, and ^indicates exponentiation. (The hash value of the empty string is zero.)

    Overrides:

    hashCodein class Obj ectReturns:

    a hash code value for this object.See Also:

    Obj ect . equal s( j ava. l ang. Obj ect ) , Hasht abl e

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    23/40

    indexOf

    publ i c i nt indexOf( i nt ch)

    Returns the index within this string of the first occurrence of the specified character. If a characterwith value choccurs in the character sequence represented by this St r i ngobject, then the index (inUnicode code units) of the first such occurrence is returned. For values of chin the range from 0 to

    0xFFFF (inclusive), this is the smallest value ksuch that: t hi s. char At ( k) == ch

    is true. For other values of ch, it is the smallest value ksuch that:

    t hi s. codePoi nt At ( k) == ch

    is true. In either case, if no such character occurs in this string, then - 1is returned.

    Parameters:ch- a character (Unicode code point).

    Returns:

    the index of the first occurrence of the character in the character sequence represented by thisobject, or - 1if the character does not occur.

    indexOf

    publ i c i nt indexOf( i nt ch, i nt f r omI ndex)

    Returns the index within this string of the first occurrence of the specified character, starting thesearch at the specified index.

    If a character with value choccurs in the character sequence represented by this Str i ngobject at anindex no smaller than f r omI ndex, then the index of the first such occurrence is returned. For valuesof chin the range from 0 to 0xFFFF (inclusive), this is the smallest value ksuch that:

    ( t hi s. charAt ( k) == ch) && ( k >= f r omI ndex)

    is true. For other values of ch, it is the smallest value ksuch that:

    ( t hi s. codePoi nt At ( k) == ch) && ( k >= f r omI ndex)

    is true. In either case, if no such character occurs in this string at or after position f r omI ndex, then- 1is returned.

    There is no restriction on the value of f r omI ndex. If it is negative, it has the same effect as if it werezero: this entire string may be searched. If it is greater than the length of this string, it has the sameeffect as if it were equal to the length of this string: - 1is returned.

    All indices are specified in char values (Unicode code units).

    Parameters:

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    24/40

    ch- a character (Unicode code point).f r omI ndex- the index to start the search from.

    Returns:

    the index of the first occurrence of the character in the character sequence represented by thisobject that is greater than or equal to f r omI ndex, or - 1if the character does not occur.

    lastIndexOf

    publ i c i nt lastIndexOf( i nt ch)

    Returns the index within this string of the last occurrence of the specified character. For values ofchin the range from 0 to 0xFFFF (inclusive), the index (in Unicode code units) returned is thelargest value ksuch that:

    t hi s. char At ( k) == ch

    is true. For other values of ch, it is the largest value ksuch that: t hi s. codePoi nt At ( k) == ch

    is true. In either case, if no such character occurs in this string, then - 1is returned. The St r i ngissearched backwards starting at the last character.

    Parameters:

    ch- a character (Unicode code point).Returns:

    the index of the last occurrence of the character in the character sequence represented by thisobject, or - 1if the character does not occur.

    lastIndexOf

    publ i c i nt lastIndexOf( i nt ch, i nt f r omI ndex)

    Returns the index within this string of the last occurrence of the specified character, searchingbackward starting at the specified index. For values of chin the range from 0 to 0xFFFF

    (inclusive), the index returned is the largest value ksuch that:

    ( t hi s. charAt ( k) == ch) && ( k

  • 8/11/2019 String (Java Platform SE 6)

    25/40

    ch- a character (Unicode code point).f r omI ndex- the index to start the search from. There is no restriction on the value off r omI ndex. If it is greater than or equal to the length of this string, it has the same effect as ifit were equal to one less than the length of this string: this entire string may be searched. If itis negative, it has the same effect as if it were -1: -1 is returned.

    Returns:

    the index of the last occurrence of the character in the character sequence represented by this

    object that is less than or equal to f r omI ndex, or - 1if the character does not occur before thatpoint.

    indexOf

    publ i c i nt indexOf( St r i ng s t r )

    Returns the index within this string of the first occurrence of the specified substring. The integerreturned is the smallest value ksuch that:

    t hi s . s tar t sWi t h(s t r , k)

    is true.

    Parameters:

    st r - any string.Returns:

    if the string argument occurs as a substring within this object, then the index of the firstcharacter of the first such substring is returned; if it does not occur as a substring, - 1is

    returned.

    indexOf

    publ i c i nt indexOf( St r i ng s t r , i nt f r omI ndex)

    Returns the index within this string of the first occurrence of the specified substring, starting at thespecified index. The integer returned is the smallest value kfor which:

    k >= Mat h. mi n( f r omI ndex, t hi s. l engt h( ) ) && t hi s. st ar t sWi t h( st r , k)

    If no such value of kexists, then -1 is returned.

    Parameters:

    st r - the substring for which to search.f r omI ndex- the index from which to start the search.

    Returns:

    the index within this string of the first occurrence of the specified substring, starting at thespecified index.

    lastIndexOf

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    26/40

    publ i c i nt lastIndexOf( St r i ng s t r )

    Returns the index within this string of the rightmost occurrence of the specified substring. Therightmost empty string "" is considered to occur at the index value t hi s . l engt h( ) . The returnedindex is the largest value ksuch that

    t hi s . star tsWi t h( str , k)

    is true.

    Parameters:

    st r - the substring to search for.Returns:

    if the string argument occurs one or more times as a substring within this object, then theindex of the first character of the last such substring is returned. If it does not occur as asubstring, - 1is returned.

    lastIndexOf

    publ i c i nt lastIndexOf( St r i ng s t r , i nt f r omI ndex)

    Returns the index within this string of the last occurrence of the specified substring, searchingbackward starting at the specified index. The integer returned is the largest value ksuch that:

    k

  • 8/11/2019 String (Java Platform SE 6)

    27/40

    the specified substring.Throws:

    I ndexOutOf BoundsExcept i on- if begi nI ndexis negative or larger than the length of thisSt r i ngobject.

    substring

    publ i c Str i ng substring( i nt begi nI ndex, i nt endI ndex)

    Returns a new string that is a substring of this string. The substring begins at the specifiedbegi nI ndexand extends to the character at index endI ndex - 1. Thus the length of the substring isendI ndex- begi nI ndex.

    Examples:

    "hambur ger" . subst r i ng( 4, 8) r etur ns "urge" "smi l es". subst r i ng( 1, 5) r et ur ns "mi l e"

    Parameters:

    begi nI ndex- the beginning index, inclusive.endI ndex- the ending index, exclusive.

    Returns:

    the specified substring.Throws:

    I ndexOutOf BoundsExcept i on- if the begi nI ndexis negative, or endI ndexis larger than thelength of this St r i ngobject, or begi nI ndexis larger than endI ndex.

    subSequence

    publ i c CharSequence subSequence( i nt begi nI ndex, i nt endI ndex)

    Returns a new character sequence that is a subsequence of this sequence.

    An invocation of this method of the form

    st r . subSequence(begi n, end)

    behaves in exactly the same way as the invocation

    st r . subst r i ng( begi n, end)

    This method is defined so that the St r i ngclass can implement the CharSequenceinterface.

    Specified by:

    subSequencein interface CharSequenceParameters:

    begi nI ndex- the begin index, inclusive.endI ndex- the end index, exclusive.

    Returns:

    the specified subsequence.

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    28/40

    Throws:

    I ndexOutOf BoundsExcept i on- if begi nI ndexor endI ndexare negative, if endI ndexisgreater than l engt h( ) , or if begi nI ndexis greater than st ar t I ndex

    Since:

    1.4

    concat

    publ i c Str i ng concat( St r i ng s t r )

    Concatenates the specified string to the end of this string.

    If the length of the argument string is 0, then this Str i ngobject is returned. Otherwise, a newSt r i ngobject is created, representing a character sequence that is the concatenation of the charactersequence represented by this St r i ngobject and the character sequence represented by the argumentstring.

    Examples:

    "car es". concat ( "s" ) r et ur ns "car ess" " t o" . concat ( "get ") . concat ( "her ") r et ur ns "t oget her "

    Parameters:

    st r - the St r i ngthat is concatenated to the end of this St r i ng.Returns:

    a string that represents the concatenation of this object's characters followed by the stringargument's characters.

    replace

    publ i c Str i ng replace( char ol dChar , char newChar)

    Returns a new string resulting from replacing all occurrences of ol dChar in this string withnewChar .

    If the character ol dChar does not occur in the character sequence represented by this St r i ngobject,then a reference to this St r i ngobject is returned. Otherwise, a new St r i ngobject is created thatrepresents a character sequence identical to the character sequence represented by this Str i ngobject, except that every occurrence of ol dChar is replaced by an occurrence of newChar .

    Examples:

    " mesqui t e i n your cel l ar" . repl ace( ' e' , ' o' ) r et ur ns "mosqui t o i n your col l ar " " t he war of baronet s". repl ace( ' r ' , ' y' ) r et ur ns " t he way of bayonets" "sparr i ng wi t h a pur pl e por poi se". repl ace( ' p' , ' t ' )

    r et ur ns "st ar r i ng wi t h a t ur t l e t or t oi se" "J onL". r epl ace( ' q' , ' x' ) r et ur ns "J onL" ( no change)

    Parameters:

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    29/40

    ol dChar - the old character.newChar - the new character.

    Returns:

    a string derived from this string by replacing every occurrence of ol dChar with newChar .

    matches

    publ i c bool ean matches( St r i ng r egex)

    Tells whether or not this string matches the given regular expression.

    An invocation of this method of the form str. mat ches( regex) yields exactly the same result as theexpression

    Pattern. mat ches( regex, str)

    Parameters:

    r egex- the regular expression to which this string is to be matchedReturns:

    trueif, and only if, this string matches the given regular expressionThrows:

    Patt ernSynt axExcept i on- if the regular expression's syntax is invalidSince:

    1.4See Also:

    Pattern

    contains

    publ i c bool ean contains( CharSequence s)

    Returns true if and only if this string contains the specified sequence of char values.

    Parameters:

    s- the sequence to search forReturns:

    true if this string contains s, false otherwiseThrows:

    Nul l Poi nt er Except i on- if s is nul lSince:

    1.5

    replaceFirst

    publ i c Str i ng replaceFirst( St r i ng regex, St r i ng repl acement )

    Replaces the first substring of this string that matches the given regular expressionwith the givenreplacement.

    An invocation of this method of the form str. r epl aceFi rst ( regex, repl) yields exactly the same

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    30/40

    result as the expression

    Pattern. compi l e( regex) . mat cher ( str) . r epl aceFi r st ( repl)

    Note that backslashes (\ ) and dollar signs ($) in the replacement string may cause the results to bedifferent than if it were being treated as a literal replacement string; seeMat cher . r epl aceFi r st( j ava. l ang. St r i ng) . Use

    Matcher . quot eRepl acement ( j ava. l ang. St r i ng) to suppress the special meaning of thesecharacters, if desired.

    Parameters:

    r egex- the regular expression to which this string is to be matchedr epl acement - the string to be substituted for the first match

    Returns:

    The resulting St r i ngThrows:

    Patt ernSynt axExcept i on- if the regular expression's syntax is invalidSince:

    1.4See Also:

    Pattern

    replaceAll

    publ i c Str i ng replaceAll( St r i ng regex, St r i ng r epl acement )

    Replaces each substring of this string that matches the given regular expressionwith the givenreplacement.

    An invocation of this method of the form str. r epl aceAl l ( regex, repl) yields exactly the sameresult as the expression

    Pattern. compi l e( regex) . mat cher ( str) . r epl aceAl l ( repl)

    Note that backslashes (\ ) and dollar signs ($) in the replacement string may cause the results to bedifferent than if it were being treated as a literal replacement string; see Matcher . r epl aceAl l . UseMatcher . quot eRepl acement ( j ava. l ang. St r i ng) to suppress the special meaning of thesecharacters, if desired.

    Parameters:

    r egex- the regular expression to which this string is to be matchedr epl acement - the string to be substituted for each match

    Returns:

    The resulting St r i ngThrows:

    Patt ernSynt axExcept i on- if the regular expression's syntax is invalidSince:

    1.4See Also:

    Pattern

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    31/40

    replace

    publ i c Str i ng replace( CharSequence target, CharSequence repl acement )

    Replaces each substring of this string that matches the literal target sequence with the specifiedliteral replacement sequence. The replacement proceeds from the beginning of the string to the end,for example, replacing "aa" with "b" in the string "aaa" will result in "ba" rather than "ab".

    Parameters:

    target - The sequence of char values to be replacedr epl acement - The replacement sequence of char values

    Returns:

    The resulting stringThrows:

    Nul l Poi nt er Except i on- if t ar get or r epl acement is nul l .Since:

    1.5

    split

    publ i c Str i ng[ ] split( St r i ng regex, i nt l i mi t )

    Splits this string around matches of the given regular expression.

    The array returned by this method contains each substring of this string that is terminated byanother substring that matches the given expression or is terminated by the end of the string. Thesubstrings in the array are in the order in which they occur in this string. If the expression does notmatch any part of the input then the resulting array has just one element, namely this string.

    The l i mi t parameter controls the number of times the pattern is applied and therefore affects thelength of the resulting array. If the limit nis greater than zero then the pattern will be applied atmost n- 1 times, the array's length will be no greater than n, and the array's last entry will containall input beyond the last matched delimiter. If nis non-positive then the pattern will be applied asmany times as possible and the array can have any length. If nis zero then the pattern will beapplied as many times as possible, the array can have any length, and trailing empty strings will bediscarded.

    The string "boo: and: f oo" , for example, yields the following results with these parameters:

    RegexLimit Result

    : 2 { "boo", "and: f oo" }

    : 5 { "boo", "and" , "f oo" }

    : -2 { "boo", "and" , "f oo" }

    o 5 { "b" , " " , " : and: f " , " " , " " }

    o -2 { "b" , " " , " : and: f " , " " , " " }

    o 0 { "b" , "" , " : and: f " }

    An invocation of this method of the form str.spl i t ( regex, n) yields the same result as theexpression

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    32/40

    Pattern.compi l e( regex) .spl i t ( str, n)

    Parameters:

    r egex- the delimiting regular expressionl i mi t - the result threshold, as described above

    Returns:

    the array of strings computed by splitting this string around matches of the given regular

    expressionThrows:

    Patt ernSynt axExcept i on- if the regular expression's syntax is invalidSince:

    1.4See Also:

    Pattern

    split

    publ i c Str i ng[ ] split( St r i ng regex)

    Splits this string around matches of the given regular expression.

    This method works as if by invoking the two-argument spl i t method with the given expressionand a limit argument of zero. Trailing empty strings are therefore not included in the resulting array.

    The string "boo: and: f oo" , for example, yields the following results with these expressions:

    Regex Result

    : { "boo", "and", "f oo" }o { "b" , "" , " : and: f " }

    Parameters:

    r egex- the delimiting regular expressionReturns:

    the array of strings computed by splitting this string around matches of the given regularexpression

    Throws:

    Patt ernSynt axExcept i on- if the regular expression's syntax is invalid

    Since:1.4

    See Also:

    Pattern

    toLowerCase

    publ i c Str i ng toLowerCase( Local e l ocal e)

    Converts all of the characters in this St r i ngto lower case using the rules of the given Local e. Casemapping is based on the Unicode Standard version specified by the Char act er class. Since casemappings are not always 1:1 char mappings, the resulting St r i ngmay be a different length than theoriginal St r i ng.

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    33/40

    Examples of lowercase mappings are in the following table:

    Language Code of

    Locale

    Upper

    Case

    Lower

    Case Description

    tr (Turkish) \u0130 \u0069capital letter I with dot above -> small letteri

    tr (Turkish) \u0049 \u0131 capital letter I -> small letter dotless i

    (all)FrenchFries

    french fries lowercased all chars in String

    (all) lowercased all chars in String

    Parameters:

    l ocal e- use the case transformation rules for this localeReturns:

    the St r i ng, converted to lowercase.Since:

    1.1See Also:

    t oLowerCase( ) , t oUpper Case( ) , t oUpper Case(Local e)

    toLowerCase

    publ i c Str i ng toLowerCase( )

    Converts all of the characters in this St r i ngto lower case using the rules of the default locale. This

    is equivalent to calling t oLower Case( Local e. get Def aul t ( ) ) .

    Note:This method is locale sensitive, and may produce unexpected results if used for strings thatare intended to be interpreted locale independently. Examples are programming languageidentifiers, protocol keys, and HTML tags. For instance, "TI TLE" . t oLower Case() in a Turkishlocale returns " t? t l e" , where '?' is the LATIN SMALL LETTER DOTLESS I character. To obtaincorrect results for locale insensitive strings, use t oLowerCase( Local e. ENGLI SH) .

    Returns:

    the St r i ng, converted to lowercase.See Also:

    t oLowerCase(Local e)

    toUpperCase

    publ i c Str i ng toUpperCase( Local e l ocal e)

    Converts all of the characters in this St r i ngto upper case using the rules of the given Local e. Casemapping is based on the Unicode Standard version specified by the Char act er class. Since casemappings are not always 1:1 char mappings, the resulting St r i ngmay be a different length than the

    original St r i ng.

    Examples of locale-sensitive and 1:M case mappings are in the following table.

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    34/40

    Language Code of

    Locale Lower Case Upper Case Description

    tr (Turkish) \u0069 \u0130small letter i -> capital letter I withdot above

    tr (Turkish) \u0131 \u0049small letter dotless i -> capital letterI

    (all) \u00df \u0053 \u0053 small letter sharp s -> two letters:SS

    (all) Fahrvergngen FAHRVERGNGEN

    Parameters:

    l ocal e- use the case transformation rules for this localeReturns:

    the St r i ng, converted to uppercase.Since:

    1.1See Also:

    t oUpperCase( ) , t oLowerCase() , t oLowerCase(Local e)

    toUpperCase

    publ i c Str i ng toUpperCase( )

    Converts all of the characters in this St r i ngto upper case using the rules of the default locale. Thismethod is equivalent to t oUpperCase(Local e. get Def aul t ( ) ) .

    Note:This method is locale sensitive, and may produce unexpected results if used for strings thatare intended to be interpreted locale independently. Examples are programming languageidentifiers, protocol keys, and HTML tags. For instance, "t i t l e". t oUpperCase( ) in a Turkishlocale returns "T?TLE" , where '?' is the LATIN CAPITAL LETTER I WITH DOT ABOVEcharacter. To obtain correct results for locale insensitive strings, uset oUpper Case(Local e. ENGLI SH) .

    Returns:

    the St r i ng, converted to uppercase.

    See Also:t oUpper Case(Local e)

    trim

    publ i c Str i ng trim( )

    Returns a copy of the string, with leading and trailing whitespace omitted.

    If this St r i ngobject represents an empty character sequence, or the first and last characters of

    character sequence represented by this Str i ngobject both have codes greater than ' \ u0020' (thespace character), then a reference to this St r i ngobject is returned.

    Otherwise, if there is no character with a code greater than ' \ u0020' in the string, then a new

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    35/40

    St r i ngobject representing an empty string is created and returned.

    Otherwise, let kbe the index of the first character in the string whose code is greater than ' \ u0020' ,and let mbe the index of the last character in the string whose code is greater than ' \ u0020' . A newSt r i ngobject is created, representing the substring of this string that begins with the character atindex kand ends with the character at index m-that is, the result of t hi s. substr i ng( k, m+1) .

    This method may be used to trim whitespace (as defined above) from the beginning and end of astring.

    Returns:

    A copy of this string with leading and trailing white space removed, or this string if it has noleading or trailing white space.

    toString

    publ i c Str i ng toString( )

    This object (which is already a string!) is itself returned.

    Specified by:

    t oSt r i ngin interface CharSequenceOverrides:

    t oSt r i ngin class Obj ectReturns:

    the string itself.

    toCharArray

    publ i c char[ ] toCharArray( )

    Converts this string to a new character array.

    Returns:

    a newly allocated character array whose length is the length of this string and whose contentsare initialized to contain the character sequence represented by this string.

    format

    publ i c stati c Str i ng format( St r i ng f ormat , Obj ect . . . args )

    Returns a formatted string using the specified format string and arguments.

    The locale always used is the one returned by Local e. get Def aul t ( ) .

    Parameters:

    f or mat - A format stringargs- Arguments referenced by the format specifiers in the format string. If there are morearguments than format specifiers, the extra arguments are ignored. The number of argumentsis variable and may be zero. The maximum number of arguments is limited by the maximum

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    36/40

    dimension of a Java array as defined by the Java Virtual Machine Specification. Thebehaviour on a nul l argument depends on the conversion.

    Returns:

    A formatted stringThrows:

    I l l egal For mat Except i on- If a format string contains an illegal syntax, a format specifierthat is incompatible with the given arguments, insufficient arguments given the format string,

    or other illegal conditions. For specification of all possible formatting errors, see the Detailssection of the formatter class specification.Nul l Poi nt er Except i on- If the f or mat is nul l

    Since:

    1.5See Also:

    Format t er

    format

    publ i c stati c Str i ng format( Local e l , St r i ng f ormat , Obj ect . . . args )

    Returns a formatted string using the specified locale, format string, and arguments.

    Parameters:

    l - The localeto apply during formatting. If l is nul l then no localization is applied.f or mat - A format stringargs- Arguments referenced by the format specifiers in the format string. If there are more

    arguments than format specifiers, the extra arguments are ignored. The number of argumentsis variable and may be zero. The maximum number of arguments is limited by the maximumdimension of a Java array as defined by the Java Virtual Machine Specification. The

    behaviour on a nul l argument depends on the conversion.Returns:

    A formatted stringThrows:

    I l l egal For mat Except i on- If a format string contains an illegal syntax, a format specifierthat is incompatible with the given arguments, insufficient arguments given the format string,or other illegal conditions. For specification of all possible formatting errors, see the Details

    section of the formatter class specificationNul l Poi nt er Except i on- If the f or mat is nul l

    Since:

    1.5See Also:

    Format t er

    valueOf

    publ i c stati c Str i ng valueOf( Obj ect obj )

    Returns the string representation of the Obj ect argument.

    Parameters:

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    37/40

    obj - an Obj ect .Returns:

    if the argument is nul l , then a string equal to "nul l " ; otherwise, the value ofobj . t oSt r i ng( ) is returned.

    See Also:

    Obj ect. t oSt r i ng( )

    valueOf

    publ i c stati c Str i ng valueOf( char [ ] dat a)

    Returns the string representation of the char array argument. The contents of the character array arecopied; subsequent modification of the character array does not affect the newly created string.

    Parameters:

    data- a char array.Returns:

    a newly allocated string representing the same sequence of characters contained in thecharacter array argument.

    valueOf

    publ i c stati c Str i ng valueOf( char [ ] dat a, i nt of f set , i nt count )

    Returns the string representation of a specific subarray of the char array argument.

    The of f set argument is the index of the first character of the subarray. The count argumentspecifies the length of the subarray. The contents of the subarray are copied; subsequentmodification of the character array does not affect the newly created string.

    Parameters:

    data- the character array.of f set - the initial offset into the value of the St r i ng.count - the length of the value of the St r i ng.

    Returns:

    a string representing the sequence of characters contained in the subarray of the characterarray argument.

    Throws:

    I ndexOutOf BoundsExcept i on- if of f set is negative, or count is negative, or of f set +countis larger than dat a. l engt h.

    copyValueOf

    publ i c stati c Str i ng copyValueOf( char [ ] dat a,

    i nt of f set , i nt count )

    Returns a String that represents the character sequence in the array specified.

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    38/40

    Parameters:

    data- the character array.of f set - initial offset of the subarray.count - length of the subarray.

    Returns:

    a St r i ngthat contains the characters of the specified subarray of the character array.

    copyValueOf

    publ i c stati c Str i ng copyValueOf( char [ ] dat a)

    Returns a String that represents the character sequence in the array specified.

    Parameters:

    data- the character array.Returns:

    a St r i ngthat contains the characters of the character array.

    valueOf

    publ i c stati c Str i ng valueOf( bool ean b)

    Returns the string representation of the bool eanargument.

    Parameters:

    b- a bool ean.

    Returns:if the argument is true, a string equal to "t rue" is returned; otherwise, a string equal to" f al se" is returned.

    valueOf

    publ i c stati c Str i ng valueOf( char c)

    Returns the string representation of the char argument.

    Parameters:

    c- a char .Returns:

    a string of length 1containing as its single character the argument c.

    valueOf

    publ i c stati c Str i ng valueOf( i nt i )

    Returns the string representation of the i nt argument.

    The representation is exactly the one returned by the I nt eger . t oSt r i ngmethod of one argument.

    Parameters:

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    39/40

    i - an i nt .Returns:

    a string representation of the i nt argument.See Also:

    I nteger . t oSt r i ng( i nt , i nt )

    valueOf

    publ i c stati c Str i ng valueOf( l ong l )

    Returns the string representation of the l ongargument.

    The representation is exactly the one returned by the Long. t oSt r i ngmethod of one argument.

    Parameters:

    l - a l ong.Returns:

    a string representation of the l ongargument.See Also:

    Long. t oSt r i ng( l ong)

    valueOf

    publ i c stati c Str i ng valueOf( f l oat f )

    Returns the string representation of the f l oat argument.

    The representation is exactly the one returned by the Fl oat . t oSt r i ngmethod of one argument.

    Parameters:

    f- a f l oat .Returns:

    a string representation of the f l oat argument.See Also:

    Fl oat . t oSt r i ng( f l oat )

    valueOf

    publ i c stati c Str i ng valueOf( doubl e d)

    Returns the string representation of the doubl eargument.

    The representation is exactly the one returned by the Doubl e. t oSt r i ngmethod of one argument.

    Parameters:

    d- a doubl e.

    Returns:a string representation of the doubl eargument.See Also:

    Doubl e. t oSt r i ng( doubl e)

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html

    f 40 08/10/2013 13:34

  • 8/11/2019 String (Java Platform SE 6)

    40/40

    intern

    publ i c Str i ng intern( )

    Returns a canonical representation for the string object.

    A pool of strings, initially empty, is maintained privately by the class Str i ng.

    When the intern method is invoked, if the pool already contains a string equal to this St r i ngobjectas determined by the equal s( Obj ect ) method, then the string from the pool is returned. Otherwise,this St r i ngobject is added to the pool and a reference to this St r i ngobject is returned.

    It follows that for any two strings sand t , s . i ntern( ) == t . i ntern( ) is trueif and only ifs. equal s(t ) is true.

    All literal strings and string-valued constant expressions are interned. String literals are defined in3.10.5 of the Java Language Specification

    Returns:a string that has the same contents as this string, but is guaranteed to be from a pool of uniquestrings.

    Overview Package Class Use Tree Deprecated Index Help Java PlatformStandard Ed. 6PREV CLASS NEXT CLASS FRAMES NO FRAMES All Cl asses

    SUMMARY: NESTED | FIELD| CONSTR| METHOD DETAIL: FIELD| CONSTR| METHOD

    Submit a bug or featureFor further API reference and developer documentation, see Java SE Developer Documentation. That documentation containsmore detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code

    examples.

    Copyright 1993, 2011, Oracle and/or its affiliates. All rights reserved.

    ng (Java Platform SE 6) http://docs.oracle.com/javase/6/docs/api/java/lang/String.html