Java String Types string

Source: Internet
Author: User

In essence, a string is actually an array of type char, represented by the Java.lang.String class, which has a series of properties and methods that provide some manipulation of the string. In addition, Java provides the StringBuffer class to handle variable-length strings.

1. Declaration, creation, and initialization of strings

A, String a= "Hello";

b, String a = new string ();//Create an empty string

c, String a = new string (char[] a);//Use all elements in a character array as the value of a string

D, String a = new string (char[] a,offset,count);//Use a portion of the string array as the value of the string, where offset represents the position at which to start interception, and Count is Geshu

E, String a = new string (original);//Use string constants to create

2. Connection of strings

Connect using the "+" sign

3. Comparison of strings

A, the CompareTo () method under the String class, compares the string in dictionary order, and returns an int value

b, the Equals () method of the object, which compares the contents of two strings for equality, returns a Boolean value

C, Equalsignorecase (), regardless of case, compares the contents of two strings for equality, returning a Boolean value

4. Method of String

(1), Length (), returns the lengths of the strings

(2), indexOf (), returns the index position of the first occurrence of the character (string) in the specified string, if it can be found, returns the index value, or 1 if it is not found;

(3), subString (), you can take a string to the operation, two methods

A, subString (int n), returns the character after n (string)

b, subString (int n,int m), returns the characters from N to M (string)

5, replace (), returns a new character, which he gets by replacing all occurrences of the specified string in this string with a new string, the original string does not change

6, StartsWith () and EndsWith () to determine whether a string starts and ends with the specified string

Example: For simple authentication, ID number 130688888558558558558,130 for Hebei province, at the end of the singular male, even female

7, toLowerCase () and toUpperCase (), convert all strings to lowercase or uppercase

StringBuffer:

1. Statement

A, construction without parameters, default 16 characters

B, construction method with integer number as parameter stringbuffer (int length)

C, construction method with string object as parameter StringBuffer (String str)

2, append (), append to the string content, there are many ways to implement, you can append any type of data

3, Deletecharat (), delete the character at the specified position, and form the remaining characters into a new string

4, insert (), inserts the content in the string type, then forms a new string, and the append () difference is: can be inserted at any place

5, Setcharat (), modify the object's index value to a new character in the specified position

Java String Types string

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.