Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

c# - Concat arabic and english string with 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)
            : "");

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)?????? [email protected]) arabic and other symbols mixed, but i need something like "here arabic string" ([email protected]. it would be great if this problem have solve with String.Format. Hope for your help. Thank you

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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)
            : "");,

Demo: .NET Fiddle Example

Similar issues:

incorrect right to left concatenation english and Arabic

Problem creating correct path concatenating left to right with right to left sections


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...