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 Java string comparison methods?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article introduces the relevant knowledge of "Java string comparison methods". In the actual case operation process, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!

In Java, there are three common methods for comparing strings: equals() method, equalsIgnoreCase() method, compareTo() method

equals()

The equals() method compares the two strings individually for each character.

"abc".equals("abc")

equalsIgnoreCase() method

equalsIgnoreCase() method has exactly the same effect and syntax as equals() method, but ignores case

"Abc".equalsIgnoreCase("abc")

returns true

compareTo() method

The compareTo() method compares the sizes of two strings in lexicographical order, based on Unicode values for individual characters in the strings.

String str1 = "a";

String str2 = "A";

System.out.println(str1.compareTo(str2));

output 32

Mistake ==

A lot of people will use == when comparing string == to indicate the comparison of the underlying address.

String str1 = "abc";

String str2 = new String("abc");

System.out.println(str1==str2);

System.out.println(str1.equals(str2));

System.out.println(str1=="abc");

System.out.println(str1.equals("abc"));

false

true

true

true

str1==str2 returns false because the two strings are stored at different addresses. str2 is a copy of str1, but with the same value, so equals

https://java-er.com/blog/java-compare-string/

"Java string comparison methods what" content is introduced here, thank you for reading. If you want to know more about industry-related knowledge, you can pay attention to the website. Xiaobian will output more high-quality practical articles for everyone!

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