Common Methods of String, common methods of String

Source: Internet
Author: User

Common Methods of String, common methods of String

Note: String stu = "abc"; String stu1 = new String ("abc"); the hash values of stu and stu1 are the same, therefore, only one HashSet can be saved (because duplicate operations are not allowed)

HashSet (stored condition) (e. hash = hash & (k = e. key) = key | key. equals (k) => if it is true, they are considered to be the same


Common Methods of String
1: Length
String. length (); // returns the string length.


2: String comparison
String 1. equals (string 2) // returns true if both strings have the same character, because the toString method of the Object is overwritten.


= Compare is the address (judge whether it is the same string object) equals compares the values of two strings. (Because the String class overrides the equals method of the Object)

String source code:

Public boolean equals (Object anObject ){
If (this = anObject ){
Return true;
}
If (anObject instanceof String) {// checks whether the passed String is a String. Otherwise, it is incomparable.
String anotherString = (String) anObject;
Int n = value. length;
If (n = anotherString. value. length) {// determine whether the two strings are of the same length
Char v1 [] = value;
Char v2 [] = anotherString. value;
Int I = 0;
While (n --! = 0) {// compare Loops
If (v1 [I]! = V2 [I]) // comparison of each character
Return false;
I ++;
}
Return true;
}
}
Return false;
}



String 1. Compare signorecase case insensitive

String aa = "wwwWWW ";
String aa1 = "wwwwwQQWWW ";
String aa2 = aa. toLowerCase (); // convert to lowercase
String aa3 = aa1.toUpperCase (); // convert to uppercase

3: String connection
Two methods:
String s = y. concat (y1) // The String must be accepted and the output is the connected String. // StringBuffer is not used.
// StringBuffer does not use object. append () to override toStrong
String name = "Xiaohua"
String a = name + ", hello ";

4: String extraction and query


String y = "raggsda ";
Y. indexOf (int ch)
Y. indexOf (String ch) // the first occurrence of a search returns a subscript, and-1 is not returned.
Y. lastindexOf (int ch)
Y. lastindexOf (String ch) // returns the subscript for the last occurrence of the search.-1 is returned if no subscript exists.
Y. substring (int index) // return the character segment after the subscript
Y. substring (int index, int index1) // return the character index between subscripts --> index1-1, Character
Y. trim () // returns a string pair without spaces

5: String. split (separator, limit)
You can use a separator to split up multiple limit entries to control the number of minutes.





String interview questions
String str = new String ("asd ");
// If the constant pool of the heap does not have asd, an object will be generated in the constant pool and an object will be generated in the heap.
Two objects are generated.


01. The value is compared with the basic data type.
02. The referenced data type is compared with the memory address.


1. Why does equals in the String class compare the String content?
01. query methods in the Object class
Public boolean equals (Object obj ){
Return (this = obj); compare the memory address
}
02. Check the methods in the String class again.
The equals method in String overrides the equals method in the Object class.
Compare content


String a = "abc ";
String B = "abc ";
String c = new String ("abc ");


System. out. println (a. equals (B); // returns true if all values are consistent.
System. out. println (a = B); // The memory address is also consistent so true
System. out. println ("************************************* ***");
System. out. println (a. equals (c); // returns true if all values are consistent.
System. out. println (a = c); // The memory address is inconsistent so false


String a = "";
String B = "bc ";
String c = "abc ";
System. out. println (c = ("a" + "bc"); // true

01. During program compilation, constant (unchanged) a and constant bc are merged into abc.
02. then compare it with "abc"
 
 
String a = "";
String B = "bc ";
String c = "abc ";
System. out. println (c = (a + B); // false

01. both a and B are variables (changes) and the value of the variable cannot be determined during compilation.

02. c has been confirmed during compilation, but AB cannot.


String s1 = "abc ";
StringBuffer s2 = new StringBuffer (s1 );
System. out. println (s1.equals (s2); // print to false
/**
* The equals method of String source code mainly describes the instanceof String parameter.
* The ancestor of StringBuffer s2 is CharSequence and all are not equal.
*
*/
StringBuffer s3 = new StringBuffer ("abc ");
System. out. println (s3.equals ("abc"); // false
/**
* StringBuffer does not overwrite the equals method.
* Therefore, The equals method of the Object is called only when the address of the two objects is the same =
* (That is, the same object) equals returns true.
*/
System. out. println (s3.toString (). equals ("abc"); // true
/**
* Because StringBuffer overrides the toString method, all returned strings
* Finally, call the String equals Method for comparison. Therefore, return true. You can check the source code of String equals.
*
*/

Final String a = "";
Final String B = "bc ";
String c = "abc ";
System. out. println (c = (a + B); // true


Final can be modified:
01. The modified attribute cannot be changed during constant running.
02. The modification method cannot be rewritten by the quilt class.
03. The modified class cannot be inherited



-1 + 2 =?
1 0 0 0 0 0 0 1 01: Find the source code of-1
1 1 1 1 1 1 1 0 02: locate the-1 anti-code // when the reverse conversion is performed, the symbol bit remains unchanged and the rest is reversed. If the value is 0, the value 1 is changed to 0.
1 1 1 1 1 1 1 1 03: locate the-1 Complement
0 0 0 0 0 1 0 04: locate the 2's Complement
======================================
0 0 0 0 0 0 1


Binary complement de-operation;
The leftmost value indicates positive and negative: 0 is positive, and 1 is negative.
Negative source code ==> complement code formula source to reverse + 1


Negative complement ==> source code formula complement first change the reverse code + 1
Negative complement ==> source code formula complement first-1 and then reverse



















Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.