包含阿拉伯和西方字符的字符串连接 [英] String concatenation containing Arabic and Western characters

查看:32
本文介绍了包含阿拉伯和西方字符的字符串连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试连接几个包含阿拉伯语和西方字符(混合在同一个字符串中)的字符串.问题是结果是一个字符串,它很可能在语义上是正确的,但与我想要获得的不同,因为字符的顺序被 Unicode 双向算法改变了.基本上,我只想将它们串联起来,就好像它们都是 LTR 一样,而忽略了一些是 RTL 的事实,这是一种不可知"串联.

I'm trying to concatenate several strings containing both arabic and western characters (mixed in the same string). The problem is that the result is a String that is, most likely, semantically correct, but different from what I want to obtain, because the order of the characters is altered by the Unicode Bidirectional Algorithm. Basically, I just want to concatenate as if they were all LTR, ignoring the fact that some are RTL, a sort of "agnostic" concatenation.

我不确定我的解释是否清楚,但我认为我不能做得更好.

I'm not sure if I was clear in my explanation, but I don't think I can do it any better.

希望有人能帮助我.

亲切的问候,

卡洛斯·费雷拉

顺便说一句,字符串是从数据库中获取的.

BTW, the strings are being obtained from the database.

编辑

前两个字符串是我想要连接的字符串,第三个是结果.

The first 2 Strings are the strings I want to concatenate and the third is the result.

编辑 2

实际上,连接后的字符串与图片中的有点不同,它在复制+粘贴过程中发生了变化,1 在第一个 A 之后,而不是在第二个 A 之前.

Actually, the concatenated String is a little different from the one in the image, it got altered during the copy+paste, the 1 is after the first A and not immediately before the second A.

推荐答案

您可以使用 unicode 格式控制代码点嵌入 bidi 区域:

You can embed bidi regions using unicode format control codepoints:

  • 从左到右嵌入 (U+202A)
  • 从右到左嵌入 (U+202B)
  • 流行方向格式 (U+202C)

因此,在 Java 中,要将像阿拉伯语这样的 RTL 语言嵌入到像英语这样的 LTR 语言中,您会这样做

So in java, to embed a RTL language like Arabic in an LTR language like English, you would do

myEnglishString + "\u202B" + myArabicString + "\u202C" + moreEnglish

反过来做

myArabicString + "\u202A" + myEnglishString + "\u202C" + moreArabic

有关详细信息,请参阅双向通用格式,或有关方向格式代码"的 Unicode 规范章节,用于源材料.

See Bidirectional General Formatting for more details, or the Unicode specification chapter on "Directional Formatting Codes" for the source material.

这篇关于包含阿拉伯和西方字符的字符串连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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