Translated from: Top ten questions of Java Strings
1. How do I compare strings? with "= =" or equals ()?
Simply put, "= =" tests whether the references of two strings are the same, and equals () tests whether the values of two strings are the same. Unless you want to check whether two strings are the same object, it is best to use equals ().
If you know the string dwell mechanism is better.
2. Why is security-sensitive information char[] better than string?
Strings are immutable, which means that once the strings are created, they remain there until the garbage collector cleans them up. And for an array, you can explicitly modify its elements. This way, security-sensitive information, such as passwords, will not appear anywhere else in the system.
3. Can we use string in the switch statement?
For Java7 the answer is yes. Starting with JDK7, we can use string as the condition of the switch statement. We cannot use string as a condition of a switch statement before JDK6.
Java 7 Only!switch (Str.tolowercase ()) {case "a": value = 1; break; Case "B": value = 2; break;}
4. How do I convert a string to an integer?
int n = integer.parseint ("10");
Very simple, too frequent to use and sometimes overlooked.
5. How do I break down a string with a white-space character?
We can simply use regular expressions to do the decomposition. "\s" stands for whitespace characters, such as "," \ T "," \ r "," \ n ".
string[] Strarray = Astring.split ("\\s+");
What does the 6.substring () method really do?
In JDK6, the substring () method provides a window that represents a character array of existing strings, but does not create a new string. To create a new string that is represented by a new character array, you can add an empty string as follows:
Str.substring (M, N) + ""
This creates an entirely new array of characters representing the new string. The method above sometimes makes the code faster because the garbage collector reclaims large unused strings, leaving only one substring.
In Oracle JDK 7, SUBSTRING () creates a new character array without an existing array. The substring () Method in JDK 6 and JDK 7 shows the difference between JDK 6 and JDK 7 substring ().
7.String vs StringBuilder vs StringBuffer
String vs Stringbuilder:stringbuilder is mutable, which means that a person can change its value after it is created.
StringBuilder vs Stringbuffer:stringbuffer is synchronous, which means it is thread-safe, but slower than StringBuilder.
8. How do I repeat a string?
In Python, we can repeat a string by multiplying it by a number. In Java, we can repeat strings through the repeat () method of the StringUtils class in the Apache Commons lang package.
String str = "ABCD"; String repeated = Stringutils.repeat (str,3);//ABCDABCDABCD
9. How do I convert a string to a date?
String str = "SEP,";D ate Date = new SimpleDateFormat ("MMMM D, yy", Locale.english). Parse (str); SYSTEM.OUT.PRINTLN (date);//tue Sep 00:00:00 EDT 2013
10. How do I count the number of occurrences of a character in a string?
Use the StringUtils class in the Apache Commons lang package.
int n = stringutils.countmatches ("11112222", "1"); SYSTEM.OUT.PRINTLN (n);
Additional Questions
How to detect a string that contains only uppercase letters
Translated from: Top ten questions of Java Strings
10 most common Java string problems