You need to sign in to do that
Don't have an account?
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?
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:
All Answers
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:
Thanks for your response Mike,
Could you please tell me how can i implement the same for my example?
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 . .
Worked fine for me wen I check like this
Can you share your complete code.
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.
Hi,
How to ignore the case while comparing two strings. If the two strings are in different cases that is small and caps.
Thanks
Nagalakshmi,
try this
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