'Remove multiple char types from end of string
I have a loop that builds up address fields, some of these fields may be empty at the end of the string
List<string> list = new List<string>();
//list can contain any number of values, some of which might be "" (empty string)
string returnValue = "";
for (int iRow = 1; iRow <= list.Count; iRow++)
returnValue += String.Format("{0}, ", list[iRow]);
returnValue = returnValue.Trim();
my output is
asd, aaa, qwe, 123123, , , , ,
How can i remove the trailing ", " from the string?
Solution 1:[1]
You should also avoid using strings in your case, instead use StringBuilder. Avoid also using senseles formatting -just list[iRow] is a better option.
Try something like this instead:
string result = string.Join(", ",
list.Where(s => !string.IsNullOrEmpty(s)).ToArray());
Solution 2:[2]
string's TrimEnd static method allows to specify which characters should be trimmed.
However, in your case it would make sense to check in the for loop whether you have empty slots, and at the end put together a resulting string with string.Join(string separator, string[] parts)
Solution 3:[3]
If you want to remove something you added, don't add them in the first place. Also, the StringBuilder type is better suited for concatenating multiple strings, as it much more memory efficient.
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.Count; i++)
{
string rowValue = list[iRow];
if (!string.IsNullOrEmpty(rowValue))
{
sb.Append(rowValue);
sb.Append(", ");
}
}
// use sb.ToString() to obtain result
Solution 4:[4]
I hate to make assumptions, but I will because it seems you want preserve the "gaps" until you get to the end, in which case you should use TrimEnd. If not, then use any one of the other options to avoid adding the empty values in the first place.
More precisely if your output could look like:
asd, aaa, qwe, 123123, , , somevalue , ,
Then you'll have to loop through and use TrimEnd.
Otherwise, if you can collapse the fields then exclude the empties upfront.
asd, aaa, qwe, 123123, somevalue
Solution 5:[5]
try this:
List<string> list = new List<string>(
new string[]{"asd", "aaa", "qwe", "123123", "", null, "", null, ""});
return String.Join(", ", list.Where(i => !String.IsNullOrEmpty(i)).ToArray());
Solution 6:[6]
I realise that this is quite unreadable AND is wasteful but (without linq):
return string.Join(" ,",string.Join(", ",list.ToArray()).Split(", ", StringSplitOptions.RemoveEmptyEntries));
Solution 7:[7]
Found this, time for a better answer.
Try this:
returnValue = returnValue.TrimEnd(", ".ToCharArray());
You can convert a regular string to a char array and all those chars will be trimmed.
However, There are more problems. See comments in code below:
string returnValue = "";
for (int iRow = 0; iRow < list.Count; iRow++) //start from 0 not 1
{
//returnValue += String.Format("{0}, ", list[iRow]);
if(string.IsNullOrEmpty(list[iRow]) == false) //still allow space as valid entry...
{
//the list is already a list of strings, no reason to convert string to string.
returnValue += list[iRow] + ", ";
}
}
//alternatively try this, it will do all the work for you:
//returnValue = string.Join<string>(", ", list);
//deal with trailing comma and space at end:
returnValue = returnValue.TrimEnd(", ".ToCharArray());
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | flq |
| Solution 3 | Cecil Has a Name |
| Solution 4 | |
| Solution 5 | Rubens Farias |
| Solution 6 | Al Option |
| Solution 7 | Leo Muller |
