使用 string.Format() 连接阿拉伯语和英语字符串 [英] Concat arabic and english string with string.Format()

查看:102
本文介绍了使用 string.Format() 连接阿拉伯语和英语字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在连接两个字符串时遇到一些麻烦.

Have some trouble with concat two string.

return string.Format("{0}{1}{2}",
            IdWithSubType,

            ExtraInfo.Any(info => info.InfoType == UniExtraInfoType.Alias)
            ? string.Format(" ({0})", string.Join(",", ExtraInfo.First(info => info.InfoType == UniExtraInfoType.Alias).Info))
            : "",

            Context != null
            ? string.Format(" ({0})", Context.IdWithSubType)
            : "");

当 IdWithSubType、extrainfo 和 context 具有拉丁语或基里尔符号时可以,但 IdWithSubType 可以是阿拉伯语,并且与它连接是错误的.例如100252575)طائرات هليكوبت@vk.com)阿拉伯语和其他符号混合,但我需要类似这里的阿拉伯语字符串"(100252575@vk.com.如果这个问题能用 String.Format 解决就太好了.希望得到你的帮助.谢谢

it's ok when IdWithSubType, extrainfo and context has latin or kirillic symbols, but IdWithSubType can be arabic, and concat with that is wrong. e.g.100252575)طائرات هليكوبت@vk.com) arabic and other symbols mixed, but i need something like "here arabic string" (100252575@vk.com. it would be great if this problem have solve with String.Format. Hope for your help. Thank you

推荐答案

那里很可能没有出现编码问题,只是 RTL(从右到左)字符串如何作为 LTR(从左到右)字符串的一部分进行排列.

It's likely no encoding issues appear there, just how RTL (right-to-left) string follows arrangement as part of LTR (left-to-right) string.

双向格式中常用的 2 个字符来标记 LTR &RTL 部分,分配为 0x200e(LTR) &0x200f (RTL).在这种情况下,使用 0x200e 标记 RTL 部分的结束(阿拉伯语)和 LTR 部分的开始:

There's 2 characters which commonly used in bidirectional formatting to mark either LTR & RTL part, assigned as 0x200e (LTR) & 0x200f (RTL). In this case, use 0x200e to mark end of RTL part (in Arabic) and starting LTR part:

string leftToRight = ((char)0x200E).ToString();

// using string.Format
return string.Format("{0}{1}{2}{3}",
            IdWithSubType,
            leftToRight,

            ExtraInfo.Any(info => info.InfoType == UniExtraInfoType.Alias)
            ? string.Format(" ({0})", string.Join(",", ExtraInfo.First(info => info.InfoType == UniExtraInfoType.Alias).Info))
            : "",

            Context != null
            ? string.Format(" ({0})", Context.IdWithSubType)
            : "");,

// alternative: using string.Join
return string.Join(leftToRight, IdWithSubType,
            ExtraInfo.Any(info => info.InfoType == UniExtraInfoType.Alias)
            ? string.Format(" ({0})", string.Join(",", ExtraInfo.First(info => info.InfoType == UniExtraInfoType.Alias).Info))
            : "",

            Context != null
            ? string.Format(" ({0})", Context.IdWithSubType)
            : "");,

演示:.NET Fiddle 示例

类似问题:

不正确的从右到左串联英语和阿拉伯语

创建正确的问题路径从左到右与从右到左部分连接

这篇关于使用 string.Format() 连接阿拉伯语和英语字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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