Java String Ten Quiz backup

Source: Internet
Author: User

How do I 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.


? 123 String str = "ABCD"; String repeated = Stringutils.repeat (str,3); Abcdabcdabcd



How do I count the number of occurrences of a character in a string?

Use the StringUtils class in the Apache Commons lang package.


How do I convert a string to an integer?

? 1 int n = integer.parseint ("10");


-Int n = stringutils.countmatches ("11112222", "1"); SYSTEM.OUT.PRINTLN (n);

Why is security-sensitive information char[] better than string?

Strings are immutable, which means that once the strings are created, they remain there until the garbage collector cleans them up. And for an array, you can explicitly modify its elements. This way, security-sensitive information, such as passwords, will not appear anywhere else in the system.


How do I compare strings? with "= =" or equals ()?

Simply put, "= =" tests whether the references of two strings are the same, and 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 dwell mechanism is better.




Can we use string in the switch statement?

For Java7 the answer is yes. Starting with JDK7, we can use string as the condition of the switch statement. We cannot use string as a condition of a switch statement before JDK6.

From Codego.net


? 123456789//Java 7 only!       Switch (Str.tolowercase ()) {case "a": value = 1;    Break       Case "B": value = 2; Break }




Very simple, too frequent to use and sometimes overlooked.


How do I break a string with a white-space character?

We can simply use regular expressions to do the decomposition. "\s" stands for whitespace characters, such as "," \ T "," \ r "," \ n ".


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



What does the substring () method really do?

In JDK6, the substring () method provides a window that represents a character array of existing strings, 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:


? 1 str.substring (M, N) + ""



This creates an entirely new array of characters representing the new string. The method above 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 shows the difference between JDK 6 and JDK 7 substring ().


String vs StringBuilder vs StringBuffer

String vs Stringbuilder:stringbuilder is mutable, which means that a person can change its value after it is created.

StringBuilder vs Stringbuffer:stringbuffer is synchronous, which means it is thread-safe, but slower than StringBuilder.




How do I convert a string to a date?

? 1234 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





This article is from the "Zhutousandebokezhijia" blog, make sure to keep this source http://9529231.blog.51cto.com/9519231/1627044

Java String Ten Quiz backup

Related Article

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.