假设多行文本框中的段落包含:
stringrandom@good
stringra12312@good
stringr2a123@bad
strsingra12312@good
strinsgr2a123@bad
我想生成这样的输出:
stringrandom@good
stringra12312@good
strsingra12312@good
我尝试过使用
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
它不起作用,是否有人知道解决方法?
答案 0 :(得分:4)
您可以使用LINQ查询列表。
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
实际上你并不真的需要If语句:
Dim newList As List(Of String) = TextBox1.Lines.ToList
newList = newList.Where(Function(x) Not x.Contains("bad")).ToList
您甚至可以通过将LINQ直接应用于TextBox来实现更简单:
Dim newList As List(Of String) = TextBox1.Lines _
.ToList _
.Where(Function(x) Not x.Contains("bad")) _
.ToList
答案 1 :(得分:0)
您可以尝试使用正则表达式(System.Text.RegularExpressions
)。试试这个:
Dim lines = textBox1.Lines _
.Where(Function(l) Not Regex.Match(l, "\w*@bad").Success) _
.ToList()
答案 2 :(得分:0)
另一种选择是(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
或者在VB中
Dim x = mails.Where(Function(e) e.Contains("good")).ToList()
答案 3 :(得分:0)
你接近开始。改为使用FindIndex。
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