创建从左到右与从右到左部分连接的正确路径时出现问题
我已经大大简化了问题,这里是示例代码:
string outputString = string.Empty;
string joinOutputString = string.Empty;
string pathOutputString = string.Empty;
string[] myStrings = new string[4];
myStrings[0] = "First entry";
myStrings[1] = "اول";
myStrings[2] = "دوم";
myStrings[3] = "Last entry";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < myStrings.Length; i++)
{
joinOutputString = string.Join(@"\", joinOutputString, myStrings[i]);
outputString = string.Format(@"{0}{1}\", outputString, myStrings[i]);
pathOutputString = System.IO.Path.Combine(pathOutputString, myStrings[i]);
sb.Append(string.Format(@"{0}\", myStrings[i]));
}
循环结束时所有字符串和 StringBuilder 的最终值是:
First Entry\Drop\Drom\Last Entry\
而不是
First Entry\Drom\Drop \最后一项\
中间从右到左的部分作为一个单元翻转。
感谢您提前抽出时间。
I have simplified in a big way the problem and here is the sample code:
string outputString = string.Empty;
string joinOutputString = string.Empty;
string pathOutputString = string.Empty;
string[] myStrings = new string[4];
myStrings[0] = "First entry";
myStrings[1] = "اول";
myStrings[2] = "دوم";
myStrings[3] = "Last entry";
StringBuilder sb = new StringBuilder();
for (int i = 0; i < myStrings.Length; i++)
{
joinOutputString = string.Join(@"\", joinOutputString, myStrings[i]);
outputString = string.Format(@"{0}{1}\", outputString, myStrings[i]);
pathOutputString = System.IO.Path.Combine(pathOutputString, myStrings[i]);
sb.Append(string.Format(@"{0}\", myStrings[i]));
}
The final value of all strings and StringBuilder at the end of the loop is:
First entry\اول\دوم\Last entry\
instead of
First entry\دوم\اول\Last entry\
The middle right to left section is flipped as one unit.
Thanks for your time in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有一个 bidi 字符串(同时包含 LTR 和 RTL 字符的字符串),并且 .NET 在输出该字符串时在 LTR 和 RTL 模式之间切换。标点符号被认为是“弱”的,并继续使用当前有效的任何方向。因此,您输出一个 LTR 字符串(“第一个条目”),后跟一串 RTL 字符(来自 myString[1] 的 3 个字符 + “\” + 来自 myString[2] 的 3 个字符),后跟一个 LTR 字符串(“最后一个条目”)。
myString[0](打印的 LTR)然后 myString[1](打印的 RTL)然后 myString[2](打印的 RTL)然后 myString[3](打印的 LTR)
请注意,整个中间字符串(由 myString[1] + " 组成\" + myString[2]) 被打印为 RTL,因此从您的期望中转置。您可以添加伪强LTR标记(Unicode字符0x200E)来强制方向改变。
http://en.wikipedia.org/wiki/Bi-direction_text
在您的代码中:
请注意 \ 是转义的 \ 和 \x200E 是伪强 LTR 标记。
You have a bidi string (a string containing both LTR and RTL characters) and .NET is switching between LTR and RTL modes when outputting the string. Punctuation is considered "weak" and continues using whatever direction is currently active. So you output a LTR string ("First entry") followed by a string of RTL characters (3 from myString[1] + "\" + 3 from myString[2]) followed by a LTR string ("Last entry").
myString[0] (printed LTR) then myString[1] (printed RTL) then myString[2] (printed RTL) then myString[3] (printed LTR)
Note that the entire middle string (composed of myString[1] + "\" + myString[2]) is printed RTL and hence transposed from your expectation. You can add a pseudo-strong LTR mark (Unicode character 0x200E) to force the direction change.
http://en.wikipedia.org/wiki/Bi-directional_text
In your code:
Note the \ is an escaped \ and \x200E is the pseudo-strong LTR mark.
执行以下操作会更容易。
如果您尝试创建路径,则
It would be easier to do the following
if you are attempting to make a path.