Ten Most common Java string problems, ten java strings

Source: Internet
Author: User

Ten Most common Java string problems, ten java strings

Translated from: Top 10 questions of Java Strings

1. How to compare strings? "=" Or "equals ()?

To put it simply, "=" tests whether the references of two strings are the same, and equals () tests whether the values of the two strings are the same. Unless you want to check whether two strings are the same object, you 'd better use equals ().
It will be better if you know the string resident mechanism.

2. Why is char [] Better Than String in terms of security-sensitive information?

The strings remain unchanged, which means that once the strings are created, they will remain there until they are cleared by the garbage collector. For an array, you can modify its elements explicitly. In this way, security sensitive information (such as passwords) will not appear anywhere in the system.

3. Can we use String in the switch statement?

For Java 7, the answer is yes. From JDK 7, we can use String as the condition for the switch statement. Before JDK 6, we cannot use String as a condition for switch statements.

// 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");

It is very simple. It is used too frequently and sometimes ignored.

5. How to break down a string with blank characters?

We can simply use regular expressions for decomposition ." \ S represents blank characters, such as "," \ t "," \ r ", and" \ n ".

String[] strArray = aString.split("\\s+");
6. What did the substring () method actually do?

In JDK 6, the substring () method provides a window for representing the character array of an existing string, but does not create a new string. To create a new string represented by a new character array, you can add an empty string as follows:

str.substring(m, n) + ""

In this way, create a new character array that represents 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 character array instead of an existing array. The chart in The substring () Method in JDK 6 and JDK 7 shows The differences between The substring () of JDK 6 and JDK 7.

7. String vs StringBuilder vs StringBuffer

String vs StringBuilder: StringBuilder is variable, which means that after creation, the user can change its value.
StringBuilder vs StringBuffer: StringBuffer is synchronized, which means it is thread-safe, but it is slower than StringBuilder.

8. How to repeat a string?

In Python, We can multiply by a number to repeat the string. In Java, we can use the repeat () method of the StringUtils class in the Apache Commons Lang package to repeat the string.

String str = "abcd";String repeated = StringUtils.repeat(str,3);//abcdabcdabcd
9. How to 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
How to detect a string containing only uppercase letters

Translated from: Top 10 questions of Java Strings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.