Java Basics String

Source: Internet
Author: User

string literal declaration: String string = "12345";

1, the concatenation of strings:

String str1 = "123";

String str2 = "456";

Method One:

System.out.println (str1 + SR2);

Method Two:

System.out. println (Str1.concat (str2));

2. Deletion and interception of strings:

  String str = "123ASDZXVCAAAA"

  Method One: Delete all the numbers in the string

System.out.println (Str.replaceall ("\\d", ""));

Method Two: intercept string, delete

1, System.out.println (str.substring (int i)); The string is truncated from subscript I, and the element preceded by I is taken away;

2, System.out.println (str.substring (int i, int j));//The string is truncated from subscript I to subscript j;

3, System.out.println (Str.replaceall ("A", "" ")); Replace all A in the STR string with "";

3, ASCII code conversion:

  String str = "123ASDASD";

SYSTEM.OUT.PRINTLN (Str.charat (int i)); Gets the ASCII code for the character labeled I,

4. Get the specified subscript element

  String str = "1234ABCD";

System.  Out. println ("str.indexof (int i)"); Get the element labeled I;

5. Check if the string contains an element

  String str = "123qwer";

if (Str.contanins ("123")) {

System.out.println ("contains 123")

}

    

Java Basics String

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.