C# remove elements in list starting with # [duplicate] - c#

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to remove elements from an array
I have an List with a bunch of data in it. Some of the lines start with a #. I want to remove those lines.
How...?

assuming its a string List
myList.RemoveAll(x => x.BeginsWith("#"));

Related

How do I delete a specific list of lines from a text document? [duplicate]

This question already has answers here:
How can I efficiently remove elements by index from a very large list?
(6 answers)
Remove oldest n Items from List using C#
(6 answers)
Remove all but the first item in a list
(2 answers)
How to remove selected array values in C#
(5 answers)
Move First 10 List Items to Another Item List
(5 answers)
Closed 1 year ago.
This is what I have but there isn't anything that talks about how to delete multiple specific lines, for example, from line 2 - 8.
var file = new List<string>(System.IO.File.ReadAllLines(lsbMatches.SelectedItem.ToString()));//location
file.RemoveAt(2);// i want to change this to multiple lines
File.WriteAllLines(lsbMatches.SelectedItem.ToString(), file.ToArray());//location
You can use RemoveRange because you have converted your text file into a list of strings.
https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1.removerange?view=net-5.0
You're best option is to do :
file.RemoveRange(2, 6);

How can I find the smallest word in an array for a form application? [duplicate]

This question already has answers here:
C# finding the shortest and longest word in a array
(9 answers)
Closed 5 years ago.
I am making a program that takes in lines of user input and finds the largest word in that list. I have everything set up but that last bit and am not sure where to even start. I think I need to make a function and then call it to display it in a text box.
If you have an array, for ex: string[] initArray={"one","twoo","threeeee"};
then
public string ReturnSmalles(string[] initArray)
{
return initArray.OrderBy(n => n.Length).FirstOrDefault();
}

Transfer content of one array to another, removing duplicates [duplicate]

This question already has answers here:
How do I remove duplicates from a C# array?
(28 answers)
Most efficient way to remove duplicates from a List
(1 answer)
Closed 6 years ago.
I have an array of about 1000 words. I want to transfer them to another array while removing any duplicates. How can I do this in C# ?
The question almost is the answer:
var array2 = array1.Distinct().ToArray();

.NET list access index [duplicate]

This question already has answers here:
Is the order of elements on a C# List<T> deterministic?
(3 answers)
Closed 8 years ago.
Follow up question from How to select List<> by its index and return the content?
Can someone please comment if the elements in the list are expected to retain order? In the example above, is "Belly Buster" always expected to be at index 1 or are there cases where that might not be true?
List<string> pizzas = new List<string>();
pizzas.Add("Angus Steakhouse");
pizzas.Add("Belly Buster");
pizzas.Add("Pizza Bianca");
pizzas.Add("Classic Cheese");
pizzas.Add("Friday Special");
string result = pizzas[1]; // result is equal to "Belly Buster"
index starts at 0 so yes if the code stays the same..belly buster will always be at 1

C# Linq Query to get between items [duplicate]

This question already has answers here:
Get previous and next item in a IEnumerable using LINQ
(11 answers)
Closed 9 years ago.
Hi I have a dictionary like this
("ABC","X")
("CDE","C")
("EFG","X")
I need to retrieve the items which are in two sides of "C" , How to achieve this with linq ?
Output Expected :
Item = n / output = (n-1)(n+1) items
("ABC","X") and ("EFG","X")
Something like this?
Dict.TakeWhile(e => e.Value = "C").Take(1).Reverse().Take(3).
Then remove the middle one.

Categories

Resources