views:

641

answers:

10

The following code:

If checkboxList.Items(i).Selected Then 
   .Fields("DESC1").Value += checkboxList.Items(i).Text + ", "
End If

should produce output such as "A, B, C,(space)", which will then be bound to a dynamically created GridView. I would like to remove the last two-char string, that is ",(space)". How can I do this?

+1  A: 

It seems you just want to get "A, B, C" from "A, B, C, ". A bit of simple string manipulation should do the job:

Dim input = "A, B, C, "
Dim result = input.Substring(0, input.LastIndexOf(","))

This is more versatile than simply removing the last two characters, since it looks for the last comma, which is what I believe you are after.

Of course, the fact that you're adding on these two chars in the first place sounds a bit dodgy. I'd need to see more context to show how this can be avoided, however.

Noldorin
This will throw an exception if input is empty
Binary Worrier
+4  A: 

I wouldn't add them on in the first place :) try

If checkboxList.Items(i).Selected Then    
    if .Fields("DESC1").Value Is System.DbNull.Value then
        .Fields("DESC1").Value = checkboxList.Items(i).Text
    else
        .Fields("DESC1").Value +=  ", " + checkboxList.Items(i).Text
    End If
End If
Binary Worrier
You don't need the += in the first case. It might even be a bug if .Value can be Null.
kibibu
I should say, you don't need the + in the +=
kibibu
@kibibu: ta pal, well spotted
Binary Worrier
it works but "IF String.IsNullOrEmpty(.Fields("DESC1").Value)" will give error, "Conversion from type "DBNull" to string is invalid". So, i change it to "IF .Fields("DESC1").Value is System.DbNull.Value" then it works! Thanx.....
WeeShian
+1  A: 

You can use .TrimEnd(", ".ToCharArray()) on the string, or you can use SubString:

strLetters.Substring(0, strLetters.Length - 2)
MartinHN
A: 

use

.Fields("DESC1").Value += checkboxList.Items(i).Text + ", "

. Fields("DESC1").Value = .Fields("DESC1").Value.TrimRight(new []{',',' '});

PS:- sorry if it is not valid vb syntax :)

TheVillageIdiot
A: 

There is also just "Remove":

string k = "okay";
string s = k.Remove(k.Length - 2, 2);
Noon Silk
A: 

Does VB have a ternary if operator?

If checkboxList.Items(i).Selected Then 
    .Fields("DESC1").Value += checkboxList.Items(i).Text + (i == checkboxList.Items.Length-1 ? "" : ", ")
End If
nexus
Yes it does, but that's not it.
Binary Worrier
What is it then? I'll edit the answer accordingly.
nexus
Sorry, I'll correct myself, No VB.Net does not an a ternary operator, but there is a function that does something similar to the ?: operator in c#. However as it's a function it'll always evaluate both statements
Binary Worrier
Ah, ok. Thanks for the correction :)
nexus
VB9 (.NET 3) actually has the ternary operator (as a true operator). It is similar to the old IIf(...) function, but is named only If(...), and can take 2 or 3 parameters.
awe
+7  A: 

Take a look at String.Join, which may do what you want, without the need to manipulate the final two characters.

DanDan
It doesn't exactly start from an array (as input), though...
Marc Gravell
String.Join takes an array of strings. `checkboxList.Items` is a collection class, not array...
awe
A: 

This will remove all trailing , and/or [space]:

.Fields("DESC1").Value = .Fields("DESC1").Value.TrimRight(", ".ToCharArrray())
awe
+3  A: 

For info, string concatenation is expensive. It looks (from the i and from the results) like you should really be using a StringBuilder; some rough pseudo-code (in C#, but trivial to translate to VB.Net):

StringBuilder sb = new StringBuilder();
for(int i = 0 ; i < checkboxList.Items.Count ; i++) {
    if(checkboxList.Items[i].Selected) {
        if(sb.Length > 0) sb.Append(", "); // add separator
        sb.Append(checkboxList.Items[i].Text); // add text
    }
}
someOjb.Fields("DESC1") = sb.ToString(); // use result
Marc Gravell
If you know approximate how long the total string will be, it will be even more efficient to pre-initialize the size of the StringBuilder by something like this (example if each item including separator is approximate 6 chars long): `StringBuilder sb = new StringBuilder(6 * checkboxList.Items.Count)` If the size you allocate is close to the actual size at the end, you will end up with little or no need for re-allocation of more memory.
awe
True, true - but you'd need to know typical text lengths; in the general case it is probably not worth scanning twice - just let the doubling algorithm handle it.
Marc Gravell
Awesome solution, Marc - this is the one I implemented.
John Dunagan
A: 
var selectedValues = checkboxList.Items
    .Where(i => i.Selected)
    .Select(i => i.Fields("DESC1").Value);

var result = String.Join(", ", selectedValues);
Sébastien Ros