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

发布于 2024-10-08 19:28:17 字数 859 浏览 8 评论 0原文

我已经大大简化了问题,这里是示例代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

海未深 2024-10-15 19:28:17

您有一个 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

在您的代码中:

joinOutputString = string.Join("\\\x200E", joinOutputString, myStrings[i]);

请注意 \ 是转义的 \ 和 \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:

joinOutputString = string.Join("\\\x200E", joinOutputString, myStrings[i]);

Note the \ is an escaped \ and \x200E is the pseudo-strong LTR mark.

别再吹冷风 2024-10-15 19:28:17

执行以下操作会更容易。

System.IO.Path.Combine(myStrings);

如果您尝试创建路径,则

It would be easier to do the following

System.IO.Path.Combine(myStrings);

if you are attempting to make a path.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文