0

I isolated my original problem in this class. It is returning 2 2. The first time I executed this code I got 1 1, I'm going crazy, I don't understand this behavior

public class Test {

    public static void main(String[] args) {
        test("‎A");
    }


    public static void test(String cadena) {
        System.out.println(cadena.length());
        System.out.println("‎A".length());
    }

}
Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
Víctor
  • 416
  • 5
  • 15

5 Answers5

2

Your "‎A" is actually a sequence of two code points, an unprintable one followed by a capital A letter.

"‎A".codePoints().forEach(System.out::println);

Will print:

8206
65

8206 is the Unicode Character 'LEFT-TO-RIGHT MARK' (U+200E) character.

Karol Dowbecki
  • 43,645
  • 9
  • 78
  • 111
  • Thank you all. Do you know how remove all characters but the letter. I have tried this without success : replaceAll("\\s", ""); – Víctor Jul 04 '18 at 14:46
  • If you need to cleanup input data use a whitelist approach, rather than excluding. It's safer. See [this answer](https://stackoverflow.com/a/49516025/1602555). – Karol Dowbecki Jul 04 '18 at 14:48
1

You have invisible character in the A. Both are not the same

   System.out.println(cadena.hashCode());
   System.out.println("‎A".hashCode());
   System.out.println("‎A".equals(cadena));

Output:

65
254451
false
Thiru
  • 2,541
  • 4
  • 25
  • 39
0

This would always print:

1
1

in normal circumstances.

In your case you seem to have some garbage/non printable character before A in "A".

I suggest you to delete and re-write the phrase "A".

Shanu Gupta
  • 3,699
  • 2
  • 19
  • 29
0

Ok from previous answer there is an invisble characrter

"‎A".chars() .forEach(i -> System.out.println("there is a char:" + (char)i));

Will Print:

there is a char:‎
there is a char:A

I think someone wants to make a Joke to you with this invisible character.

--Edit--

In Java for removing A control character for your String you can use Regex:

public static void test(String cadena) {
    System.out.println(cadena.length());
   "‎A".chars() .forEach(i -> System.out.println("there is a char:" + (char)i));
   String b ="‎A".replaceAll("\\p{C}","");
   System.out.println(b.length());
    b.chars() .forEach(i -> System.out.println("there is a char:" + (char)i));
}

OutPut:

2
there is a char:‎
there is a char:A
1
there is a char:A
Gatusko
  • 2,503
  • 1
  • 17
  • 25
0

I tried to compile your code online here since I don't have java in my pc, and I see that you have this character (left-to-right mark)

You can just remove the string and retype it. :)

triForce420
  • 719
  • 12
  • 31