Java String Class

Source: Internet
Author: User

 

First, there are two questions:

    1. String a = "abc";
    2. String B = "abc";
    1. String a = new string ("abc");
    2. String B = "abc"; A=b? is a and b equal? Why?

Analysis:

1. The string is not the first of 8 basic data types, string is an object. Because the default value of the object is NULL, the default value of string is also null, but it is a special object that has some attributes that other objects do not have.

2. Both the new string () and the new string ("") are all declared as an empty string, and the string is not null;

The string class is useful for describing a string thing, so it provides several ways to manipulate a string.

What are the common operations?

1. Get

1.1 The number of characters contained in the string, that is, the length of the string

int Length ()

1.2 Gets the position of a character based on its position.

char charAt (int index)

1.3 Gets the character's position in the string, based on the character.

int indexOf (int ch)//Returns the position of the first occurrence of the character in the string

int indexOf (int ch, int fromIndex)//starts at FromIndex specified position, gets the position of CH in the string

int indexOf (string str)//Returns the position of the first occurrence of STR in a string

int indexOf (string str, int fromIndex)//starts at the specified position of FromIndex, gets the position of STR in the string

int lastindexod (int ch)

2. Judging

2.1 Whether a substring is contained in a string.

Boolean contains (str);

What's Special: IndexOf (str):

2.2 Whether there is content in the string

Boolean isemppty ();

2.3 Whether the string starts with the specified content

Boolean startwith (str);

2.4 Whether the string ends with the specified content

Boolen Endwith (str);

2.5 Determine if string contents are the same

Boolean equals (str);

2.6 Determine if string contents are the same and ignore case

Boolean equalsignorecase ();  

3. Conversion

3.1 Turning character arrays into strings

constructor function:

Strnig (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[], offset, count);

Static String ValueOf (char ());

3.2 Convert a string to a character array

Char[] ToCharArray ();

3.3 Convert a byte array to a string

Strnig (byte[])

String (byte[], offset, count); To convert a portion of a byte array into a string

3.4 Turning a string into a byte array

Byte[] GetBytes ();

3.5 Convert a base data type to a string

static String valueOf (int)

Static String valueOf (double)

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

4. Replace

String replace (Oldchar, Newchar);  

5. Cutting

String[] Split (regex);

6. Sub-string

String substring (begin);

String substring (begin, end);

7. Conversion

7. 1 turn a string into uppercase or lowercase

String toUpperCase ();

String toLowerCase ();

7.2 Remove multiple spaces at both ends of a string

String trim ();

7.3 Comparing two strings in a natural order

int CompareTo (string);

Java 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.