Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the specific Java string problems?

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

What are the specific Java string problems, I believe that many inexperienced people do not know what to do about this, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

Here are 10 questions that Java developers often ask about Java strings. If you are also a beginner in Java, take a closer look:

1. How to compare strings, should you use "= =" or equals ()?

In general, "= =" is used to compare the reference address of a string, while equals () is the value of the comparison string. The result of comparing two strings with the same value with "= =" is likely to be false, while using equals () must be true. Unless the two strings are objects from the same new, you should use equals () to compare whether the string values are the same.

2. Why is it better to use char [] to store security-sensitive information than String?

String is immutable, which means that once it is created, it resides in memory until the garbage collector collects it. However, with array storage, you can explicitly change the elements in the array, so with the array, security information may not be stored anywhere in the system memory.

3. Can I declare switch statements with strings?

JAVA 7 and later versions are supported. In JDK 7, you are allowed to use strings as comparison conditions for switch statements. Versions prior to jdk 6 cannot be used in this way:

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

4. How to convert a string to a numeric int type?

Int n = Integer.parseInt ("10")

It's simple, but it's often used and easily ignored.

5. How to split a string with space characters?

We can easily split strings with regular expressions, where "s" represents space characters, such as "", "t", "r", "n".

String [] strArray = aString.split ("s +")

6. What on earth is the substring () method?

In jdk 6, the substring () method provides a window to intercept characters in the original string, and it does not create a new String instance. If you want to create a new character array, you can add an empty character after substring (), like this:

Str.substring (m, n) + ""

This creates a new String instance, and the above method can sometimes make your program run faster, because the garbage collector can collect useless large strings and retain their substrings.

7. Which is better, String, StringBuilder or StringBuffer?

String and StringBuilder:StringBuilder are mutable, which means that you can change a string created with StringBuilder at any time. StringBuilder and StringBuffer:StringBuffer are synchronized and thread-safe (thread-safe), but much less efficient than StringBuilder.

8. How to repeat the output string?

In Python, we only need to multiply the string by a number to output the string repeatedly. However, in Java, we can use the repeat () method of StringUtils, and StringUtils is one of the class library members of the Apache common language library.

String str = "abcd"; String repeated = StringUtils.repeat (str,3); / / abcdabcdabcd

9. How to convert a string to a date type?

You can do this in the following way, and the code is as follows:

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 frequency of a specified character in a string

Similarly, we take advantage of StringUtils in the Apache common language library, with the following code:

Int n = StringUtils.countMatches ("11112222", "1"); System.out.println (n); after reading the above, have you mastered the specific methods of Java string problems? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report