连接左对齐和右对齐的字符类型

时间:2021-10-11 12:36:51

It seems that a combinations of character types can produce unexpected results for the resulting order of an explicit paste operation:

似乎字符类型的组合可以为显式粘贴操作的结果顺序产生意想不到的结果:

(x = paste(c('green','أحمر', 'أزرق'), collapse=' ')) # arabic for blue and red
#> [1] "green أحمر أزرق"
paste(x, 'yellow')
#> [1] "green أحمر أزرق yellow"
paste(x, 123)
#> [1] "green أحمر أزرق 123"

Is there any known solution to this - i.e. a way to ensure concatenation in the same sequence as the arguments are given? Perhaps the answer is don't concatenate different alphabets!

是否有已知的解决方法—即确保在给定参数的相同序列中进行连接的方法?也许答案是不要连接不同的字母!

1 个解决方案

#1


3  

You may use the Unicode control characters 'left-to-right embedding', u202A ("Treat the following text as embedded left-to-right"):

您可以使用Unicode控制字符'从左到右嵌入',u202A(“将以下文本视为从左到右嵌入的文本”):

paste(x, "\u202A", 123)
# [1] "green أحمر أزرق ‭ 123"

See also Terminating Explicit Directional Embeddings and Overrides, (u202C), a thorough description on UNICODE BIDIRECTIONAL ALGORITHM, and here.

另请参见终止显式定向嵌入和重写(u202C)、关于UNICODE双向算法的详细描述以及这里。

#1


3  

You may use the Unicode control characters 'left-to-right embedding', u202A ("Treat the following text as embedded left-to-right"):

您可以使用Unicode控制字符'从左到右嵌入',u202A(“将以下文本视为从左到右嵌入的文本”):

paste(x, "\u202A", 123)
# [1] "green أحمر أزرق ‭ 123"

See also Terminating Explicit Directional Embeddings and Overrides, (u202C), a thorough description on UNICODE BIDIRECTIONAL ALGORITHM, and here.

另请参见终止显式定向嵌入和重写(u202C)、关于UNICODE双向算法的详细描述以及这里。