Java basics: 19th string usage (below)

Source: Internet
Author: User

 

The string method described in this article includes comparison content, case-sensitivity conversion, prefix and suffix, and an example is provided. Compare string content. Format 1: method definition: Public Boolean equals (Object O) method Description: compare whether the parameter is the same, case sensitive. Example: Str. equals ("This") Result: False Form 2: method definition: Public Boolean inclusignorecase (Object O) method Description: whether the comparison is the same as the parameter, do not distinguish between uppercase and lowercase. Example: Str. Repeated signorecase ("This") Result: false Case-sensitive ConversionConvert to uppercase or lowercase. Convert to uppercase: method definition: Public String touppercase () method Description: Convert all characters in the string to uppercase. Example: Str. touppercase () Result: this is a test! Convert to lower case: method definition: Public String tolowercase () method Description: Convert all characters in the string to lower case. Example: Str. tolowercase () Result: this is a test! Prefix and suffixDetermines whether the string starts or ends with a specified parameter. Prefix: method definition: Public Boolean startswith (string prefix) method Description: determines whether the string is prefixed with the substring specified by the parameter. Example: Str. startswith ("This") Result: True to determine the Suffix: method definition: Public Boolean endswith (string suffix) method Description: whether the string is suffixed with the substring specified by the parameter. Example: Str. endswith ("This") Result: false [Example] determines the number of times that another string appears in a string. Package ch8; import Java. io. datainputstream; public class stringtest {public static void main (string ARGs []) {system. out. println ("calculates the number of times the first string appears in the second string. "); Datainputstream din = new datainputstream (system. in); try {system. out. println ("Enter the first string"); string str1 = din. readline (); system. out. println ("enter the second string"); string str2 = din. readline (); string str3 = str2.replace (str1, ""); int COUNT = (str2.length ()-str3.length ()/str1.length (); system. out. println (str1 + "appear in" + str2 + ":" + count);} catch (exception e) {system. out. println (E. tostring () ;}} the running result is: Calculates the number of times the first string appears in the second string. Enter the first string AB. Enter the second string abcedabsdabajabab in abcedabsdabajab for the following times: 4 Note that the string itself is a constant. Once a string is created, its content cannot be changed, so how to explain the following code: S1 + = S2; the content of string S2 is not added to the end of string S1, instead, a new string is created. The content is the connection between S1 and S2, and S1 is directed to the newly created string. If the content of a string needs to change frequently, the string should not be used, because the object creation process is in fact during the change process. In this case, the stringbuffer should be used. Last time: 18th string usage (I)Next time: 20th conversion between Java basic types and stringsLi 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.