字符串数组中的 NULL

发布于 2024-08-21 22:06:24 字数 185 浏览 10 评论 0原文

如何删除字符串数组中的空值,

例如 { ,-2,3, ,-4,+5, ,66...}

我需要删除中间的那些空值并重新调整数组的大小

  1. 我不'不想使用列表

  2. 我不想创建新数组

如果需要,请告诉我用简单的代码就可以实现。 谢谢。

How to remove Null values in string array

Like { ,-2,3, ,-4,+5, ,66...}

I need to remove those null values in between and re-size the array

  1. I don't want to use lists

  2. I don't want to create a new array

Please let me know if it is possible with simple code.
Thank You.

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

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

发布评论

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

评论(7

迷迭香的记忆 2024-08-28 22:06:24

不,如果不创建新数组,这是不可能的。您无法调整数组的大小。

您可以轻松创建一个没有空字符串和空引用的新数组,如下所示:

string[] items = new string[] { "", "-2", "3", null, "-4", "+5", null, "66" };

items = items.Where(s => !String.IsNullOrEmpty(s)).ToArray();

No, it's not possible without creating a new array. You can't resize an array.

You can easily create a new array without empty strings and null references like this:

string[] items = new string[] { "", "-2", "3", null, "-4", "+5", null, "66" };

items = items.Where(s => !String.IsNullOrEmpty(s)).ToArray();
灼疼热情 2024-08-28 22:06:24

如果您不想创建新数组,那么不,这是不可能的。您无法从简单数组中添加删除项目(如string[])。

完成您想要实现的目标的最直接方法(如果删除第二个要求)将是:

  1. 计算源数组中空值的数量
  2. 创建一个与源数组长度相同的新数组减去其中的空值数量步骤 1
  3. 将源数组中的所有非空值复制到新数组中
  4. (可选)将对源数组的引用(例如 srcArray)设置为新数组

If you don't want to create a new array, then no, it's not possible. You cannot add or remove an item from a simple array (as in, string[]).

The most straightforward way to accomplish what you want to achieve (if you remove your second requirement) would be:

  1. Count the number of null values in your source array
  2. Create a new array of the same length as your source array minus the number of nulls from step 1
  3. Copy all non-null values from your source array into the new array
  4. (Optional) Set the reference to your source array (e.g., srcArray) to your new array
只想待在家 2024-08-28 22:06:24

正如 Dan 所说,您无法在Array 中添加或删除值。但是,您可以使用 LINQ 删除这些值并生成第二个数组。

originalArray = originalArray.Where(s => !string.IsNullOrEmpty(s)).ToArray()

As Dan said, you can't add or remove values from an Array. You can, however, use LINQ to remove the values and produce a second array.

originalArray = originalArray.Where(s => !string.IsNullOrEmpty(s)).ToArray()
赠意 2024-08-28 22:06:24

可能不是性能最好的解决方案,但是......

array.Where(s => s != null).ToArray();

它将创建一个新数组,但我想不出一个不会的解决方案。

Probably not the most performant solution but...

array.Where(s => s != null).ToArray();

It will create a new array, but I cannot think of a solution that won't.

罪#恶を代价 2024-08-28 22:06:24

在决定如何继续之前,您确实需要考虑谁拥有对您正在操作的数组的引用。

如果该数组没有被任何其他引用代码(作为类的成员,作为 lambda 中捕获的变量,或在某个集合中)那么您不必担心创建新数组。在这种情况下,我会使用类似于 @Codesleuth@Guffa建议。

但是,如果可能存在其他代码来保存对同一数组的引用,那么您就不走运了,除非您可以安全地识别和更新在其他位置保存的引用。这是一件很难做到的事情 - 并且您应该非常小心,假设您始终可以更新保存引用的所有其他位置。

Before deciding how to proceed, you really need to think about who holds a reference to the array you are operating on.

If the array is not referenced by any other code (as a member of a class, as a captured variable in a lambda, or in some collection somewhere) then you shouldn't worry about creating a new array. In that case I would use something like what @Codesleuth or @Guffa suggest.

However, if other code may exist that holds a reference to this same array - then you are out of luck, unless you can safely identify and update the references held in those other places. This is a hard thing to do - and you should be very careful assuming that you can always update all other places where a reference is held.

静谧 2024-08-28 22:06:24

我是这里唯一一个会扫描数组并将成员移回 NULL 的人吗,从而制作一个连续的非空列表。

这不会创建新的数组,并且实现起来很简单,并且重要的是要知道您可以在数组中移动条目。

不幸的是,我正在工作,所以无法提供完整的代码,但是您可以通过在数组中搜索 NULL,然后将数组中的剩余项目向上移动一项来实现它。继续这样做直到最后。我建议在搜索完成后清除剩余的全部。

Am I the only one here that would scan the array and move the members back over the NULLs, therefore making a continuous list of non-nulls.

This doesn't create a new array and it's simple to implement and it's immportant to know you can move the entries around the array.

Unfortunately I'm at work so can not supply full code, however you would implement it by searching the array for NULLs then moving the remaining items in the array up one. Keep doing this until the end. I would suggest clearing the remaining entires once the search is completed.

寻找我们的幸福 2024-08-28 22:06:24
string[] _array= new string[] { "", "z", "d", null, "a", "b", null, "66" };

// select non-null elements only
_array= _array.Where(a => !String.IsNullOrEmpty(a)).ToArray();
string[] _array= new string[] { "", "z", "d", null, "a", "b", null, "66" };

// select non-null elements only
_array= _array.Where(a => !String.IsNullOrEmpty(a)).ToArray();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文