如何检测一个字符是否属于从右到左的语言?
判断字符串是否包含从右到左语言的文本的好方法是什么。
我发现了这个问题 建议采用以下方法:
public bool IsArabic(string strCompare)
{
char[] chars = strCompare.ToCharArray();
foreach (char ch in chars)
if (ch >= '\u0627' && ch <= '\u0649') return true;
return false;
}
虽然这可能适用于阿拉伯语,但这似乎不适用于其他 RTL 语言,例如希伯来语。是否有通用方法可以知道某个特定字符属于 RTL 语言?
What is a good way to tell whether a string contains text in a Right To Left language.
I have found this question which suggests the following approach:
public bool IsArabic(string strCompare)
{
char[] chars = strCompare.ToCharArray();
foreach (char ch in chars)
if (ch >= '\u0627' && ch <= '\u0649') return true;
return false;
}
While this may work for Arabic this doesn't seem to cover other RTL languages such as Hebrew. Is there a generic way to know that a particular character belongs to a RTL language?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Unicode 字符具有不同的相关属性。这些属性不能从代码点导出;您需要一个表格来告诉您某个角色是否具有某种属性。
您对具有双向属性“R”或“AL”(RandALCat) 的字符感兴趣。
以下是 Unicode 3.2 的完整列表(来自 RFC 3454):
以下是一些获取代码Unicode 6.0 的完整列表:
请注意,这些值是 Unicode 代码点。 C#/.NET 中的字符串采用 UTF-16 编码,需要首先转换为 Unicode 代码点(请参阅 Char.ConvertToUtf32)。下面是一种检查字符串是否至少包含一个 RandALCat 字符的方法:
Unicode characters have different properties associated with them. These properties cannot be derived from the code point; you need a table that tells you if a character has a certain property or not.
You are interested in characters with bidirectional property "R" or "AL" (RandALCat).
Here's the complete list as of Unicode 3.2 (from RFC 3454):
Here's some code to get the complete list as of Unicode 6.0:
Note that these values are Unicode code points. Strings in C#/.NET are UTF-16 encoded and need to be converted to Unicode code points first (see Char.ConvertToUtf32). Here's a method that checks if a string contains at least one RandALCat character:
您可以尝试在命名块中使用“命名块”。 Regular-Expressions.info/unicode.html">正则表达式。只需挑选从右到左的块,并形成正则表达式即可。例如:
如果该正则表达式返回 true,则字符串中至少有一个希伯来语或阿拉伯语字符。
You can try using "named blocks" in regular expressions. Just pick out the blocks that are right to left, and form the regex. For example:
If that regex returns true, then there was at least one hebrew or arabic character in the string.
Unicode 6.0 的所有“AL”或“R”(来自 http://www.unicode.org /Public/6.0.0/ucd/UnicodeData.txt)
All "AL" or "R" of Unicode 6.0 (from http://www.unicode.org/Public/6.0.0/ucd/UnicodeData.txt)
编辑:
这是我现在使用的,它包括元音字符以及希伯来语和阿拉伯语中的所有内容:
旧答案:
如果您需要检测句子中的 RTL 语言,这简化的正则表达式可能就足够了:
如果想用希伯来语写一些东西,则必须使用这些字符之一,情况与阿拉伯语类似。
它不包括元音字符,因此如果您需要捕获所有整个单词或绝对所有 RTL 字符,您最好使用其他答案之一。
希伯来语中的元音字符在非诗歌文本中非常罕见。
我不知道阿拉伯语文本。
EDIT:
This is what I use now, it includes the Vowelization chars and everything in Hebrew and Arabic:
OLD ANSWER:
If you need to detect RTL language in a sentence, this simplified RegEx will probably be enough:
If one wants to write something in Hebrew it will have to use one of these characters, and the case is similar with Arabic.
It does not include vowelization characters, so if you need to catch all whole words or absolutely all RTL chars you better use one of the other answers.
Vowelization chars in Hebrew are very rare in non-poetry texts.
I don't know about Arabic texts.
在我的正则表达式实现中,我既不能使用 \u、\x 也不能使用 {} 语言命名组。
因此,我根据 UnicodeData.txt。
这应该是相当全面的,到目前为止我已经在阿拉伯语和希伯来语文本上对其进行了测试。
On my implementation of regex I could not use neither \u, \x, nor {} language named groups.
So I built my own pattern programatically based on all "R" and "AL" (RandALCat) bidirectional characters as listed in UnicodeData.txt.
This should be decently comprehensive and I've tested it on Arabic and Hebrew text so far.