What is the best way to remove ,
from following strings.
six,seven,eight,nine,ten,
int strLength = someString.Length; if (strLength > 0) { findString = findString .Substring(0, str.Length - 1); } if (findString ==",") { someString.Remove(someString.Length - 1) }
I have quickly compiled this code directly in StackExchange editor as an example (It may be have syntax error.
Please take above as logic purpose only.
I would appreciate if someone can provide optimized code for above logic.
UPDATE: I actually want to remove ,
from string if present at the end of string not in middle.
How about this.
yourString = yourString.TrimEnd(',');
A one line option:
someString = someString.EndsWith(",") ? someString.Substring(0, someString.Length - 1) : someString;
EDIT:
As per Dilshod's answer, this can be expressed as
someString = someString.TrimEnd(',');
which, IMHO, is better.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With