[Java Learning] Java strings (String)

Source: Internet
Author: User

On the surface, the string is the data between the double quotes, such as "micro-school", "Http://www.weixueyuan.net" and so on. In Java, you can use the following method to define a string:

String stringname = "string Content";

For example:

1. String url = "Http://www.weixueyuan.net";

2. String webName = "Micro School";

Strings can be concatenated with a "+", and a "+" operation of the base data type and the string is typically automatically converted to a string, for example:

Copy Plain Text New window

1. public class Demo {

2. public static void Main (string[] args) {

3. String stuname = "Xiaoming";

4. int stuage = 17;

5. Float Stuscore = 92.5f;

6.

7. String info = stuname + "Of the Age is" + Stuage + ", the score is" + Stuscore;

8. SYSTEM.OUT.PRINTLN (info);

9.}

10.}

Operation Result:

Xiaoming's age is 17, the score is 92.5.

string strings and arrays have one thing in common, when they are initialized, the length is constant and the content is unchanged. If you want to change its value, a new string is generated, as follows:

1. String str = "Hello";

2. str + = "world!";

This assignment expression looks a bit like a simple solitaire and adds a "world!" directly behind Str. String that forms the last string "Hello world!". It works like this: The program first generates a STR1 string and applies a space in memory. It is not possible to append a new string, since the string is initialized and the length is fixed. If you want to change it, only give up the original space, re-apply to accommodate "Hello world!" The memory space of the string, and then "Hello world!" The string is placed in memory.

In fact, String is a class under the Java.lang package, in the form of a standard object-oriented syntax, which should be:

1. String stringname = new String ("string content");

For example:

1. String url = new String ("Http://www.weixueyuan.net");

However, because string is particularly common, Java provides a simplified syntax.

Another reason to use simplified syntax is that there is a big waste of memory usage in terms of standard object-oriented syntax. For example, string str = new String ("abc"), actually creates two string objects, one is an "ABC" object, is stored in a constant space, and one is the space requested by the object str using the New keyword.

String manipulation

String objects have many ways to manipulate strings conveniently.

1) Length () method

Length () returns the lengths of the strings, for example:

1. String str1 = "Micro School";

2. String str2 = "Weixueyuan";

3. System.out.println ("The lenght of str1 is" + str1.length ());

4. System.out.println ("The lenght of str2 is" + str2.length ());

Output Result:

The lenght of str1 is 3

The lenght of str2 is 10

Visible, the length of each character is 1, whether it is a letter, a number, or a kanji.

2) CharAt () method

The function of the CharAt () method is to obtain the specified character in a string by index value. java specifies that the index value of the first character in the string is 0, the index value of the second character is 1, and so on. For example:

1. String str = "123456789";

2. System.out.println (Str.charat (0) + "" + Str.charat (5) + "" + Str.charat (8));

Output Result:

1 6 9

3) contains () method

The contains () method is used to detect whether a string contains a substring, for example:

1. String str = "Weixueyuan";

2. System.out.println (Str.contains ("Yuan"));

Output Result:

True

4) Replace () method

A string substitution that replaces all the specified substrings in a string, for example:

1. String str1 = "The URL of Weixueyuan is www.weixueyuan.net!";

2. String str2 = Str1.replace ("Weixueyuan", "Micro School");

3. SYSTEM.OUT.PRINTLN (STR1);

4. SYSTEM.OUT.PRINTLN (STR2);

Output Result:

The URL of Weixueyuan is www.weixueyuan.net!

The URL of the Micro School is www. net! garden.

Note: the replace () method does not change the original string, but instead generates a new string.

5) Split () method

Splits the current string with the specified string as a delimiter, and the result of the split is an array, for example:

1. Import java.util.*;

2. public class Demo {

3. public static void Main (string[] args) {

4. String str = "Wei_xue_yuan_is_good";

5. String strarr[] = Str.split ("_");

6. System.out.println (arrays.tostring (Strarr));

7.}

8.}

Operation Result:

[Wei, Xue, Yuan, is, good] (Edit: Lelinpeng Source: Network)

[Java Learning] Java strings (String)

Related Article

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.