Dark Horse: String string

Source: Internet
Author: User

1. Two instantiation Methods: String

String str1 = "ABC"; // good, heap memory is shared. A string is an anonymous object of the string class. Anonymous objects are objects that have opened up heap memory space and can be used directly. The same heap memory is shared when multiple objects are instantiated.

Sring str2 = new string ("ABC"); // The heap memory is not shared. Multiple heap memory is allocated when multiple objects are instantiated. This will cause a waste of memory.

2. Equals, = differences

= Compare whether the addresses of the two objects are the same; equals compare whether the memory content of the two objects is

3. String Method

First, obtain

A. Obtain the number of characters in a string, that is, the length of the string.

Int length ().

B. Obtain a character at the position based on the position.

Char charat (INT index ).

C. Obtain the position of the string that appears for the first time based on the character.

Int indexof (INT ch): return the location where ch first appears in the string.

Int indexof (int ch, int formindex): obtains the position where ch appears in the string starting from the specified position of formindex.

Int indexof (string Str): return the position where STR first appears in the string.

Int indexof (string STR, int formindex): obtains the position where STR appears in the string starting from the specified position of formindex.

Int lastindexof (INT ch): searches forward from the end of the string, and returns the location where the CH first appears in the string.

Note: If the method cannot be obtained,-1 is returned.

Ii. Judgment

A. Whether the string contains a substring.

Booleancontains (STR );

Special note: indexof (STR) can be used to search for the position where STR appears for the first time. If-1 is returned, STR does not exist in the string. Therefore, indexof (STR) can also be used to determine whether or not it contains.

B. Whether there is content in the string.

Booleanisempty ();

C. Whether the string starts with the specified content.

Booleanstartswith (STR );

D. Whether the string ends with the specified content.

Booleanendswith (STR );

E. Determine whether the string content is the same and overwrite the equals method in the object.

Booleanequals (STR );

F. Determine whether the content is the same and ignore the case sensitivity.

Booleanequalsignores (STR );

Third, conversion

A. Convert character arrays to strings

Constructor:

String (char [])

String (char [], offset, count) converts one part of the character array into a string

Static Method:

Staticstring copyvalueof (char []);

Staticstring copyvalueof (char [] data, int offset, int count );

Staticstring valueof (char []);

B. convert a string to a character group.

Char [] tochararaay (); when the object is this, there is no Parameter

C. Convert the byte array into a string.

String (byte []);

String (byte [], offset, count );

D. convert a string into bytes.

Byte [] getbytes ();

E. Convert the basic data type to a string.

Staticstring valueof (INT );

Staticstring valueof (double );

Special: the encoding table can be specified during conversion of string and byte arrays.

Fourth, replace

String Replace (oldchar, newchar );

Fifth, cutting

String [] aplit (RegEx );

6. substrings

Obtain part of a string

String substring (BEGIN); from the specified position to the end

Stringsubstring (begin, end); contains the header, not the end

7. Conversion

Converts a string to uppercase or lowercase letters.

String touppercase ();

Stringtolowercase ();

Eighth, remove Spaces

Stringtrim ();

IX. Comparison

Intcompareto (string );

 

Booleanequalsignorecase ();

Public classstringdemo01 {

Publicstatic void main (string [] ARGs ){

Method_is ();

Method_get ();

}

Publicstatic void method_is (){

Stringstr = "arraydemo. Java ";

 

// Determine whether the file name starts with Array

Sop (Str. startswith ("array "));

// Determine whether the file name is. Java files

Sop (Str. endswith (". Java "));

// Determine whether the file contains a demo string

Sop (Str. Contains ("demo1 "));

}

 

Publicstatic void method_get (){

Stringstr = "abcdeakpf ";

 

// Obtain the string length

Sop (Str. Length ());

// Returns the stringindexoutofboundsexception when the subscript is out of bounds.

Sop (Str. charat (4 ));

// Obtain the index based on Characters

Sop (Str. indexof ('M', 3 ));

// Reverse index where a character appears

Sop (Str. lastindexof (""));

 

}

 

Publicstatic void Sop (Object OBJ ){

System. Out. println (OBJ );

}

}

4. strinbuffer

Stringbuffer is a string buffer, a container, can be seen as a basin and a plane, and the length is changeable.

C: Create U: Update R: Read D: Delete

1. Storage

Stringbufferappend (): add the specified data as a parameter to the end of the existing data.

Stringbufferinsert (index, data): You can insert data to the specified index position.

2. Delete

Stringbufferdelete (START, end): deletes data in the buffer, including the header, not the end.

Stringbufferdeletecharat (): deletes data at a specified location.

Third, obtain

Char charat ()

Intindexof ()

Stringsubstring (INT start, int end)

4. Modify

Stringbufferreplace (START, end, string );

Voidsetcharat (INT index, char ch );

Fifth, reverse

Stringbufferreverse ();

5. stringbuilder

Stringbuilder is similar to stringbuffer, but stringbuffer is thread-safe, but has low efficiency. stringbuilder is thread-insecure, but has high efficiency. It can be solved with lock and unlock for thread-safety.

 

Summary:

1. String nine methods are used to operate string strings.

2. Difference Between stringbuffer and stringbuilder, thread security and efficiency.

 

 

 

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.