Delete the last two lines of char!

The following code:

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

should output output such as "A, B, C, (space)", which will then be associated with dynamically generated GridView. I would like to remove the last line of two char, that is, ", (space)". How can i do this?

+3
source share
10 answers

I would not add them first :) 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
+4
source

Take a look at String.Join , which can do what you want without having to manipulate the last two characters.

+7
source

. ( i ), StringBuilder; ( #, 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
+3

, "A, B, C" "A, B, C". :

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

, , , , , .

, , , . , , .

+1

.TrimEnd( ",".ToCharArray()) , SubString:

strLetters.Substring(0, strLetters.Length - 2)
+1

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

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

PS: - , vb:)

0

"":

string k = "okay";
string s = k.Remove(k.Length - 2, 2);
0
source

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

.Fields("DESC1").Value = .Fields("DESC1").Value.TrimRight(", ".ToCharArrray()) 
0
source
var selectedValues = checkboxList.Items
    .Where(i => i.Selected)
    .Select(i => i.Fields("DESC1").Value);

var result = String.Join(", ", selectedValues); 
0
source

Does VB have a triple if statement?

If checkboxList.Items(i).Selected Then 
    .Fields("DESC1").Value += checkboxList.Items(i).Text + (i == checkboxList.Items.Length-1 ? "" : ", ")
End If
-1
source

Source: https://habr.com/ru/post/1715880/


All Articles