编辑数组元素

发布于 2024-12-15 07:36:23 字数 484 浏览 0 评论 0原文

我有一个驱动器号数组,我需要在每个字母后附加一个冒号,然后将该数组传递给另一个函数。我可以这样做还是需要创建一个新数组?或者也许根本不是数组而是某种列表?

string source = "C|D|E";
string[] sourcearray = source.Split('|');
foreach (string driveletter in sourcearray)
{
    //need to append ":" to each drive letter
}

编辑:有时源数组可能以管道结尾:

string source = "C|D|E|";

当发生这种情况时,如果我使用公共 for 循环,数组中的最后一个元素将是冒号,但我不能这样做。如何最好地处理这个问题?当发生这种情况时,最终的数组需要看起来像:

C: D: E:

谢谢。

I have an array of drive letters and I need to append a colon to each letter and then pass the array to another function. Can I do this or do I need to create a new array? Or maybe not an array at all but some kind of List instead?

string source = "C|D|E";
string[] sourcearray = source.Split('|');
foreach (string driveletter in sourcearray)
{
    //need to append ":" to each drive letter
}

EDIT: There are times when the source array could end in a pipe:

string source = "C|D|E|";

When that happens the last element in the array will be a colon if I use a common for loop, and I can't have this. How best to handle this? When this happens the final array needs to look like:

C: D: E:

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

泛泛之交 2024-12-22 07:36:23

字符串是不可变的,因此您无法更改字符串实例,但必须使用新字符串更改数组槽:

string source = "C|D|E";
string[] sourcearray = source.Split(new []{'|'}, StringSplitOptions.RemoveEmptyEntries);
for(int i=0; i < sourcearray.Length; i++)
{
   sourcearray[i] = sourcearray[i] + ":";
}

Strings are immutable, so you can't change the string instance but you must change the array slots with new strings:

string source = "C|D|E";
string[] sourcearray = source.Split(new []{'|'}, StringSplitOptions.RemoveEmptyEntries);
for(int i=0; i < sourcearray.Length; i++)
{
   sourcearray[i] = sourcearray[i] + ":";
}
剩一世无双 2024-12-22 07:36:23

将 for 循环替换为

 string[] resultArray = sourcearray.Select(s => s + ":").ToArray();

重新编辑:

  string source = "C|D|E|";

这里最好的解决方案是这是 string.Split() 方法的特殊变体。不幸的是,这需要一组分隔符,所以我们得到:

sourceArray = source.Split(new char[] {'|'}, 
          StringSplitOptions.RemoveEmptyEntries);

Replace your for-loop with

 string[] resultArray = sourcearray.Select(s => s + ":").ToArray();

Re the Edit:

  string source = "C|D|E|";

The best solution here is to this is a special variation of the string.Split() method. Unfortunately that one requires an array of separator chars, so we get:

sourceArray = source.Split(new char[] {'|'}, 
          StringSplitOptions.RemoveEmptyEntries);
梦太阳 2024-12-22 07:36:23
for (var i = 0; i < sourcearray.Length; i++)
{
   sourceArray[i] += ":";
}
for (var i = 0; i < sourcearray.Length; i++)
{
   sourceArray[i] += ":";
}
你怎么这么可爱啊 2024-12-22 07:36:23
string[] sourcearray = source.Split('|').Select(s => s + ":").ToArray();
string[] sourcearray = source.Split('|').Select(s => s + ":").ToArray();
有木有妳兜一样 2024-12-22 07:36:23
var newArray = source.Split('|').Select(s => s + ":").ToArray();
var newArray = source.Split('|').Select(s => s + ":").ToArray();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文