The following are the 10 most common questions about strings in Java.
How do I compare strings? Use = = or equals ()
Simply put, "= =" is used to determine whether a reference is equal, and equals () is used to determine whether the value is equal. You should use the Equals () method unless you want to compare whether two strings are the same object. It would be better if you knew the concept of string residency.
Use a character array rather than a string for sensitive information
Strings are immutable, meaning that once created they will persist until the garbage collector reclaims them. For an array, however, you can explicitly change their elements. With this approach, sensitive information, such as passwords, will not be present in the system for a long time.
Whether the switch statement can use string
Yes, for JDK7, we can use strings as switch conditions, but in jdk6 we can't use strings as switch conditions.
// Java 7 only! Switch (Str.tolowercase ()) { case "A": = 1; Break ; Case "B": = 2; Break ;}
How to turn a string into an integer type
int n = integer.parseint ("10");
It's simple, but sometimes it can be overlooked.
How to split a string by whitespace character (possibly multiple)
string[] Strarray = Astring.split ("\\s+");
What does the substring () method do?
The JDK 6,substring () method does not create a new character array, but instead uses the original string of the character array, in order to create a new character array consisting of a string, you can add an empty string followed by the following:
Str.substring (M, N) + ""
This creates a new character array that makes up the new string object. Using this method can sometimes improve performance because the garbage collector can reclaim useless large strings, leaving only the remaining substrings.
The Jdk7,substring () method creates a new character array instead of the one you already have, and you can refer to the following links to learn more about the similarities and differences between the substring () methods in JDK6 and JDK7.
http://www.programcreek.com/2013/09/the-substring-method-in-jdk-6-and-jdk-7/
String vs StringBuilder vs StringBuffer
String vs Stringbuilder:stringbuilder is mutable, meaning that you can modify it after it has been created.
StringBuilder vs Stringbuffer:stringbuffer is synchronous, meaning it is thread-safe, but slower than StringBuilder.
How to create a repeating string
In Python, we can simply use numeric repeating strings, whereas in Java we can use the repeat () of the StringUtils tool class under the Apache Commons Lang package, as follows:
String str = "ABCD"= stringutils.repeat (str,3); // ABCDABCDABCD
How to turn a string into a date type
String str = "Sep,"new SimpleDateFormat ("MMMM D, yy", Locale.english). Parse (str); SYSTEM.OUT.PRINTLN (date); // Tue Sep 00:00:00 EDT
Count the number of occurrences of a character in a string
Use the StringUtils tool class under the Apache Commons Lang Package
int n = stringutils.countmatches ("11112222", "1"); SYSTEM.OUT.PRINTLN (n);
Left to readers: how to detect whether a string is uppercase
Link: http://www.programcreek.com/2013/09/top-10-faqs-of-java-strings/
10 common problems in Java string "Simple Java"