Method of String

Source: Internet
Author: User
Tags first string numeric memory usage numeric value
String Class

1. Initialization of String objects

Because string objects are particularly common, Java provides a simplified special syntax when initializing a string object, in the following format:

String s = "abc";

s = "Java language";

In fact, according to the object-oriented standard syntax, the format should be:

string s = new String ("abc");

s = new String ("Java language");

Just according to object-oriented standard syntax, there is a big waste in memory usage. For example, string s = new String ("abc"); Two string objects are actually created, one is an "ABC" object, stored in a constant space, and one is a space that uses the new keyword as the object s application.

The parameters of other construction methods can be referred to the API documentation for the string class.

2, common operation of strings

A, Charat method

The effect of this method is to get the specified character in the string by index value (the index value of the first character in the string is 0, the second character is 1, and so on). For example:

String s = "abc";

char C = s.chatat (1);

The value of the variable c is ' B '.

B, CompareTo method

The purpose of this method is to compare the size of the two strings, which is to compare the character encoding of each character in turn. First, compare the first character of the two string, returns a value greater than 0 if the character encoding of the first string is greater than the character encoding of the second string, and returns less than 0 if less than, or 0 if the character encoding in the two string is identical.

For example:

String s = "abc";

String S1 = "Abd";

int value = S.compareto (S1);

Value is a value that is less than 0, that is, 1.

There is also a similar method comparetoignorecase in the string class, which ignores the case of characters and compares the rules with CompareTo. For example:

String s = "aBc";

String S1 = "ABC";

int value = S. Comparetoignorecase (S1);

Then value is 0, or two strings equal.

C, concat method

The purpose of this method is to concatenate strings, to concatenate two strings to form a new string. For example:

String s = "abc";

String S1 = "Def";

String s2 = s.concat (S1);

The value of the new string S2 generated after the connection is "abcdef", while the value of the string s and S1 does not change. If you need to connect multiple strings, you can use the following methods:

String s = "abc";

String S1 = "Def";

String s2 = "1234";

String s3 = S.concat (S1). Concat (S2);

The resulting new string S3 the value "abcdef1234".

In fact, in actual use, the syntax provides a simpler form, is to use "+" for string concatenation. For example:

String s = "abc" + "1234";

The value of the string s is "abc1234", which makes writing easier and more intuitive.

and use "+" to connect, not only to connect strings, but also to connect to other types. However, when a connection is required, at least one content that participates in the connection is a string type. and the "+" matching order is left-to-right, if the contents of the connection between the two are basic numeric type, then the addition operation, if the participation of the content of the connection is a string to follow the string to connect.

For example:

int a = 10;

String s = "123" + A + 5;

Then after the connection the value of the string s is "123105", the calculated procedure is to first concatenate the value of the string "123" and the variable A, generate the string "12310", and then use the string to connect with the number 5 to generate the final result.

And the following code:

int a = 10;

String s = a + 5 + "123";

Then the value of the string s is "15123" after the connection, and the calculation process is to first compute a and 5, since all are numeric, the addition or the numeric value of 15, and then use the numeric value 15 and the string "123" to connect to obtain the final result.

and the following connection code is wrong:

int a = 12;

String s = a + 5 + ' s ';

Because there is no string that participates in the connection, the computed result is a numeric value that cannot be assigned a numeric value to the string s when assigned.

D, EndsWith method

The purpose of this method is to determine whether a string ends with a string, and returns True if it ends with the corresponding string.

For example:

String s = "Student.doc";

Boolean B = S.endswith ("Doc");

The value of the variable B is true.

E, Equals method

The purpose of this method is to determine whether the contents of two string objects are the same. Returns true if the same, otherwise returns false. For example:

String s = "abc";

string S1 = new String ("abc");

Boolean B = S.equals (S1);

The use of "= =" compares the two objects in memory stored in the same address. For example, in the above code, if you judge:

Boolean b = (s = = S1);

The value of variable B is false, because the address of the S object is the address of "ABC", and S1 uses the new keyword to apply for additional memory, so the memory address is not the same as the address of "ABC" of S, so the value obtained is false.

There is a similar method equalsignorecase in the string class, which is used to ignore case comparison of two strings for the same content. For example:

String s = "abc";

String S1 = "ABC";

Boolean B = S. Equalsignorecase (S1);

The value of the variable B is true.

F, GetBytes method

The purpose of this method is to convert the string to the corresponding byte array, thus facilitating the storage and transmission of the data. For example:

String s = "Computer";

Byte[] B = s.getbytes (); Convert to byte array using native default string

Byte[] B = s.getbytes ("gb2312"); Convert to byte array using the GB2312 character set

In the actual conversion, be sure to pay attention to the character set problem, otherwise the Chinese in the conversion will have problems.

G, IndexOf method

The purpose of this method is to find the starting position of a particular character or string in the current string, or return 1 if it does not exist. For example:

String s = "abcded";

int index = s.indexof (' d ');

int index1 = S.indexof (' h ');

Returns the position of the first occurrence of the character D in the string s, with a value of 3. Because the character H does not exist in the string s, the index1 value is-1.

Of course, you can also find the corresponding characters from a specific location, such as:

int index = s.indexof (' d ', 4);

To find the first character d in the string s that appears after the index value 4 (including 4), the value of index is 5.

Because IndexOf is overloaded, you can also find the starting position of a particular string appearing in the current string, in the same way as you would find a character.

Another similar approach is the LastIndexOf method, which is used to look forward from the end of the string to the specified character or string that appears for the first time, for example:

String s = "abcded";

int index = S. LastIndexOf (' d ');

The value of index is 5.

H, Length method

The purpose of this method is to return the length of the string, that is, to return the number of characters in the string. Chinese characters are also one character. For example:

String s = "abc";

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.