logo

Java String е равно на IgnoreCase()

Java Клас низ равно на IgnoreCase() методът сравнява двата дадени низа въз основа на съдържанието на низа, независимо от регистъра (малък и горен) на низа. Той е точно като метода equals(), но не проверява чувствителността към главни и малки букви. Ако някой символ не съответства, той връща false, иначе връща true.

Подпис

 publicboolean equalsIgnoreCase(String str) 

Параметър

ул : друг низ, т.е. в сравнение с този низ.

java bean

Се завръща

Връща се вярно ако символите на двата низа са равни, в противен случай се пренебрегва регистърът на буквите невярно .

Вътрешно изпълнение

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

От разглеждането на имплементацията е очевидно, че методът equalsIgnoreCase() извиква метода regionMatches(). Това прави метода equalsIgnoreCase() нечувствителен към главни и малки букви. Сигнатурата на метода regionMatches() е спомената по-долу.

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

Методът regionMatches() анализира пет параметъра. Първият параметър ignoreCase е зададено на true в горната реализация. По този начин, когато методът се изпълни, той проверява дали ignoreCase флагът е верен или не. Ако да, тогава се взема по един знак от двата низа и след това се сравнява. Ако сравнението даде невярна стойност, тогава и двата знака се преобразуват в главни букви и след това се проверява дали сравнението все още дава невярна стойност, тогава и двата знака се преобразуват в малки букви и след това се сравняват. Ако сравнението даде истинската стойност, тогава и двата низа имат еднакво съдържание; иначе не. Кодовият фрагмент на обсъжданото сравнение е споменат по-долу.

празнота 0
 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)>
Тествайте сега

Изход:

 true true false 

Java String equalsIgnoreCase() Пример за метод 2

Нека да видим пример, в който тестваме равенството между низовете.

Име на файл: 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;); } } } } 

Изход:

 Mukesh kumar is present