Java Razred nizov enako IgnoreCase() metoda primerja dva podana niza na podlagi vsebine niza ne glede na velikost (malo in veliko) niza. Je tako kot metoda equals(), vendar ne preverja občutljivosti med velikimi in malimi črkami. Če se kateri koli znak ne ujema, vrne false, sicer vrne true.
Podpis
publicboolean equalsIgnoreCase(String str)
Parameter
str : drug niz, tj. v primerjavi s tem nizom.
preklopna metoda java
Vračila
Vrača se prav če so znaki obeh nizov enaki, sicer zanemarimo velike in male črke lažno .
Notranja izvedba
public boolean equalsIgnoreCase(String anotherString) { return (this == anotherString) ? true : (anotherString != null) && (anotherString.value.length == value.length) && regionMatches(true, 0, anotherString, 0, value.length); }
Če pogledamo izvedbo, je očitno, da metoda equalsIgnoreCase() kliče metodo regionMatches(). Zaradi tega je metoda equalsIgnoreCase() neobčutljiva na velike in male črke. Podpis metode regionMatches() je omenjen spodaj.
public Boolean regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len)
Metoda regionMatches() razčleni pet parametrov. Prvi parameter ignoreCase je v zgornji izvedbi nastavljen na true. Tako, ko se metoda izvede, preveri, ali je ignoreCase zastava je resnična ali ne. Če da, se vzame po en znak iz obeh nizov in nato primerja. Če primerjava daje napačno vrednost, se oba znaka pretvorita v velike črke in nato preverita, ali primerjava še vedno daje napačno vrednost, potem se oba znaka pretvorita v male črke in nato primerjata. Če primerjava daje pravo vrednost, imata oba niza enako vsebino; sicer pa ne. Delček kode obravnavane primerjave je omenjen spodaj.
c programi
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='javatpoint'; String s2='javatpoint'; String s3='JAVATPOINT'; String s4='python'; 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'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 = 'Mukesh Kumar'; ArrayList list = new ArrayList(); list.add('Mohan'); list.add('Mukesh'); list.add('RAVI'); list.add('MuKesH kuMar'); list.add('Suresh'); for (String str : list) { if (str.equalsIgnoreCase(str1)) { System.out.println('Mukesh kumar is present'); } } } } </pre> <p> <strong>Output:</strong> </p> <pre> Mukesh kumar is present </pre> <hr></last)>Preizkusite zdaj
Izhod:
true true false
2. primer metode Java String equalsIgnoreCase().
Oglejmo si primer, kjer preizkušamo enakost nizov med nizi.
Ime datoteke: EqualsIgnoreCaseExample2.java
import java.util.ArrayList; public class EqualsIgnoreCaseExample2 { public static void main(String[] args) { String str1 = 'Mukesh Kumar'; ArrayList list = new ArrayList(); list.add('Mohan'); list.add('Mukesh'); list.add('RAVI'); list.add('MuKesH kuMar'); list.add('Suresh'); for (String str : list) { if (str.equalsIgnoreCase(str1)) { System.out.println('Mukesh kumar is present'); } } } }
Izhod:
Mukesh kumar is present