Trim away the last part of a string
-
Hi all, i just want to know how to trim away the last part of a string in a variable... I have the following: string strNum = "0821234567,0721234567,0831234567," string strNum2 = ""; if(strNum.EndsWith(",")) { } "Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
-
Hi all, i just want to know how to trim away the last part of a string in a variable... I have the following: string strNum = "0821234567,0721234567,0831234567," string strNum2 = ""; if(strNum.EndsWith(",")) { } "Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
-
You create a new string from the part of the string that you want to keep:
strNum = strNum.SubString(0, strNum.Length - 1);
--- b { font-weight: normal; }
Thank you for your time... :)
"Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
-
Thank you for your time... :)
"Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
-
Hmm, you should consider using "Trim" or "TrimEnd" instead of "Substring" - after all, you want to - well - trim :) It will save you the clumpsy "if" construction as well.
Thank you, the first one worked. so after i checked if there is a "," at the end of the string it just leaves it out. but i'll use the trim end next time. ;) thanks.
"Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
-
Hi all, i just want to know how to trim away the last part of a string in a variable... I have the following: string strNum = "0821234567,0721234567,0831234567," string strNum2 = ""; if(strNum.EndsWith(",")) { } "Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
On another note, something faster, and optimized than substring or trimending (think what shall happen calling substring on a very huge string?) strNum[strNum.Length-1] = string.Empty; There are few scenarios where this will be the best and others where this wont be a better idea.
Excelsior Arjun Bahree "By The Might of Mjolnir" I Came! I Coded! I Conquered!
-
On another note, something faster, and optimized than substring or trimending (think what shall happen calling substring on a very huge string?) strNum[strNum.Length-1] = string.Empty; There are few scenarios where this will be the best and others where this wont be a better idea.
Excelsior Arjun Bahree "By The Might of Mjolnir" I Came! I Coded! I Conquered!
Two problems with your code: strNum[index] is a char. string.Empty is a string. You can't assign a string to a char. Strings can't be changed. Hence strNum[index] is a read-only indexer. So I do not see how this should work. Using a StringBuilder might help if the strings are repeatedly modified, but for a single trim operation it would not make any sense.