The ten most common Java string problems of translation

  • 2020-04-01 03:46:07
  • OfStack

Translated from: (link: http://www.programcreek.com/2013/09/top-10-faqs-of-java-strings/)

1. How to compare strings? Equals ()?

Simply put, "==" tests that two strings have the same reference, and equals() tests that two strings have the same value. Unless you want to check whether two strings are the same object, it's better to use equals().
If you know (link: http://www.programcreek.com/2013/04/why-string-is-immutable-in-java/) mechanism will be better.

2. Why is char[] better than String for security-sensitive information?

(link: http://www.programcreek.com/2009/02/diagram-to-show-java-strings-immutability/), which means once the string is created, they will always remain there until by the garbage collector to clean up. For an array, you can explicitly modify its elements. This way, security-sensitive information, such as passwords, will not appear anywhere else on 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 the switch statement. Before JDK6, we could not use String as a condition for the switch statement.


// java 7 only!
switch (str.toLowerCase()) {
   case "a":
      value = 1;
      break;
   case "b":
      value = 2;
      break;
}

4. How to convert a string to an integer?


int n = Integer.parseInt("10");

 

Quite simply, it is used so often that it is sometimes overlooked.

5. How to decompose a string with whitespace 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+");

 

6. What does the substring() method really do?

In JDK6, the substring() method provides a window representing an array of characters of an existing string, but does not create a new string. To create a new string represented by a new array of characters, add an empty string as follows:


str.substring(m, n) + ""

 

This creates a whole new array of characters representing the new string. The above method sometimes makes the code faster because the garbage collector will recycle large, unused strings and keep only one substring.
In Oracle JDK 7, substring() creates a new array of characters instead of an existing array. (link: http://www.programcreek.com/2013/09/the-substring-method-in-jdk-6-and-jdk-7/) in the diagram illustrates the JDK 6 and JDK 7 of the substring ().

7.String vs StringBuilder vs StringBuffer

String vs StringBuilder: StringBuilder is mutable, which means that one can change its value after creation.
StringBuilder vs StringBuffer: StringBuffer is synchronous, which means it is thread-safe, but slower than StringBuilder.

8. How to 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 17, 2013";
Date date = new SimpleDateFormat("MMMM d, yy", Locale.ENGLISH).parse(str);
System.out.println(date);
//Tue Sep 17 00:00:00 EDT 2013

 

10. How to count the number of times a character appears 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
(link: http://www.programcreek.com/2011/04/a-method-to-detect-if-string-contains-1-uppercase-letter-in-java/)

Translated from: (link: http://www.programcreek.com/2013/09/top-10-faqs-of-java-strings/)


Related articles: