0

I am using .equals for String comparison below, but x does not match "OU":

String memberOfValue="CN=cn,​OU=ou,​OU=roles,​OU=de,​OU=apps,​DC=meta,​DC=cc,​DC=com";
String[] pairs = memberOfValue.split(",");
for (int i=0;i<pairs.length;i++) {
    String pair = pairs[i];
    String[] keyValue = pair.split("=");
    System.out.println(keyValue[0]);
    String x = keyValue[0];
    String y = "OU";
    System.out.println(x);
    System.out.println(x.equals(y));
}

Where am I going wrong?

Kevin J. Chase
  • 3,856
  • 4
  • 21
  • 43
rinuthomaz
  • 1,393
  • 2
  • 23
  • 38
  • 7
    Define "not working". What behavior are you expecting? What are you seeing? – Hovercraft Full Of Eels Apr 04 '17 at 22:20
  • An unrelated note: That `for` loop could be replaced with a simpler "foreach" loop: `for (String pair : pairs) {...}`. – Kevin J. Chase Apr 04 '17 at 23:07
  • If you're trying to parse [LDAP](https://tools.ietf.org/html/rfc4511) messages, you're going to need more than `String.split`. Unless this is a one-off assignment or very minor side project, you should probably look for a Free library that already handles the weird corner cases for you (Apache offers one). For example, was your input string invalid, or was it allowed to contain characters like that Unicode zero-width space? I don't know, but it's defined somewhere, and (probably) handled by a pre-existing library. I'm sure there are more surprises like that lying in wait, too. – Kevin J. Chase Apr 05 '17 at 00:00

3 Answers3

7

Adding these two lines of code shows the problem:

System.out.println("x: " + x + " - " + x.chars().boxed().collect(Collectors.toList()));
System.out.println("y: " + y + " - " + y.chars().boxed().collect(Collectors.toList()));

It gives

x: ​OU - [8203, 79, 85]
y: OU - [79, 85]

Which shows that you have some invisible char whose integer value is 8203 (zero width space, see What's HTML character code 8203?) in your string. Not sure how you got that.

Community
  • 1
  • 1
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Gotta hate those dumb text editors that insert invisible characters into your text. The solution is however very easy: **Use an IDE**. They are made for programming, and won't do crap like this. – Andreas Apr 04 '17 at 22:40
1

As @JB Nizet says, you have non-printable characters in your memberOfValue variable, there are some types of characters as for example:

control, format, private use, surrogate, unassigned, etc...

Here is the complete list: http://www.fileformat.info/info/unicode/category/index.htm

In these cases, you can remove all characters from your string using this regular expression: \\P{Print}

For example:

String x = keyValue[0].replaceAll("[\\P{Print}", "");

When you compare the strings again, the result will be correct.

JUAN CALVOPINA M
  • 3,695
  • 2
  • 21
  • 37
0

There are two possible problems from what I'm seeing.

A.) If the strings are capitalized differently they will not return equal unless you use the method .equalsIgnoreCase() instead of .equals()

B.) You're not getting the right strings that you're expecting. Be sure to print out or debug which string is getting parsed through.

Raheel138
  • 147
  • 10