3

Possible Duplicate:
How automatically remove all white spaces start or end in a string

I have a number of strings that I need to trim whitespaces from, however I only need to remove them from the end. They are being used for company and individual names, so it's ok for there to be spaces in the middle of the string.

Community
  • 1
  • 1
NealR
  • 10,189
  • 61
  • 159
  • 299
  • It's always a good idea to explain what you're looking for in an answer. The "best method" often depends on what your main focus is (readability, speed, resource usage, etc.) - but admitedly TrimEnd() should cover most of these. – 4444 Aug 23 '12 at 16:15

5 Answers5

17

You could just use Trim() or if you just want to apply to just the end TrimEnd()

string s = "test                      ";

s = s.Trim();
s = s.TrimEnd();
Gabe
  • 49,577
  • 28
  • 142
  • 181
2

Only from the end?

String.TrimEnd()

http://msdn.microsoft.com/en-us/library/system.string.trimend.aspx

Richard Morgan
  • 7,601
  • 9
  • 49
  • 86
1

String.TrimEnd is the answer — trims selected characters from the end of the string only.

Note: You should pay attention to the documentation in MSDN: there was a breaking change to the behavior of this method, so the actual behavior depends on the version of .NET Framework you are targetting:

Notes to Callers
The .NET Framework 3.5 SP1 and earlier versions maintains an internal list of white-space characters that this method trims if trimChars is null or an empty array. Starting with the .NET Framework 4, if trimChars is null or an empty array, the method trims all Unicode white-space characters (that is, characters that produce a true return value when they are passed to the Char.IsWhiteSpace method). Because of this change, the Trim() method in the .NET Framework 3.5 SP1 and earlier versions removes two characters, ZERO WIDTH SPACE (U+200B) and ZERO WIDTH NO-BREAK SPACE (U+FEFF), that the Trim() method in the .NET Framework 4 and later versions does not remove. In addition, the Trim() method in the .NET Framework 3.5 SP1 and earlier versions does not trim three Unicode white-space characters: MONGOLIAN VOWEL SEPARATOR (U+180E), NARROW NO-BREAK SPACE (U+202F), and MEDIUM MATHEMATICAL SPACE (U+205F).

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
  • @downvoter Would you care to comment? Did you mention the OP's sentence „however I only need to remove them **from the end**“? – Ondrej Tucny Sep 20 '12 at 07:55
1

You can just use s.TrimEnd(' ');.

If you omit the parameter, it'll remove all the Unicode whitespace characters (which may or may not be what you want to achieve).

Vlad
  • 35,022
  • 6
  • 77
  • 199
  • Note that every call this way will create a `char[]` which has to be garbage collected. – springy76 Jul 06 '16 at 08:46
  • @springy76: yes, but you anyway have to have an allocation (strings are immutable), so I don't see a _big_ problem here. If performance microoptimizations is really really that important (say, the code is inside a tight loop), one should start with replacing `string` with a `StringBuilder` for the start. – Vlad Jul 06 '16 at 08:56
0
Strings.RTrim("string")

docs

"string".TrimEnd()

docs

levinalex
  • 5,889
  • 2
  • 34
  • 48