function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Madhura BMadhura B 

Issue with EqualsIgnoreCase

Hi,

 

I am facing an issue while comparing two strings.

Both the strings are same but then on comparion the value returned is False.

There is a line break in both the strings and i suspect that is the reason for the failure.

Here's my code snippet

 

sg.commonField='Percentage of EAL students above National Average 49% of EAL students underachieving in English and 48% in Maths'

 

is.Issue__c= 'Percentage of EAL students above National Average 49% of EAL students underachieving in English and 48% in Maths'

 

if(sg.commonField.equalsignorecase(is.Issue__c))
          {
                      System.debug('inside common** '+is.id);
                         sg.subg.Issues_Smart_Targets__c=is.id;
          } 

 

The if condition returns False.

 

**sg.commonField and is.Issue__c are values from my wrapper class.

 

If the reason for this is line break as i suspected then how do I go about it and resolve the issue?

 

Best Answer chosen by Admin (Salesforce Developers) 
mulvelingmulveling

You're probably right to suspect the line breaks -- equalsIgnore case is a character-level comparison, and line-breaks are not always represented by the same character sequence. Windows systems traditionally used the '\r\n' sequence for a line-breaks, while UNIX simply uses '\n' -- such a difference will definitely cause string comparisons to fail.

 

Testing equality on a full sentence can be tricky, depending on what exactly you want to do, and how flexible it needs to be. For example, you might try tokenizing the string into words and then comparing the sets of words for equality and order. A simple way to achieve that would be via a Regex/Pattern to first normalize the whitespace chars, and then using the usual equalsIgnoreCase:

 

// returns true if the strings are equal after normalizing case and whitespace:
public static Boolean equalsNormCaseAndWhitespace(String val1, String val2) {
    // before invoking equalsIgnoreCase, replace any contiguous run of whitespace 
    // chars with a single space-char; also trim the start/end of whitespace:
    return val1 == null || val2 == null ? val1 == val2 : 
            val1.replaceAll('\\s+', ' ').trim().equalsIgnoreCase(val2.replaceAll('\\s+', ' ').trim());
}

// true:
System.debug(equalsNormCaseAndWhitespace('foo \n bar', 'FOO \r\n \tBar '));
// still false:
System.debug(equalsNormCaseAndWhitespace('foo bar', 'foo baz'));

 

 

All Answers

mulvelingmulveling

You're probably right to suspect the line breaks -- equalsIgnore case is a character-level comparison, and line-breaks are not always represented by the same character sequence. Windows systems traditionally used the '\r\n' sequence for a line-breaks, while UNIX simply uses '\n' -- such a difference will definitely cause string comparisons to fail.

 

Testing equality on a full sentence can be tricky, depending on what exactly you want to do, and how flexible it needs to be. For example, you might try tokenizing the string into words and then comparing the sets of words for equality and order. A simple way to achieve that would be via a Regex/Pattern to first normalize the whitespace chars, and then using the usual equalsIgnoreCase:

 

// returns true if the strings are equal after normalizing case and whitespace:
public static Boolean equalsNormCaseAndWhitespace(String val1, String val2) {
    // before invoking equalsIgnoreCase, replace any contiguous run of whitespace 
    // chars with a single space-char; also trim the start/end of whitespace:
    return val1 == null || val2 == null ? val1 == val2 : 
            val1.replaceAll('\\s+', ' ').trim().equalsIgnoreCase(val2.replaceAll('\\s+', ' ').trim());
}

// true:
System.debug(equalsNormCaseAndWhitespace('foo \n bar', 'FOO \r\n \tBar '));
// still false:
System.debug(equalsNormCaseAndWhitespace('foo bar', 'foo baz'));

 

 

This was selected as the best answer
Madhura BMadhura B

Thanks for your response Mike,

Could you please tell me how can i implement the same for my example?

 

amidstcloudamidstcloud

Hi ,

 

I tried the same thing with the below code ...

 

string a;
string b;
a='Percentage of EAL students above National Average 49% of EAL students underachieving in English and 48% in Maths';
b= 'Percentage of EAL students above National Average 49% of EAL students underachieving in English and 48% in Maths';
system.debug(a.equalsIgnoreCase(b));

 

and I  got true for this . Please excute once at ur end . .

Shashikant SharmaShashikant Sharma

Worked fine for me wen I check like this

String s1 ='Percentage of EAL students above National Average 49% of EAL students underachieving in English and 48% in Maths';
 
String s2= 'Percentage of EAL students above National Average 49% of EAL students underachieving in English and 48% in Maths';
 
if(s1.equalsignorecase(s2))
          {
              System.debug('inside common** ');
          } 

 Can you share your complete code.

Madhura BMadhura B

just gave an example for the 2 strings. there's a line break in the sentence so i am facing an issue. the string example i have given here is copied from the debug log. that's how it looks in the debug log. but in real there's a line break.

nagalakshminagalakshmi

Hi,

 

How to ignore the case while comparing two strings. If the two strings are in different cases that is small and caps.

 

 

Thanks

Madhura BMadhura B

Nagalakshmi,

 

try this

String s1 ='ABC'; String s2= 'abc'; if(s1.equalsignorecase(s2)) { System.debug('inside if loop** '); } 
nagalakshminagalakshmi

Hi Madhura,

 

I have set of strings, i need to compare with set of strings. leftselected value is having first letter as capital. And leftvalues1 having normal values that is either cap or small as first letter. Now my requirement is remove that values from leftvalues1. Please help me.

 

for(String s : leftselected1)
{
leftvalues1.remove(s);
rightvalues1.add(s);

}

 

Thanks,

Lakshmi