Bali C
Bali C

Reputation: 31231

C# Extracting String From ListView

I have an issue where my ListView (when showing as a string variable) keeps showing the items as ListViewItem {//item}. I wondered if there was anyway of extracting the string from between the {}?

The method I have at the moment works but is by no means how I want to be doing this.

string item = listView1.Items[i].ToString().Replace("ListViewItem: ", "").Replace("{", "").Replace("}", "");

i is from a for loop, just an incrementing number.

Thanks

Upvotes: 2

Views: 909

Answers (3)

Fadrian Sudaman
Fadrian Sudaman

Reputation: 6465

Ideal candidate for regex. If you match the string with the simple regex below, whatever in the group "val" value will be the text you want.

ListViewItem: \{(?<val>.*)\}

Upvotes: 0

pstrjds
pstrjds

Reputation: 17428

I think you should be able to use the Text property:

listview1.Items[i].Text;

Upvotes: 6

ipr101
ipr101

Reputation: 24236

I'm not sure if this is what you want but you can use -

string item = listView1.Items[i].Text

To get the text value of the item.

Upvotes: 7

Related Questions