10 common problems and solutions for string classes (String operations) in Java _java

Source: Internet
Author: User

This article describes the 10 most common questions about string in Java:

1. String comparison, use "= =" or equals ()?
In simple terms, "= =" Determines whether two references are the same memory address (the same physical object).
equals determines whether the values of two strings are equal.
You should always use the Equals () method unless you want to determine whether two string references are the same object.
This problem is better understood if you understand the presence of strings (string interning)

2. Why is it better to use char[than string for sensitive information?
A string is an immutable object, meaning that once created, the entire object cannot be changed. Even if the novice feels that a string reference has changed, it is actually just a reference to another (new) object.
While programmers can explicitly modify character arrays, sensitive information (such as passwords) is not easily exposed elsewhere (as long as you use them to char[] 0).

3. Use string as a case condition in a switch statement?
Starting with JDK7, this is OK, wordy, Java 6 and previous versions do not support this.

Copy Code code as follows:

Only available in Java 7 and later!
Switch (Str.tolowercase ()) {
Case "a":
Value = 1;
Break
Case "B":
Value = 2;
Break
}


4. Convert String to Number
For very large numbers, use long, as shown in the following code
Copy Code code as follows:

int age = Integer.parseint ("10");
Long id = long.parselong ("190"); If the value can be very large.


5. How to split a string with a white-space character
Strings that are received by the split () method of string are parsed as regular expressions,
"\s" represents white space characters, such as space "", Tab tab "\ T", newline "\ n", carriage return "\ r".
The compiler also makes a literal transcoding when parsing the source code, so "\\s" is required.
Copy Code code as follows:

string[] Strarray = Astring.split ("\\s+");


6. How is the substring () method handled internally?
In JDK6, the substring () method is used to share the original char[] array, and a "new" string is constructed with offsets and lengths.
Want to substring () get a completely new created object, using the following method:
Copy Code code as follows:

String sub = str.substring (start, end) + "";

Of course, in Java 7, substring () created a new char[] array, not shared.
For more information, refer to the substring () methods and their differences in JDK6 and JDK7

7. String vs StringBuilder vs StringBuffer
StringBuilder are mutable, so you can modify the internal values after you create them.
StringBuffer are synchronized and therefore thread-safe, but are relatively inefficient.

8. How do I stitch the same string again?
Scenario 1: Use the StringUtils tool class of the Apache Commons Lang Library.

Copy Code code as follows:

String str = "ABCD";
String repeated = Stringutils.repeat (str,3);//ABCDABCDABCD

Programme 2:
Use StringBuilder constructs. More flexible.
Copy Code code as follows:

String src = "name";
int len = Src.length ();
int repeat = 5;
StringBuilder builder = new StringBuilder (len * repeat);
for (int i=0; i<repeat; i++) {
Builder.append (SRC);
}
String DST = builder.tostring ();


9. How do I convert a string to a date?
Copy Code code as follows:

SimpleDateFormat format = new SimpleDateFormat ("Yyyy-mm-dd");
String str = "2013-11-07";
Date date = Format.parse (str);
System.out.println (Format.format (date));//2013-11-07


10. How to count the number of occurrences of a character?
Also use the Apache Commons Lang Library StringUtils class:
Copy Code code as follows:

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.