Initialization of 1, 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 object-oriented Standard syntax, the format should be: &NB Sp 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 function 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: &NBSP string s = "abc"; char C = s.chatat (1); The value of the variable c is ' B '. b, CompareTo method The effect of this method is to compare the size of 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: & nbsp string s = "ABC"; &NB Sp string S1 = "Abd"; &NB Sp int value = S.compareto (S1);    The value is a value less than 0, that is, 1. A similar method exists in the String class Comparetoignorecase, this method is to ignore the case of characters for comparison, the comparison of the rules and compareto the same. Example: string s = "aBc"; &NB Sp string S1 = "ABC"; &NB Sp 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, and to concatenate two strings to form a new string. Example: string s = "abc"; &NB Sp string S1 = "Def"; &NB Sp 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: &N Bsp string s = "abc"; &NB Sp string S1 = "Def"; &NB Sp string s2 = "1234"; string s3 = S.concat (S1). Concat (S2); The resulting new string S3 the value "abcdef1234". In factIn practical use, the syntax provides a simpler form of using "+" 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. 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. 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 if the end of the corresponding string, Returns True. 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 judged: 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 role of this method is to convert the string to the corresponding byte array, thus facilitating data storage and transmission. Example:   String s = "Computer"; byte[] B = s.getbytes (); //
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.