logo

Java niz jednak IgnoreCase()

Java Klasa niza jednako IgnoreCase() metoda uspoređuje dva zadana niza na temelju sadržaja niza bez obzira na velika i mala slova (mala i velika) niza. To je kao metoda equals(), ali ne provjerava osjetljivost na velika i mala slova. Ako se bilo koji znak ne podudara, vraća false, inače vraća true.

Potpis

 publicboolean equalsIgnoreCase(String str) 

Parametar

str : drugi niz, tj. u usporedbi s ovim nizom.

python generira uuid

Povratak

Vraća se pravi ako su znakovi oba niza jednaki, zanemarujući velika i mala slova u suprotnom lažno .

Interna implementacija

 public boolean equalsIgnoreCase(String anotherString) { return (this == anotherString) ? true : (anotherString != null) && (anotherString.value.length == value.length) && regionMatches(true, 0, anotherString, 0, value.length); } 

Gledajući implementaciju, očito je da metoda equalsIgnoreCase() poziva metodu regionMatches(). To čini metodu equalsIgnoreCase() neosjetljivom na velika i mala slova. Signatura metode regionMatches() navedena je u nastavku.

public Boolean regionMatches(boolean ignoreCase, int toffset, String other, int ofset, int len)

Metoda regionMatches() analizira pet parametara. Prvi parametar ignoriraj velika i velika slova je postavljeno na istinito u gornjoj implementaciji. Dakle, kada se metoda izvrši, ona provjerava je li ignoriraj velika i velika slova zastava je istinita ili ne. Ako da, tada se uzima po jedan znak iz oba niza i zatim uspoređuje. Ako usporedba daje lažnu vrijednost, tada se oba znaka pretvaraju u velika slova i zatim se provjerava daje li usporedba još uvijek lažnu vrijednost, tada se oba znaka pretvaraju u mala slova i zatim uspoređuju. Ako usporedba daje pravu vrijednost, tada oba niza imaju jednak sadržaj; inače, ne. Isječak koda razmatrane usporedbe naveden je u nastavku.

pretvoriti string datum
 while (toffset <last) { char ch1="getChar(value," toffset++); ch2="getChar(other," ooffset++); if (ch1="=" ch2) continue; } convert each character to uppercase and then make the comparison. comparison yeilds a true value, next pair of characters should be scanned uch1="Character.toUpperCase(ch1);" uch2="Character.toUpperCase(ch2);" (uch1="=" u2) lowercase otherwise, return false. (character.tolowercase(uch1)="=" character.tolowercase(uch2)) false; reaching here means content both strings are same after ignoring case sensitiveness true; < pre> <p>One may argue that if we made a comparison after converting to uppercase, then why do we need an extra comparison by converting characters to the lowercase. The reason behind this is to provide to the requirement of Georgian alphabets. Conversion in uppercase does not work properly for the Georgian alphabets, as they have some strange rules about the case conversion. Therefore, one extra comparison, by converting characters to the lowercase, is required.</p> <h2>Java String equalsIgnoreCase() Method Example</h2> <p> <strong>FileName:</strong> EqualsIgnoreCaseExample.java</p> <pre> public class EqualsIgnoreCaseExample{ public static void main(String args[]){ String s1=&apos;javatpoint&apos;; String s2=&apos;javatpoint&apos;; String s3=&apos;JAVATPOINT&apos;; String s4=&apos;python&apos;; System.out.println(s1.equalsIgnoreCase(s2));//true because content and case both are same System.out.println(s1.equalsIgnoreCase(s3));//true because case is ignored System.out.println(s1.equalsIgnoreCase(s4));//false because content is not same }} </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> true true false </pre> <h2>Java String equalsIgnoreCase() Method Example 2</h2> <p>Let&apos;s see an example where we are testing string equality among the strings.</p> <p> <strong>FileName:</strong> EqualsIgnoreCaseExample2.java</p> <pre> import java.util.ArrayList; public class EqualsIgnoreCaseExample2 { public static void main(String[] args) { String str1 = &apos;Mukesh Kumar&apos;; ArrayList list = new ArrayList(); list.add(&apos;Mohan&apos;); list.add(&apos;Mukesh&apos;); list.add(&apos;RAVI&apos;); list.add(&apos;MuKesH kuMar&apos;); list.add(&apos;Suresh&apos;); for (String str : list) { if (str.equalsIgnoreCase(str1)) { System.out.println(&apos;Mukesh kumar is present&apos;); } } } } </pre> <p> <strong>Output:</strong> </p> <pre> Mukesh kumar is present </pre> <hr></last)>
Testirajte sada

Izlaz:

 true true false 

Java String equalsIgnoreCase() Primjer metode 2

Pogledajmo primjer gdje testiramo jednakost nizova među nizovima.

Naziv datoteke: EqualsIgnoreCaseExample2.java

 import java.util.ArrayList; public class EqualsIgnoreCaseExample2 { public static void main(String[] args) { String str1 = &apos;Mukesh Kumar&apos;; ArrayList list = new ArrayList(); list.add(&apos;Mohan&apos;); list.add(&apos;Mukesh&apos;); list.add(&apos;RAVI&apos;); list.add(&apos;MuKesH kuMar&apos;); list.add(&apos;Suresh&apos;); for (String str : list) { if (str.equalsIgnoreCase(str1)) { System.out.println(&apos;Mukesh kumar is present&apos;); } } } } 

Izlaz:

 Mukesh kumar is present