Translated from: Top Questions of Java Strings
1. How do I compare strings? Use "= =" or equals ()?
Simply put, "= =" tests two strings for the same reference, 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 hosting mechanism would be better.
2. Why is security-sensitive information char[] better than string?
strings are invariant, which means that once the strings are created, they remain there until they are cleaned up by the garbage collector. And for an array, you can explicitly modify its elements. As a result, 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 a condition for a switch statement. We cannot use string as a condition for 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");
It's simple, it's too often used and sometimes it's ignored.
5. How to decompose a string with white space characters?
We can simply use regular expressions to do the decomposition. "\s" represents white space 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 an array of characters for an existing string, 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 a completely new character array that represents the new string. The above method 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 illustrates the difference between JDK 6 and JDK 7 substring ().
7.String vs StringBuilder vs StringBuffer
String vs Stringbuilder:stringbuilder is mutable, which means that people can change their values after they are created.
StringBuilder vs Stringbuffer:stringbuffer is synchronized, which means that 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 a string 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, 2013";
Date date = new SimpleDateFormat ("MMMM D, yy", Locale.english). Parse (str);
SYSTEM.OUT.PRINTLN (date);
Tue Sep 00:00:00 EDT 2013
10. How to 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 containing only uppercase letters
Translated from: Top Questions of Java Strings