Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change ArrayList value

Tags:

c#

I want to remove all '\0' characters from the items of an ArrayList in c#. Here is my code:

ArrayList users = um.getAllUsers(server,Instance); //user count=3

So now I want to add a code in order to replase all '\0' with empty string in all the items of the list.

Is this possible?

like image 505
Davideg Avatar asked Aug 28 '26 11:08

Davideg


2 Answers

First, it's a little odd to still be using ArrayList in 2010... and also to have Camel cased names (getAllUsers rather than GetAllUsers) but still...

for (int i = 0; i < users.Count; i++)
{
    string user = (string) users[i];
    user = user.Replace("\0", "");
    users[i] = user;
}
like image 190
Jon Skeet Avatar answered Aug 31 '26 01:08

Jon Skeet


If you're not stuck with some ancient version of .NET, you might be interested in upgrading your code to use the generic List<T> collection and have a look at LINQ:

List<string> users = um.getAllUsers(server,Instance)
                       .Cast<string>()
                       .Select(user => user.Replace("\0", ""))
                       .ToList();

(I assume getAllUsers returns an ArrayList containing string instances).

like image 35
dtb Avatar answered Aug 31 '26 01:08

dtb