Java basics: 18th string usage (I)

Source: Internet
Author: User

 

String is a special data type. It does not belong to the basic data type, but can be directly assigned as the basic data type. It is not instantiated using the new keyword. You can also use the keyword New for instantiation like other types. The following code is valid: String S1 = "this is a string! "; String S2 = new string (" this is another string! "); In addition, you do not need to use the Import Statement to import strings. You can also use the" + "operator. To connect strings, use "+. For example, S1 + S2. Some common methods of string are as follows. To illustrate the method, the sample string used in the method is str = "this is a test !"; LengthMethod definition: Public int length (). Method Description: obtains the number of characters in a string. Example: Str. Length () Result: 15 Obtains characters in a string.Method definition: Public char charat (INT index ). Method Description: obtains the index character in the string, starting from 0. Example: Str. charat (3) Result: s Note: It is 4th characters. SubstringThere are two forms. Format 1: method definition: Public string substring (INT beginindex, int endindex ). Method Description: Obtain the substring from beginindex to endindex, including beginindex, excluding endindex. Example: Str. substring () Result: His format is as follows: method definition: Public string substring (INT beginindex) method Description: Obtain the substring from beginindex to end, for example, str. substring (5) Result: is a test! Locate characters or stringsThere are four forms. Format 1: method definition: Public int indexof (INT ch) method Description: locate the character specified by the parameter. Example: Str. indexof ('I') Result: 2 Form 2: method definition: Public int indexof (INT CH, int index) method Description: locate the character specified by the parameter from index. Example: Str. indexof ('I', 4) Result: 5 Form 3, for example: method definition: Public int indexof (string Str) method Description: locate the string specified by the parameter. Example: Str. indexof ("is") Result: 2 format 4: method definition: Public int indexof (string STR, int index) method Description: locate the string specified by STR from index. Example: Str. indexof ("is", 6) Result:-1 indicates no Replacement character and stringThere are three forms. Format 1: method definition: Public String Replace (char C1, char C2) method Description: Replace the character C1 in the string with the character C2, for example, str. replace ('I', 'I') Result: this is a test! Form 2: method definition: Public String replaceall (string S1, string S2) method Description: replace all S1 in the string with S2, for example, replaceall ("is ", "is") Result: this is a test! The format is as follows. Method definition: Public String replacefirst (string S1, string S2) method Description: Replace the first S1 in the string with S2, for example, replacefirst ("is", "is"). Result: this is a test! Last time: 17th Basic Input and OutputNext time: 19th string usage (lower)Li xucheng csdn blog: http://blog.csdn.net/javaeeteacher invites you as a friend: http://student.csdn.net/invite.php? U= 124362 & C = 7be8ba2b6f3b6cc5

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.