user2625447
user2625447

Reputation: 9

how to a specific delete line in textbox

Suppose that a paragraph in a multiline textbox contained:

stringrandom@good
stringra12312@good
stringr2a123@bad
strsingra12312@good
strinsgr2a123@bad

I want to produce output like this:

stringrandom@good
stringra12312@good
strsingra12312@good

I have tried using

  If TextBox1.Lines.Count > 2 Then
      Dim newList As List(Of String) = TextBox1.Lines.ToList
      If newList.Contains("@bad") Then
          newList.RemoveAt(newList.Item(newList.Contains("@bad")))
      End If

      TextBox1.Lines = newList.ToArray
  End If

It does not work, does anyone know a workaround?

Upvotes: 0

Views: 329

Answers (4)

tinstaafl
tinstaafl

Reputation: 6948

Your were close to begin with. Use FindIndex instead.

  If TextBox1.Lines.Count > 2 Then
      Dim newList As List(Of String) = TextBox1.Lines.ToList
      If newList.Contains("@bad") Then
          newList.RemoveAt(newList.FindIndex(Function(x) x.Contains("@bad")))
      End If

      TextBox1.Lines = newList.ToArray
  End If

Upvotes: 0

Alex Filipovici
Alex Filipovici

Reputation: 32561

You may try to use the regular expressions (System.Text.RegularExpressions). Try this:

Dim lines = textBox1.Lines _
    .Where(Function(l) Not Regex.Match(l, "\w*@bad").Success) _
    .ToList()

Upvotes: 0

Hexie
Hexie

Reputation: 4221

Another alternative would be (C#);

const string mail = "stringrandom@good";
const string mail1 = "stringra12312@good";
const string mail2 = "stringr2a123@bad";
const string mail3 = "strsingra12312@good";
const string mail4 = "strinsgr2a123@bad";

var mails = new string[] { mail, mail1, mail2, mail3, mail4 }; //List of addresses

var x = mails.Where(e => e.Contains("good")).ToList(); //Fetch where the list contains good

Or in VB

Dim x = mails.Where(Function(e) e.Contains("good")).ToList()

Upvotes: 0

Giannis Paraskevopoulos
Giannis Paraskevopoulos

Reputation: 18411

You may use LINQ to query the list.

If TextBox1.Lines.Count > 2 Then
    Dim newList As List(Of String) = TextBox1.Lines.ToList
    newList = newList.Where(Function(x) Not x.Contains("bad")).ToList
End If

In fact you don't really need the If statement:

Dim newList As List(Of String) = TextBox1.Lines.ToList
newList = newList.Where(Function(x) Not x.Contains("bad")).ToList

And You can even do it simpler by applying the LINQ directly to the TextBox:

Dim newList As List(Of String) = TextBox1.Lines _
                                 .ToList _
                                 .Where(Function(x) Not x.Contains("bad")) _
                                 .ToList

Upvotes: 4

Related Questions