5

I have an array of string . I want to remove an item from string. HOw i can do this.

string []values = User.Split(';');

Suppose values contains "1","2","3","4"

I want to delete or remove item "2" from values. How i can do this. Is there built in function in C#

4
  • Possible duplicate of this Commented Oct 9, 2015 at 6:15
  • You are right @JenishRabadiya Commented Oct 9, 2015 at 6:17
  • Possible duplicate of.... wait.... just google your title google.com/… Commented Oct 9, 2015 at 6:28
  • @Backs hmm you are right. Thanks for pointing it out but now I won't be able to edit that comment so would delete that comment. Commented Oct 9, 2015 at 6:32

2 Answers 2

12

Array is immutable object. So, you can't remove from array. You can create new array without this value using LINQ:

values = values.Where(o=> o != "2").ToArray();

Or, you can create List and remove from list:

List<string> values = User.Split(';').ToList();
values.Remove("2");
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

myArray = myArray.Where(w => w != myArray[2]).ToArray(); 

1 Comment

you can't be sure, that myArray[2] == "2"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.