2016/1/12 String Notes Collation

Source: Internet
Author: User

String Introduction file name TestString has an instance

The string class, which is not the basic data type of Java, is a literal type, but can be used like a basic data type and declared in double quotation marks. A string variable is created in Java using the constructor of the string class.

Common construction methods

1, String s=new string ();

2, Char a[]={' g ', ' o ', ' o ', ' d '};

String S=new string (a); string S=new string (a,1,3); takes from second to fourth characters;

The above two are equivalent to string S=new string ("good");

Method: Length () of the ①string class gets the string format str.length

② gets the index position of the string

When the IndexOf method searches for a string, it searches from the beginning of the string, returns the index where the query character first appears, and if no string is retrieved,

Returns-1. Format Str.indexof (SUBSTR)

When the LastIndexOf () method of the call string is searched, it searches from the beginning of the string, returns the index of the last occurrence, and if no string is retrieved,

Return-1. Format Str.lastindexof (SUBSTR)

If the argument in the lastIndexOf () method is an empty string "", the result is the same as the return result of the length () method that called the string.

③ truncate SUBSTRING

    SUBSTRING (start index)

    substring (start index, end index): The maximum value of the end index is the length of the string, and the substring length is the end index minus start index;

Start index cannot be greater than end index

④ remove before and after the space trim () format str2= "a ad fd a" System.out.println ("

ABC AB c go to space "+Str2.trim ());

⑤ Replace: 1, find replace first Replacefirst("Already available", "replace")

2, find replacement replace ("existing string", "string to replace")

⑥ find: Find string lastindexOf("string") to find the last character position from the back forward

Find string firstindexof("string") to find the first character position from the point of travel

⑦ judge: Whether strings are equal 1,.equals(string or variable) is case-sensitive

2,.equalsIgnorecase (string or variable) is not casing-sensitive

Start startswith ("")

End EndsWith ("")

⑧ capitalization conversion: toLowercase () lowercase toUpper() uppercase

⑨ split:split("delimiter") returns an array

Construction method: 1,string str="string constant"

2,string str=New String ("")//new means opening up the memory space

3,string str=New String (type char array)

2016/1/12 String Notes Collation

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.