Java judges strings are equal

Source: Internet
Author: User

I haven't used Java for a long time. Today, a new Java student asked me not to use the EQUAL () function to help him determine whether the strings are equal, let's review Java.

Package bzu.edu.cn; </P> <p> public class text {</P> <p>/** <br/> * @ Param ARGs <br/> */<br/> Public static void main (string [] ARGs) {<br/> // todo auto-generated method stub <br/> string str1 = "ABCD"; <br/> string str2 = "ABCD "; <br/> string str3 = "BaCe"; </P> <p> // determines whether the string is equal. <br/> If (equ (str1, str3 )) <br/>{< br/> system. out. println (str1 + "and" + str3 + "equal"); <br/>}else <br/>{< br/> system. out. println (str1 + "and" + Str3 + "not equal"); <br/>}</P> <p> If (equ (str1, str2 )) <br/>{< br/> system. out. println (str1 + "and" + str2 + "equal"); <br/>}else <br/>{< br/> system. out. println (str1 + "and" + str2 + "not equal "); <br/>}</P> <p> Public static Boolean equ (string str1, string str2) <br/>{< br/> Boolean isequ = false; // The initial values are not equal. <br/> If (str1.length () = str2.length ()) <br/>{< br/> for (INT I = 0; I <str1.length (); ++ I) <br/>{< br/> If (str1.char At (I )! = Str2.charat (I) <br/> return isequ; <br/>}< br/>} else <br/>{< br/> return isequ; <br/>}< br/> isequ = true; </P> <p> return isequ; <br/>}</P> <p >}< br/>

 

 

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.