c.a.s c.a.s - 9 days ago 5
C# Question

How to get a specific value in C# ListBox

Hello i wanna get all the values after the last square bracket ']' so lets say the following values are in my ListBox

[Database]ColumnName
[Firefox]Browser
[Program]VisualStudio
[SQL]SQLSERVER


and i wanna see

ColumnName
Browser
VisualStudio
SQLSERVER


my code

foreach (var s in ListBox1.Items)
{
if (s.Equals("]") == true)
{
var desc = s.Equals("]");
ListBox2.items.add(desc)
}
}

Answer

you can use split and last

 foreach (var s in ListBox1.Items)
 {
      var desc = s.ToString().Split("]").Last();
      ListBox2.Items.Add(desc);
 }

this way, your code won't go error even if there is no ']' in your string