JAVA basics, strings, and java Basics

Source: Internet
Author: User

JAVA basics, strings, and java Basics
String (a character array, constant, unchangeable ):

1. Create and initialize a string:

1). Use a String constant to initialize String s = "hello! ";

2). Create and initialize a String () using the constructor; // Initialize an object, indicating a NULL Character Sequence

String (value); // creates a new object using an existing String constant

String (char [] value); // create a String using a character array

String (char [] value, int offset, int count); // truncates the character array offset to count to create a non-empty String

String (StringBuffer buffer); // use the StringBuffer object to initialize the String object

2. Common Methods of string:

Obtain string information:

Subscript: indexOf (sub-character) lastIndexOf (sub-character)

Character: charAt (subscript)

Byte array: getBytes ()

Character array: toCharArray ()

Length: length ()

Judge string

Equal: equals (string)

Prefix: startsWith (prefix)

Suffix: endsWith (suffix)

Size: compareTo ()

Substring: reagionMatches () (you can use the parameter list to set whether to ignore case sensitivity)

Replace string

Remove leading and trailing spaces: trim ()

Substring: split (string), StringTokenizer ()

Truncate string

Single-point truncation: subString (start subscript)

Double-point truncation: subString (start subscript, end subscript)

1) charAt (subscript): gets the character at the specified subscript position of the string and returns the char value.

2) length (): returns the length of the string and the int value.

3) getBytes (): converts a string to a byte array and returns the value of byte [].

4) indexOf (sub-string): returns the subscript of the specified sub-string in the source string. The int value is returned.-1 is not found. (You can specify the position subscript to start searching)

5) lastIndexOf (): returns the subscript of the last occurrence of the specified substring in the source string.

6) isEmpty (): checks whether the length of a string is 0 and returns a Boolean value.

7) replace (old substring, new String): replace the old substring in the source String with the specified new String, return the replaced String, return the String

8) subString (start subscript, end subscript): truncates a string from the start to the end subscript range. The result contains the start and end, excluding the end. If the end subscript is not given, it is intercepted directly to the end.

9) split (String): splits the source String according to the specified String and returns the String [] array.

10) trim (): returns a string that removes spaces at the beginning and end of a string.

11) valueOf (other types of data): converts the specified data to a string value and returns

12) toCharArray (): returns the string to be converted into a new character array.

13) toString (): returns the object itself

14) toLowerCase (): converts all strings to lowercase letters.

15) toUpperCase (): converts all strings to uppercase.

16) startsWith (prefix): determines whether the prefix is the same

17) endsWith (suffix): determines whether the suffix is the same

18) compareTo (): determines the size and relationship of strings. For details, refer to ASSCI table.

19) compareToIgnoreCase (): Ignore case sensitivity to determine the size of a string.

20) equals (string): checks whether the string is equal to the specified string. A boolean value is returned.

21) semantic signorecase (): determines whether the content is the same regardless of Case sensitivity.

22) reagionMatches (): test whether the two string regions are equal

3. String Conversion

Convert all to uppercase/lowercase:

Uppercase: toLowerCase ()

Lower case: toUpperCase ()

Conversion from other basic types:

Converts a string in double quotation marks to a numeric type: Packaging class. parse packaging class (string );

Long. parseLong ("1231 ");

Double. parsedouble( "0.213 ");

Convert other types of parameters to string type: valueOf ()

Method 1: Basic Data Type Variable + ""

Method 2: String. valueOf (other types of parameters );

4. Description of the string class:

The string class is a constant class, so the string is a constant and cannot be changed.

Related Article

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.