468,512 Members | 1,457 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

retrieve listbox values and make comma seperated

I need make a comma seperated list, but whwn I build the list I get a
comma at the end. How do I remove it?

foreach (ListItem lst in REIPropertyType.Items)
{
if (lst.Selected == true)
{
REIPropertyTypeVal += lst.ToString() + ",";
}

}

Jul 18 '06 #1
3 2821
I need make a comma seperated list, but whwn I build the list I get a
comma at the end. How do I remove it?
Hello,

Instead of a 'for-each' loop, you can use a regular 'for' loop and check
whether the iterator's value is less than the last item's index
(REIPropertyType.Items.Count???? - 1) and if so, append the comma:

for (int i = 0; i < REIPropertyType.Items.Count; i++)
{
ListItem lst = REIPropertyType.Items[i] as ListItem;
if (lst.Selected == true)
REIPropertyTypeVal += lst.ToString();
if (i < REIPropertyType.Items.Count - 1)
REIPropertyTypeVal += ",";
}

Hope this helps.
--
Best regards,

Stanimir Stoyanov
ad***@nospam.stoyanoff.info

Jul 18 '06 #2
Just remove it afterwards:
REIPropertyTypeVal = REIPropertyTypeVal.Remove(REIPropertyTypeVal.Lengt h -
1);
or
REIPropertyTypeVal = REIPropertyTypeVal.TrimEnd(',');

/claes

<df********@hotmail.comwrote in message
news:11*********************@m79g2000cwm.googlegro ups.com...
>I need make a comma seperated list, but whwn I build the list I get a
comma at the end. How do I remove it?

foreach (ListItem lst in REIPropertyType.Items)
{
if (lst.Selected == true)
{
REIPropertyTypeVal += lst.ToString() + ",";
}

}

Jul 18 '06 #3
Note that the first would raise an ArgumentOutOfRangeException in the case
that there are no (selected) items in the ListBox control and thus
REIPropertyTypeVal is an empty string - trying to remove all characters after
the -1st position.
--
Best regards,

Stanimir Stoyanov
ad***@nospam.stoyanoff.info
"Claes Bergefall" wrote:
Just remove it afterwards:
REIPropertyTypeVal = REIPropertyTypeVal.Remove(REIPropertyTypeVal.Lengt h -
1);
or
REIPropertyTypeVal = REIPropertyTypeVal.TrimEnd(',');

/claes

<df********@hotmail.comwrote in message
news:11*********************@m79g2000cwm.googlegro ups.com...
I need make a comma seperated list, but whwn I build the list I get a
comma at the end. How do I remove it?

foreach (ListItem lst in REIPropertyType.Items)
{
if (lst.Selected == true)
{
REIPropertyTypeVal += lst.ToString() + ",";
}

}


Jul 18 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by muzamil | last post: by
1 post views Thread by -rick | last post: by
4 posts views Thread by Henrik Holle | last post: by
4 posts views Thread by Justin | last post: by
6 posts views Thread by Janaka | last post: by
3 posts views Thread by tshad | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.