Organize 10 most Java String FAQ

Source: Internet
Author: User
Tags stringbuffer in python

Share my work learning Java's 10 string problems.


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 17, 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);

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.