简体   繁体   中英

remove spaces and empty line in the end of a string vb.net

in vb.net i have a string that looks like this

"text text text                           
                             "

so in the end of it there are spaces and a new empty line

How can make this look like

"text text text"

string.TrimEnd :

var s = @"text text text                           
                         ";
Console.Write(s.TrimEnd() + "<-- End"); // Output: text text text<-- End

TrimEnd trims from the end of the string, Trim removes from both the beginning and end of the string.

You're looking for the Trim() method.

I'm not sure that Trim() will cut the new lines too...if not you can use it with param - Trim('\n') or Trim('\t') for tabs or even specify a list of characters which you'd like to cut off.

Dim value As String = "text text text                           
                         "
Dim trimmed As String = value.Trim()

Trim removes leading and trailing whitespace. String data often has leading or trailing whitespace characters such as newlines, spaces or tabs. These characters are usually not needed. With Trim we strip these characters in a declarative way.

Reference: http://www.dotnetperls.com/trim-vbnet

Use the string.TrimEnd(params char[] trimChars) method:

yourString.TrimEnd();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM