Leer en inglés Editar

Compartir a través de


Strings.Trim(String) Method

Definition

Important

Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.

Returns a string containing a copy of a specified string with no leading spaces (LTrim), no trailing spaces (RTrim), or no leading or trailing spaces (Trim).

C#
public static string Trim(string? str);
C#
public static string Trim(string str);

Parameters

str
String

Required. Any valid String expression.

Returns

A string containing a copy of a specified string with no leading spaces (LTrim), no trailing spaces (RTrim), or no leading or trailing spaces (Trim).

Examples

This example uses the LTrim function to strip leading spaces and the RTrim function to strip trailing spaces from a string variable. It uses the Trim function to strip both types of spaces.

VB
' Initializes string.
Dim testString As String = "  <-Trim->  "
Dim trimString As String
' Returns "<-Trim->  ".
trimString = LTrim(testString)
' Returns "  <-Trim->".
trimString = RTrim(testString)
' Returns "<-Trim->".
trimString = LTrim(RTrim(testString))
' Using the Trim function alone achieves the same result.
' Returns "<-Trim->".
trimString = Trim(testString)

Remarks

The LTrim, RTrim, and Trim functions remove spaces from the ends of strings.

Applies to

Producto Versiones
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

See also