Excerpted from Http://developer.51cto.com/art/201503/469443.htm
Here I summarize the 10 Java developers often ask about the Java string problem, if you are also Java beginners, take a closer look:
1, how to compare strings, should use "= =" or equals ()?
In general, "= =" is used to compare the reference address of a string, and equals () is the value of the comparison string. Two strings with the same value may be false with "= =", while equals () must be true. Unless the two string is the same new object, the comparison string value should be equal to equals ().
2. For those security-sensitive information, why use char[] to store better than string?
The string is immutable, which means that once it is created, it will reside permanently in memory until the garbage collector recycles it. With array storage, however, you can explicitly change the elements in the array so that the security information will probably not exist anywhere in the system memory in the array way.
3. Can I use a string to declare a switch statement?
JAVA 7 and later versions are supported. In JDK 7, you are allowed to use strings as a comparison condition for switch statements. The previous version of JDK 6 cannot be used this way:
switch (str.tolowercase ()) {case "a": = 1; Break ; Case "B": = 2; Break
4. How do I convert a string to a numeric int type?
Int
It's simple, but it's often used, and it's easy to ignore.
5, how to use space characters to split the string?
We can easily use regular expressions to split the string, "s" for space characters, such as "," "T", "R", "N"
string[] Strarray = Astring.split ("s+");
6. What is the substring () method?
In JDK 6, the substring () method provides a window that intercepts characters in the original string, and it does not create a new string instance. If you want to create a new character array, you can add a null character after substring (), just like this:
This creates a new string instance, which can sometimes make your program run faster because the garbage collector can reclaim the useless large string and preserve its substring.
7, String, StringBuilder and StringBuffer which is better?
String and Stringbuilder:stringbuilder are mutable, meaning that strings created with StringBuilder you can change it at any time. StringBuilder and Stringbuffer:stringbuffer are synchronous, and they are thread-safe (thread-safe), but are much less efficient than StringBuilder.
8, how to repeat the output string?
In Python, we only need to multiply the string by a number to repeat the output of the string. In Java, however, we can use the StringUtils repeat () method, StringUtils is one of the class library members of the Apache Common Language library.
String str = "ABCD"; = Stringutils.repeat (str,3); //
9. How do I convert a string to a date type?
You can do this in the following manner, the code is as follows:
String str = "Sep,"; New SimpleDateFormat ("MMMM D, yy", Locale.english). Parse (str); SYSTEM.OUT.PRINTLN (date); //
10. How to count the frequency of occurrences of a specified character in a string
We also took advantage of StringUtils in the Apache Common Language library, with the following code:
int n = stringutils.countmatches ("11112222", "1"
Java beginners must know about Java string problems