[Java03]java string

Source: Internet
Author: User

The string is not a Java basic data type, but rather a string class in the Java standard library, and each string enclosed in double quotation marks is an instance of the string class. such as: String name="Tonny" .

1. Sub-string

Using substring, you can extract a substring from a larger string. Like what:

 String greeting="Hello World!"; System.out.println(greeting.substring(2));//llo World! System.out.println(greeting.subSequence(26));//llo W

SUBSTRING () when a parameter is accepted, the argument is the starting position of the substring, and the string is calculated from 0.
SUBSTRING () receives 2 parameters, respectively, as the start position and end position, and the truncated string contains the start position without ending position.

2. Stitching

Use "+" to stitch 2 strings. For example

String str1="Hello"String str2="World!"String str3=str1+str2; System.out.println(str3);//HelloWorld

When a string is stitched together with a non-string, the latter is converted to a string type. Any string object in Java can be converted to a string.

3.Immutable characters

There is no way to modify strings in Java, and Java uses concatenation and re-assignment to implement string modifications. The original string still exists, but the variable points to the new string. Hello is always a sequence of code elements that contain H, E, L, L, O.

4. Check that the strings are equal

This problem often occurs in various written and interview questions. The Equals method of the string is used to check whether the values of the 2 strings are equal, = = compares the addresses of the 2 strings, that is, whether they point to the same string. Strings that use substring, + intercept, or splice, although the content is the same, but the address is different.

 String a="ucas"; String b=a.substring(0,3)+"s"; System.out.println("ucas".equals(a));//true System.out.println(a.equals(b)); //true System.out.println(a==b);  //false
5. Empty string and null string

The empty string is a string object with empty content and a length of 0. Null means that there is no object associated with it. Determines whether a string object is not an empty string:

if(str.length==0)或者if(str.equals(""))

Checks for an object that is neither null nor empty:

if(str!=null&&str.length!=0)
6. String API
    • Boolean Endwith () to determine the string suffix
    • Equalignorecase () ignoring case comparisons
    • int indexOf () substring first occurrence position
    • int LastIndexOf () returns the position of the last occurrence of the string
    • int length () returns the string length
    • Boolean startwith (prefix) to determine whether a string starts with prefix
    • String toLowerCase to lowercase
    • String toUpperCase to uppercase
    • String trim () to remove spaces

[Java03]java 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.