Java字符串中的不可见字符 [英] Invisible characters in Java Strings

查看:3307
本文介绍了Java字符串中的不可见字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

String a = "Hello\u200e";
String b = "Hello\u200f";

System.out.println("a = '" + a + "' and b = '" + b + "' are length "
                     + a.length() + " and " + b.length()
                     + ", equals() is " + a.equals(b));

上面代码段中的代码产生以下输出。

The code in the above code snippet produces the following output.


a ='Hello'和b ='Hello'的长度为6和6,equals()为false

a = 'Hello‎' and b = 'Hello‏' are length 6 and 6, equals() is false

虽然控制台上显示的 a b 的值均为 Hello a.equals(b)返回 false 。怎么样?

Although the value of both a and b displayed on the console is Hello‏, a.equals(b) returns false. How?

推荐答案

U + 200E U + 200F 可打印字符。它们都是控制字符,用于指示文本应该如何呈现 - 从左到右,或从右到左。

U+200E and U+200F are not printable characters. They're both control characters which dictate how the text should be rendered - either left to right, or right to left.

你不会在终端中看到这些,它们不应该是等价的字符串。

You won't see these in the terminal, and they shouldn't be equivalent strings.

0x200E ^ 0x200F!= 0

这篇关于Java字符串中的不可见字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆