Java string correlation

Source: Internet
Author: User

First, create and initialize the string

1.String s = "Hello World";

2.String s = new String ("Hello World");

3.String s = new String ();

s = "Hello World";

Ii. Main methods

1.*.length ()//Get length

2. Equals ()//Compare strings (determine if content is the same)

"= =" cannot be used to compare strings

3.equalsIgnoreCase ()//compare strings (ignoring case)

4.toLowerCase ()//convert to lowercase letters

5.toUpperCase ()//Convert to uppercase

6.*.trim ()//remove string and trailing spaces (middle retention)

7.subString (int beginindex)

subString (int beginindex,int endIndex)//Intercept a string from Beginindex to EndIndex-1

8.startsWith ()//check the starting character of the string

EndsWith ()//Check the end character of the string

9.charAt (int index)//returns the character at the index position of the specified indexed range starting at 0

10.indexOf (string str);//start retrieving str from the string and return to the first occurrence of the position, no return-1

LastIndexOf (string str);//start retrieving str from a string and return the last occurrence, no return 1

Three, string conversion

1. Converting a string into a character array

Public char[] ToCharArray ();

2. Converting a string into a string array

Public string[] Split (String regex);//regex is a given match

3. Convert other data types to strings

(1) public static String ValueOf (Boolean B);

(2) public static String valueOf (char c);

(3) public static String valueOf (int i);

(4) public static String valueOf (long i);

(5) public static String valueOf (float f);

(6) public static String valueOf (double D);

(7) public static String valueOf (char[] data);

(8) public static String valueOf (Object obj);

StringBuffer, variable string

So in the actual use, if you often need to modify a string, such as inserting, deleting and so on, use StringBuffer to be more appropriate.

Each modification of the StringBuffer object changes the object itself, which is the biggest difference from the string class.

1. Initialization

StringBuffer s = new StringBuffer ();

StringBuffer s = new StringBuffer ("abc");

Note: stringbuffer s = "abc"; Assignment type mismatch (Error usage)

2, StringBuffer and string convert each other

String s = "abc";

StringBuffer sb1 = new StringBuffer ("123");

StringBuffer SB2 = new StringBuffer (s); Convert String to StringBuffer

String S1 = sb1.tostring (); StringBuffer Convert to String

3. Common methods

Append ()//append content to the current string object

Deletecharat ()//delete the character at the specified position, forming the remaining content into a new string

Insert ()//Inserts the content at the specified index, forming a new string

Setcharat ()//Set the character at the index as a new character

Example StringBuffer sb = new StringBuffer ("abc");

Sb.setcharat (1, ' D '); The value of the object SB will become "ADc".

Java string correlation

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.