Java-string,stringbuilder,stringbuffe type Use

Source: Internet
Author: User

1,string class

1.1 Definition :

    Stringclass represents a string. All string literals (such as) in a Java program "abc" are implemented as instances of this class.

strings are constants , and their values cannot be changed after they are created. A string buffer supports variable strings. Because String

Objects are immutable, so they can be shared. For example:

String s1= "ABC";

String s2= "ABC";//Because ABC is constant, so Java no longer opens up new space, so

S1 and S2 point to the same "ABC", that is, S1==s2 is true. and string S3=new string ("abc");

The S3 points to an object, which points to "ABC".

1.2 Construction methods

String (byte[] bytes);//The character that corresponds to the value of bytes.

String (char[] value);//consists of a char character array

string (char[],offset,count): Converts a portion of a character array into a string.

String (string original);

String (StringBuilder builder);

String (stringbuffer buffer);

1.3 Member methods

1, get

    1.1 contains the number of characters in the string, which is the length of the string.
         int length (): Gets the length.
    1.2 gets the position of a character based on its location.
          char charAt (int index): index is the subscript position of the string.
    1.3 gets the character in the string position based on the character.
         int indexOf (int ch): Returns the position of the first occurrence of the corresponding character of CH in the string. CH can be changed to char ch.
         int indexOf (int ch, int FromIndex): Gets the position where Ch appears in the string, starting at the specified position in FromIndex.

int indexOf (String str): Returns the position of the first occurrence of STR in a string.
int indexOf (string str, int fromIndex): Gets the position where Str appears in the string, starting at the specified position fromIndex.

int lastIndexOf (int ch):

2, Judge.
2.1 Whether a substring is contained in a string.
Boolean contains (str):
Special: IndexOf (str): The first occurrence of STR can be indexed if 1 is returned. Indicates that the STR does not exist in the string.
Therefore, it can also be used to determine whether the specified is included. if (str.indexof ("AA")!=-1) and the method can be judged,

There are locations where you can get the occurrences.

whether there is content in 2.2 characters.
boolean isEmpty (): The principle is to determine whether the length is 0.
2.3 Whether the string starts with the specified content.
boolean startsWith (str);
2.4 Whether the string ends with the specified content.
boolean endsWith (str);
2.5 Determines whether the string contents are the same. The Equals method in the object class is replicated.
Boolean equals (str);
2.6 Determine if the content is the same and ignore the case.
boolean equalsignorecase ();

3, Convert.
3.1 Converts a character array into a string.
Constructor: String (char[])
String (Char[],offset,count): Converts a portion of a character array into a string.

static method:
Static String copyvalueof (char[]);
Static String copyvalueof (char[] data, int offset, int count)

Static String valueOf (char[]):


3.2 Converts a string into a character array.
Char[] ToCharArray ():

3.3 Converts a byte array into a string.
String (byte[])
String (Byte[],offset,count): Converts a portion of a byte array into a string.

3.4 Converts a string into a byte array.
Byte[] GetBytes ():
3.5 Convert the base data type to a string.
static String valueOf (int)
Static String valueOf (double)

"";//string.valueof (3);

Special: Strings and byte arrays can be specified in the encoding table during the conversion process.

3.5, replace
String replace (Oldchar,newchar);

3.6, cutting
String[] Split (regex);

3.7, sub-string. Gets the part of the string.
String substring (begin);
String substring (begin,end);

3.8, convert, remove spaces, compare.
3.8.1 the string to uppercase or lowercase.
String toUpperCase ();
String toLowerCase ();

3.8.2 removes multiple spaces at both ends of a string.
String trim ();

3.8.3 a natural order comparison of two strings.
int CompareTo (string);

Returns 0 only if two strings are the same. The remainder returns the ASSII code difference of their different characters, which is the positive negative number.

Java-string,stringbuilder,stringbuffe type Use

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.