Java Chuanjiang How string objects are defined

Source: Internet
Author: User
Tags first string

How do I define a string object? string s = "abc"; The data that is caused by double quotation marks is a string object.

features : Once the string is initialized, it cannot be changed and stored in a constant pool in the method area. Because the string class is final decorated

------------------------------------------------------

String S1 = "abc";//S1 there is only one object in memory pointing to ABC.

string s2 = new String ("abc");//s2 There are two object ABC, new in the point .

System.out.println (S1==S2);//false

System.out.println (s1.equals (S2));//true , equals in the string compares the contents of the strings.

-------------------------------------------------------

Method of String:

1 : Constructor method: converts a byte array or an array of characters into a string.

string S1 = new string ();//A string that creates an empty content.

String s2 = Null;//s2 does not have any object pointing, it is a null constant value.

string s3 = "";//s3 points to a specific string object, except that there is no content in the string.

// generally, when you define a string, you do not need new.

String S4 = new String ("Java video");

String S5 = "Java video"; This is commonly used in this notation

new String (char[]);// converts a character array into a string.

new String (Char[],offset,count);// converts a portion of a character array into a string.

2 : General method:

According to the idea of object-oriented:

2.1 Get:

2.1.1: Gets the length of the string. length ();

2.1.2: The character at the specified position. Char charAt(int index);

2.1.3: Gets the position of the specified character. If there is no return-1, you can use the return value-one to determine if a character does not exist.

int indexOf(int ch);//Returns the first-time character corner label

int indexOf (int ch,int fromIndex); Returns the first time a corner label was found at the beginning of the specified position

int indexOf (String str); Returns the first string corner label found

int indexOf (String str,int fromIndex);

int lastIndexOf(int ch);

int lastIndexOf (int ch,int fromIndex);

int lastIndexOf (String str);

int lastIndexOf (String str,int fromIndex);

2.1.4: Gets the substring.

String substring(int start);//starting at the start bit, up to length ()-1.

String substring (int start,int end);//from Start to end. Contains the start bit, and does not contain an end bit.

SUBSTRING (0,str.length ());//Get the whole string

2.2 Judgment:

2.2.1: Does the string contain the specified string?

Boolean contains(String substring);

2.2.2: Does the string start with the specified string?

Boolean startsWith(string);

2.2.3: Does the string end with the specified string?

Boolean EndsWith (string);

2.2.4: Determine if strings are the same

Boolean equals(string);//Overrides the method in object to determine whether the contents of the string are the same.

2.2.5: Determines whether the string contents are the same, ignoring case.

Boolean equalsignorecase(string);

2.3 Conversion:

2.3.1: You can convert a character array or a byte array into a string by using a constructor function.

2.3.2: You can convert a character array to a string by using a static method in the string.

Static String copyvalueof(char[]);

Static String copyvalueof (Char[],int offset,int count);

Static String valueOf(char[]);

Static String valueOf (Char[],int offset,int count);

2.3.3: Converts the base data type or object into a string.

Static String valueOf (char);

Static String ValueOf (Boolean);

Static String valueOf (double);

Static String valueOf (float);

static String valueOf (int);

Static String valueOf (long);

Static String valueOf (Object);

2.3.4: Turns the string to case.

String tolowercase();

String touppercase();

2.3.5: Converts a string to an array.

Char[] tochararray();//turn into a character array.

Byte[] getBytes();//Can be added to the encoding table. Turns into a byte array.

2.3.6: Converts a string into an array of strings. Cutting method.

String[] split(split rule-string);

2.3.7: Replace the string with the content. Note: Changing to a new string does not modify the original string directly.

String Replace(Oldchar,newchar);

String replace (oldstring,newstring);

2.3.8: string concat(string);//Append to Strings.

String trim();//remove spaces at both ends of the string

int compareTo();//If the argument string equals this string, the value 0 is returned, or if the string is less than the string parameter in dictionary order, a value less than 0 is returned if the string is larger than the string parameter in dictionary order , a value greater than 0 is returned .

Java Chuanjiang How string objects are defined

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.