Concatenating left and right-aligned character types

最后都变了- 提交于 2019-12-08 14:58:36

问题


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:


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

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.



来源:https://stackoverflow.com/questions/45106874/concatenating-left-and-right-aligned-character-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!