omnia Mm
omnia Mm

Reputation: 157

how to get specific string from list box

i have a List box with some texts , i wanna get all the strings after Other: and this is the code i use

string[] causearr = lst_Box.Items[i].Value.Split('-');

SqlSrcComplainCgeAnalysis.InsertParameters["ID"].DefaultValue =causearr[0].ToString();                           SqlSrcComplainCgeAnalysis.InsertParameters["subId"].DefaultValue = causearr[2].ToString();

if (txt_OtherCause.Text != string.Empty && txt_OtherCause.Visible == true && (int.Parse(causearr[2].ToString()) == 7 || int.Parse(causearr[2].ToString()) == 15 || int.Parse(causearr[2].ToString()) == 21))
     SqlSrcComplainCgeAnalysis.InsertParameters["CauseComments"].DefaultValue = causearr[3].ToString().Substring(causearr[3].ToString().LastIndexOf(":") + 3);
else
      SqlSrcComplainCgeAnalysis.InsertParameters["CauseComments"].DefaultValue = string.Empty;

and this is the data in the List Box

Cause:SpareParts-SubCause:Others: First Line
Cause:Technical-SubCause:Others: Second Line

so how to return the data after "Others:" if this data changes , and also the Cause changes according to the user selection .

Upvotes: 0

Views: 455

Answers (3)

Matthew
Matthew

Reputation: 5202

What you can do is split the string first on the colons ':', and then the last item in the array would be the data that you want.

result[0] == 'Cause'
result[1] == 'SpareParts-SubCause'
result[2] == 'Others'
result[4] == 'Cause'
result[5] == 'First Line'

or you could use a Regex to do all this in a cool way:

http://msdn.microsoft.com/en-us/library/hs600312.aspx

Upvotes: 0

kuzditomi
kuzditomi

Reputation: 730

string value = "Cause:SpareParts-SubCause:Others: First Line"

string[] lines = Regex.Split(value, ":Others:");

you now need lines[1] i guess

Upvotes: 0

juergen d
juergen d

Reputation: 204746

string s = "Cause:SpareParts-SubCause:Others: First Line".Split(new char[] { ' ' }, 2)[1];

Upvotes: 1

Related Questions