Remove element from comma separeted string and return new a comma separated string
I am trying to remove a element from a comma separated string.
Input : "monkey,bear,cobra";
Output : "monkey, cobra";
if i do this step by step, it works
string AnimalArray = AnimalString.Split(','); //split
List<string> AnimalList = AnimalArray.ToList();//list
Console.WriteLine(String.Join(",", AnimalList.ToArray())); //recreate the comma-seperated string
if I do this all on one line, I get this error
'bool' does not contain a definition for 'ToArray'
string AnimalString2 = "monkey,bear,cobra";
Where is the bool in this action ?
What is the best way to do this ?
The problem is in the ".ToArray()" call after the "Remove("bear")" call. The List class indeed has a remove method, which removes the element from the list. However, that remove method does not return a reference to the list itself; it returns a boolean value based on whether or not it succeeds in finding the specified value.
You can see this if you call:
In other words, you can't cotinue to chain method-calls after the .Remove() call, since it does not return a reference to the underlying List object.
Unfortunately, the call to Remove() has to be done on a separate line. The only way to get around this would be to write your own Remove function, which returns a reference to the List.
This is why the error message is telling you "'bool' does not contain a definition for 'ToArray'". It's interpreting the code as:
Which makes no sense to the compiler.
Daniel Adam Can you explain yourself more ?
This is not a dataset or a database.
It is a string in a protocol and the result should be a string a well. Why should i use dataset in such a small method ?