All methods of the Java.lang.String class

Source: Internet
Author: User
Tags locale ranges string format

All methods of the Java.lang.String class
Method Summary
char charAt (int index)
Returns the char value at the specified index.
int codepointat (int index)
Returns the character (Unicode code point) at the specified index.
int codepointbefore (int index)
Returns the character (Unicode code point) before the specified index.
int codepointcount (int beginindex, int endIndex)
Returns the number of Unicode code points in the specified text range for this String.
int CompareTo (String anotherstring)
Compares two strings in a dictionary order.
int comparetoignorecase (String str)
Compares two strings in a dictionary order, regardless of case.
String concat (String str)
The specified string is linked to the end of this string.
Boolean contains (Charsequence s)
Returns True when and only if this string contains a specified sequence of char values.
Boolean contentequals (Charsequence CS)
Returns true if and only if this String represents the same char value as the specified sequence.
Boolean contentequals (StringBuffer SB)
Returns true if and only if this String represents the same sequence of characters as the specified stringbuffer.
Static String copyvalueof (char[] data)
Returns a string representing the sequence of characters in the specified array.
Static String copyvalueof (char[] data, int offset, int count)
Returns a string representing the sequence of characters in the specified array.
Boolean endsWith (String suffix)
Tests whether this string ends with the specified suffix.
Boolean equals (Object anobject)
Compares this string with the specified object.
Boolean equalsignorecase (String anotherstring)
Compares this string to another string, regardless of case.
static string format (Locale L, String format, Object ... args)
Returns a formatted string using the specified locale, format string, and parameters.
static string format (string format, Object ... args)
Returns a formatted string using the specified format string and parameters.
Byte[] GetBytes ()
Use the default character set of the platform to decode this String into a sequence of bytes and store the result in a new byte array.
void getBytes (int srcbegin, int srcend, byte[] DST, int dstbegin)
is obsolete. The method cannot convert the characters correctly to bytes. Starting with JDK 1.1, the preferred method to complete the conversion is through the getBytes () construction method, which uses the platform's default character set.
Byte[] GetBytes (String charsetname)
Decodes this String into a sequence of bytes using the specified character set and stores the result in a new byte array.
void GetChars (int srcbegin, int srcend, char[] DST, int dstbegin)
Copies the character from this string to the target character array.
int Hashcode ()
Returns the hash code for this string.
int indexOf (int ch)
Returns the index at which the specified character first appears in this string.
int indexOf (int ch, int fromIndex)
Starts the search from the specified index, returning the index at the first occurrence of the specified character in this string.
int indexOf (String str)
Returns the index of the first occurrence of the specified substring in this string.
int indexOf (String str, int fromIndex)
Returns the index in this string for the first occurrence of the specified substring, starting at the specified index.
String Intern ()
Returns a normalized representation of a string object.
int lastIndexOf (int ch)
Returns the index of the last occurrence of the specified character in this string.
int lastIndexOf (int ch, int fromIndex)
Returns the index of the last occurrence of the specified character in this string, starting at the specified index.
int lastIndexOf (String str)
Returns the index of the specified substring that appears rightmost in this string.
int lastIndexOf (String str, int fromIndex)
Searches backward at the specified index, returning the index of the specified substring that occurred last in this string.
int Length ()
Returns the length of this string.
Boolean matches (String regex)
Notifies whether this string matches a given regular expression.
int offsetbycodepoints (int index, int codepointoffset)
Returns the index of the Codepointoffset code point offset from the given index in this String.
Boolean Regionmatches (boolean ignoreCase, int toffset, String other, int ooffset, int len)
Tests whether two string regions are equal.
Boolean regionmatches (int toffset, String other, int ooffset, int len)
Tests whether two string regions are equal.
String replace (char OldChar, char Newchar)
Returns a new string that is generated by replacing all OldChar that appear in this string with Newchar.
String replace (charsequence target, charsequence replacement)
Replaces each substring of this string with the literal value target sequence, using the specified literal substitution sequence.
String ReplaceAll (string regex, string replacement)
Replaces this string with the given replacement string to match each substring of the given regular expression.
String Replacefirst (string regex, string replacement)
Replaces this string with the given replacement string to match the first substring of a given regular expression.
String[] Split (String regex)
Splits this string according to the match of the given regular expression.
String[] Split (String regex, int limit)
Splits the string by matching the given regular expression.
Boolean startsWith (String prefix)
Tests whether this string starts with the specified prefix.
Boolean startsWith (String prefix, int toffset)
Tests whether this string starts with the specified prefix, starting at the specified index.
Charsequence subsequence (int beginindex, int endIndex)
Returns a new sequence of characters that is a subsequence of this sequence.
String substring (int beginindex)
Returns a new string that is a substring of this string.
String substring (int beginindex, int endIndex)
Returns a new string that is a substring of this string.
Char[] ToCharArray ()
Converts this string to a new character array.
String toLowerCase ()
Use the default locale rule to convert all characters in this String to lowercase.
String tolowercase (locale locale)
Converts all characters in this String to lowercase using the rules for a given Locale.
String toString ()
Returns the object itself (it is already a string!) )。
String toUpperCase ()
Converts all characters in this String to uppercase using the rules of the default locale.
String touppercase (locale locale)
Converts all characters in this String to uppercase using the given Locale rule.
String Trim ()
Returns a copy of the string, ignoring leading and trailing blanks.
Static String ValueOf (Boolean B)
Returns a string representation of a Boolean parameter.
Static String valueOf (char c)
Returns a string representation of the char parameter.
Static String valueOf (char[] data)
Returns a string representation of a char array parameter.
Static String valueOf (char[] data, int offset, int count)
Returns a string representation of a specific subarray of char array parameters.
Static String valueOf (double D)
Returns a string representation of the double argument.
Static String valueOf (float f)
Returns a string representation of the float parameter.
static String valueOf (int i)
Returns the string representation of the int parameter.
Static String valueOf (long L)
Returns the string representation of a long parameter.
Static String valueOf (Object obj)
Returns the string representation of the Object parameter.
Methods of inheriting from class Java.lang.Object
Clone, Finalize, GetClass, notify, Notifyall, wait, wait, wait

--------------------------------------------------------------------------------

CharAt
public char charAt (int index) returns the char value at the specified index. The index range is from 0 to length ()-1. The first char value of a sequence is at index 0, the second at index 1, and so on, which is similar to an array index.
Returns the surrogate value if the specified char value for the index is a surrogate.


Designated by:
CharAt in the interface charsequence
Parameters:
The index of the Index-char value.
Return:
This string specifies the char value at the index. The first char value is at index 0.
Thrown:
Indexoutofboundsexception-If the index parameter is negative or less than the length of this string.

--------------------------------------------------------------------------------

Codepointat
public int codepointat (int index) returns the character (Unicode code point) at the specified index. The index refers to a char value (Unicode code unit), which ranges from 0 to length ()-1.
If the char value specified by the given index belongs to a high surrogate range, subsequent indexes are less than the length of this String. Similarly, if the char value at the subsequent index falls under a low surrogate scope, the corresponding supplemental code point is returned for that surrogate. Otherwise, returns the char value at the given index.


Parameters:
Index of the Index-char value
Return:
The code point value of the character at the index
Thrown:
Indexoutofboundsexception-If the index parameter is negative or less than the length of this string.
Start from the following versions:
1.5

--------------------------------------------------------------------------------

Codepointbefore
public int codepointbefore (int index) returns the character (Unicode code point) before the specified index. The index refers to the char value (Unicode code unit), which ranges from 1 to length.
If the char value at (index-1) belongs to a low surrogate range, then (index-2) is non-negative, and if the Char value at (index-2) is of the high and low range, the supplementary code point value of the surrogate pair is returned. If the char value at Index-1 is an unpaired low (high) surrogate, the surrogate value is returned.


Parameters:
Index-indexes that should be returned after the code point
Return:
The Unicode code point before the given index.
Thrown:
Indexoutofboundsexception-If the index parameter is less than 1 or greater than the length of this string.
Start from the following versions:
1.5

--------------------------------------------------------------------------------

Codepointcount
public int Codepointcount (int beginindex,
int endIndex) Returns the number of Unicode code points in the specified text range for this String. The text range starts at the specified Beginindex and continues to the char at index ENDINDEX-1. Therefore, the length of the text range (in char) is endindex-beginindex. The non-paired surrogate in the text range is counted as a code point.

Parameters:
Beginindex-The index of the first char of the text range.
EndIndex-the index after the last char of the text range.
Return:
Specify the number of Unicode code points in a text range
Thrown:
Indexoutofboundsexception-If Beginindex is negative, or endIndex is greater than the length of this String, or beginindex is greater than endIndex.
Start from the following versions:
1.5

--------------------------------------------------------------------------------

Offsetbycodepoints
public int offsetbycodepoints (int index,
int codepointoffset) Returns the index in this String that offsets Codepointoffset code points from a given index. The non-paired surrogate items given by index and Codepointoffset are counted as one code point within the text range.

Parameters:
Index-The indexes to offset
Codepointoffset-Offset in the code point
Return:
The index of the String
Thrown:
Indexoutofboundsexception-If index is negative or greater than the length of this String, or if Codepointoffset is positive and the substring starts with index with fewer code points than Codepointoffset , or if Codepointoffset is negative, and the substring before index has fewer code points than the absolute value of Codepointoffset.
Start from the following versions:
1.5

--------------------------------------------------------------------------------

GetChars
public void GetChars (int srcbegin,
int Srcend,
Char[] DST,
int Dstbegin) Copies the character from this string to the target character array.
The first character to copy is at index Srcbegin, and the last character to be copied is at index srcEnd-1 (so the total number of characters to be copied is Srcend-srcbegin). The characters to be copied to the DST sub-array start at index Dstbegin and end at index:


Dstbegin + (Srcend-srcbegin)-1

Parameters:
Srcbegin-The index of the first character in a string to copy.
Srcend-the index after the last character in the string to copy.
DST-Destination Array.
Dstbegin-The starting offset in the target array.
Thrown:
Indexoutofboundsexception-If any one of the following is true:
Srcbegin is negative.
Srcbegin Greater than srcend
Srcend is greater than the length of this string
Dstbegin is negative
dstbegin+ (Srcend-srcbegin) greater than dst.length

--------------------------------------------------------------------------------

GetBytes
@Deprecated
public void getBytes (int srcbegin,
int Srcend,
Byte[] DST,
int dstbegin) is obsolete. The method cannot convert the characters correctly to bytes. Starting with JDK 1.1, the preferred method to complete the conversion is through the getBytes () construction method, which uses the platform's default character set.

Copies the character from this string to the destination byte array. Each byte receives a 8 low of the corresponding character. The highs of each character are not copied, and they do not participate in any way of conversion.
The first character to copy is at index Srcbegin, and the last character to be copied is at index srcEnd-1. The total number of characters to copy is Srcend-srcbegin. Copy the character that you want to convert to bytes into a sub-array of DST, starting at index Dstbegin and ending at index:


Dstbegin + (Srcend-srcbegin)-1

Parameters:
Srcbegin-The index of the first character in a string to copy.
Srcend-the index after the last character in the string to copy.
DST-Destination Array.
Dstbegin-The starting offset in the target array.
Thrown:
Indexoutofboundsexception-If any one of the following is true:
Srcbegin is negative
Srcbegin Greater than srcend
Srcend is greater than the length of this String
Dstbegin is negative
dstbegin+ (Srcend-srcbegin) greater than dst.length

--------------------------------------------------------------------------------

GetBytes
Public byte[] GetBytes (String charsetname)
Throws Java.io.UnsupportedEncodingException decodes this String into a sequence of bytes using the specified character set and stores the result in a new byte array.
When this string cannot be decoded in a given character set, the method has no specified behavior. You should use the Charsetencoder class when you need to further control the decoding process.


Parameters:
CharsetName-supported CharSet names
Return:
Result byte array
Thrown:
Java.io.UnsupportedEncodingException-if the specified character set is not supported
Start from the following versions:
JDK1.1

--------------------------------------------------------------------------------

GetBytes
Public byte[] GetBytes () decodes this String into a sequence of bytes using the default character set of the platform and stores the result in a new byte array.
When this string cannot be decoded in the default character set, the method has no specified behavior. You should use the Charsetencoder class when you need to further control the decoding process.


Return:
Result byte array
Start from the following versions:
JDK1.1

--------------------------------------------------------------------------------

equals
The public boolean equals (object AnObject) compares this string with the specified object. The result is true when and only if the parameter is not NULL, and is a String object that represents the same sequence of characters as this object.

Covered:
equals in the class Object
Parameters:
AnObject-the object to compare with this String.
Return:
Returns true if String is equal, otherwise false.
See also:
CompareTo (java.lang.String), Equalsignorecase (java.lang.String)

--------------------------------------------------------------------------------

Contentequals
public boolean contentequals (StringBuffer sb) returns True if and only if this String represents the same sequence of characters as the specified stringbuffer.

Parameters:
SB-The StringBuffer to compare.
Return:
Returns true if and only if this String represents the same sequence of characters as the specified StringBuffer, otherwise returns false.
Thrown:
NullPointerException-if SB is null
Start from the following versions:
1.4

--------------------------------------------------------------------------------

Contentequals
public boolean contentequals (Charsequence cs) returns True when and only if this String represents the same char value as the specified sequence.

Parameters:
CS-the sequence to compare.
Return:
Returns true if and only if this String represents a sequence of the same char value as the specified sequence;
Thrown:
NullPointerException-IF CS is null
Start from the following versions:
1.5

--------------------------------------------------------------------------------

Equalsignorecase
The public boolean equalsignorecase (string anotherstring) compares this string to another string, regardless of case. If the length of the two strings is equal, and the corresponding characters in the two strings are Fudusian (ignoring case), the two strings are considered equal.
In cases where case is ignored, if at least one of the following is true, C1 and C2 are considered to be the same characters.

The two characters are the same (compared using the = = operator).
Applying method Character.touppercase (char) to each character produces the same result.
Applying method Character.tolowercase (char) to each character produces the same result.

Parameters:
Anotherstring-string that is compared to this string.
Return:
Returns true if the argument is not NULL, and if the two strings are equal when the case is ignored, false is returned.
See also:
Equals (Object), Character.tolowercase (char), character.touppercase (char)

--------------------------------------------------------------------------------

CompareTo
The public int compareTo (string anotherstring) compares two strings in a dictionary order. The comparison is based on the Unicode values of the individual characters in the string. Compares the sequence of characters represented by this string object with the sequence of characters represented by the argument string. If this string object is in the dictionary order before the parameter string, the comparison result is a negative integer. If this string object is in the dictionary order after the parameter string, the comparison result is a positive integer. If the two strings are equal, the result is 0;compareto returns 0 only if the method equals (Object) returns True.
This is the definition of the dictionary sort. If the two strings are different, either they have different characters at an index, both are valid indexes, either they are of different lengths, or both of these cases. If they have different characters at one or more index positions, assuming that k is the minimum value for such an index, then the string determined by the < operator has a smaller value on position k, and its dictionary order precedes the other string. In this case, COMPARETO returns the two different char values of the two strings at position K, that is, the value:

This.charat (k)-anotherstring.charat (k)
If they do not have a different index position, the shorter string is preceded by a longer string in the dictionary order. In this case, COMPARETO returns the difference between the two string lengths, which is the value:
This.length ()-anotherstring.length ()

Designated by:
CompareTo in the interface comparable<string>
Parameters:
Anotherstring-The String to compare.
Return:
If the argument string equals this string, a value of 0 is returned, and if this string is less than the string parameter in dictionary order, a value less than 0 is returned, and a value greater than 0 is returned if the string is larger than the string parameter in dictionary order.

--------------------------------------------------------------------------------

Comparetoignorecase
public int comparetoignorecase (string str) does not consider case and compares two strings in a dictionary order. This method returns an integer whose sign is the sign of the call to CompareTo, which is called with a normalized version of the string, with a case difference that has been made by calling Character.tolowercase on each character (Character.touppercase ( character)) is eliminated.
Note that this method does not take into account the locale, and therefore may produce undesirable sorting in some specific locales. The Java.text package provides collators to complete locale-sensitive sorting.


Parameters:
STR-the String to compare.
Return:
A negative integer, 0, or positive integer that depends on whether the specified string is greater than, equal to, or less than the string, regardless of case.
Start from the following versions:
1.2
See also:
Collator.compare (String, String)

--------------------------------------------------------------------------------

Regionmatches
public boolean regionmatches (int toffset,
String Other,
int Ooffset,
int len) to test for equality of two string regions.
A substring of the string object is compared to a substring of the parameter other. If these two substrings represent the same sequence of characters, the result is true. The substring of the string object to compare starts at index Toffset and is Len Long. The substring of the other to be compared starts at index Ooffset and is len in length. The result is false when and only if at least one of the following is true:

Toffset is negative.
Ooffset is negative.
Toffset+len is greater than the length of this String object.
Ooffset+len is greater than the length of another parameter.
There is a nonnegative integer k that is less than Len, which satisfies: This.charat (toffset+k)! = Other.charat (ooffset+k)

Parameters:
Toffset-The starting offset of the neutron region of the string.
Other-string parameter.
Ooffset-The starting offset of the neutron region of the string parameter.
Len-the number of characters to compare.
Return:
Returns true if the specified sub-region of the string exactly matches the specified sub-region of the string parameter;

--------------------------------------------------------------------------------

Regionmatches
Public Boolean regionmatches (Boolean ignoreCase,
int Toffset,
String Other,
int Ooffset,
int len) to test for equality of two string regions.
Compares the substring of this string object with the substring of the parameter other. If these two substrings represent the same sequence of characters, the result is true, and the case is ignored if and only if IgnoreCase is true. The substring of the string object to compare starts at index Toffset and is Len Long. The substring of the other to be compared starts at index Ooffset and is len in length. The result is false when and only if at least one of the following is true:

Toffset is negative.
Ooffset is negative.
Toffset+len is greater than the length of this String object.
Ooffset+len is greater than the length of another parameter.
IgnoreCase is false and there is a non-negative integer k that is less than Len, i.e.:
This.charat (toffset+k)! = Other.charat (ooffset+k)
IgnoreCase is true, and there is a non-negative integer k that is less than Len, which is:
Character.tolowercase (This.charat (toffset+k))! =
Character.tolowercase (Other.charat (ooffset+k))
And:
Character.touppercase (This.charat (toffset+k))! =
Character.touppercase (Other.charat (ooffset+k))

Parameters:
IgnoreCase-If true, the case is ignored when the character is compared.
Toffset-The starting offset of the sub-range in the string.
Other-string parameter.
Toffset-The starting offset of the sub-range in the string parameter.
Len-the number of characters to compare.
Return:
Returns true if the specified sub-region of this string matches the specified sub-region of the string parameter; Whether the exact match or case sensitivity depends on the ignoreCase parameter.

All methods of the Java.lang.String class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.