compareTo
public int compareTo(String anotherString) {
int len1 = value.length;
int len2 = anotherString.value.length;
int lim = Math.min(len1, len2);
char v1[] = value;
char v2[] = anotherString.value;
int k = 0;
while (k < lim) {
char c1 = v1[k];
char c2 = v2[k];
if (c1 != c2) {
return c1 - c2;
}
k++;
}
return len1 - len2;
}
This is compareTo underlying code,its method is judge every code , if they are same , len1 - len2 is zero , else , The first chars string will subtract second chars string , return result , end
public class test {
public static void main(String[] args) {
String str = "gbhk";
String str2 = "bjn";
int i = str.compareTo(str2);
System.out.println(i);
String test = "g";
String test2 = "b";
System.out.println(test.compareTo(test2));
}
}
The result all is 5
compareTo just be used to Number group , it is Packaging Group Integer、Long、Byte、Double、Float、Short , but int,double and so on ,They cant use compareTo
String
String cant be change , if u want change String’s code , u must delete it , and create the same name String
StringBuffer and StringBuilder can be change
concat
public String concat(String str) {
int otherLen = str.length();
if (otherLen == 0) {
return this;
}
int len = value.length;
char buf[] = Arrays.copyOf(value, len + otherLen);
str.getChars(buf, len);
return new String(buf, true);
}
concat’s underlying code,firstly , we analyze getChars()
public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {
if (srcBegin < 0) {
throw new StringIndexOutOfBoundsException(srcBegin);
}
if (srcEnd > value.length) {
throw new StringIndexOutOfBoundsException(srcEnd);
}
if (srcBegin > srcEnd) {
throw new StringIndexOutOfBoundsException(srcEnd - srcBegin);
}
System.arraycopy(value, srcBegin, dst, dstBegin, srcEnd - srcBegin);
}
it has some judge , if we import some wrong number , will throws exception
Test
public static void main(String[] args) {
char[] chars = new char[]{'a', 'b', 'c', 'd', 'e'};
String str = "xyz";
str.getChars(0, 2, chars, 1);
System.out.println(chars);
}
The result is axyde , we can see that , getChars’s first parameter is str start , second parameter is str end , third parameter is array’s name , last paramter is array’s start position , and chars is changed , not str , so we send chars
So , we can know why concat can do this
public static void main(String[] args) {
String str = "abc";
String str2 = "def";
String concat = str.concat(str2);
System.out.println(concat);
}
StringBuffer and StringBuilder
StringBuffer and StringBuilder is different with String , String cant be change , but StringBuffer and StringBuilder can
StringBuffer is Thread safety , but StringBuilder is not
We always use StringBuilder beacuse its run is quickly , But if application ask for thread safety u must use StringBuffer
Some StringBuffer’s method
StringBuffer stringBuffer = new StringBuffer("abcd");
stringBuffer.append("efg");
System.out.println(stringBuffer); // abcdefg
stringBuffer.reverse();
System.out.println(stringBuffer); // gfedcba
stringBuffer.delete(1, 2);
System.out.println(stringBuffer); // gedcba
String str = "xyz";
stringBuffer.insert(1, str);
System.out.println(stringBuffer); // gxyzedcba
String str2 = "abc";
stringBuffer.replace(1, 3, str2);
System.out.println(stringBuffer); // gabczedcba
Regular Expression
public static void main(String[] args) {
String str = "abcdef";
String str2 = ".*bcd.*";
boolean matches = Pattern.matches(str2, str);
System.out.println(matches);
}
.* is regular expression chars , .* is different with .*? , .*? is just matching one time , but .* will matching more than once
Pattern.matches method the first parameter is element , second parameter is all character String
must use .* , else it will send false
next & nextLine
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println(":");
String next = scanner.next(); // asd asd
System.out.println(next); // asd
}
We can use Scanner method get user input
next() and nextLine() is different
next():
It must get effective chars then it will be over
If it get effective chars before get blank , will delete blank , until get effective chars
It will think blank is end chars when it find effective chars
Next() cant get characters string have blank
nextLine():
The only end code is Enter , so nextLine() can print all input code
it can get blank