Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

c#
string
communitycreator

How to remove leading and trailing characters from a string in C#

Siddharth Singh

The String class in the Namespace System provides the Trim() method with multiple overloads. Trim(char[]) returns a new instance of a string in which all the leading and trailing occurrences of a set of characters provided by the caller are removed.

Syntax

Trim(char[])

public string Trim (params char[]? trimChars);
  • The trimChars parameter can accept a variable number of characters as an array because it is a params parameter.
  • If any character not present in the trimChars array is encountered during the leading trim operation, it is stopped.
  • If any character not present in the trimChars array is encountered during the trailing trim operation, it is stopped.
  • If the trimChars parameter is entered as a null or empty array, it removes all of the leading and trailing whitespace characters from the string.
  • Trim() returns a new string with the remaining characters and does not modify the existing string instance.

Examples

Input trimChars Output
“aaabbbaaa” [‘a’] bbb
“abccccccab” [‘a’,‘b’] cccccc
" abc cab" [] “abc cab”

Code

Here, we are calling the Trim() method to remove the leading and trailing underscore and hyphen characters from a username. Notice that the underscore and hyphen in the middle of the string remain unmodified.

Output

Initial UserName : __high_sky-user---- 
After removing leading and trailing _ and - :  high_sky-user
using System;

class HelloWorld
{
    static void Main()
    {
       //Let's remove all leading and trailing underscores and hyphens from the username
       char[] trimChars = { '-', '_' };
       string userName = "__high_sky-user----";
       string output = userName.Trim(trimChars);
       Console.WriteLine("Initial UserName : {0} \nAfter removing leading and trailing _ and - :  {1}",userName, output);
    }
}

Other overloads of Trim()

The String class also has Trim(char) and Trim() overloads, which are used to remove the leading and trailing occurrences of single characters and whitespaces, respectively.

Trim(char)

Trim(char) removes the leading and trailing occurrences of the input character from a string and returns a new string. Each leading and trailing trim operation is stopped when a character other than the input character is encountered.

Syntax

public string Trim (char trimChar);

If the string is empty or all of the characters are equal to the trimChar, Trim(char) returns an empty string.

Trim()

Trim() removes the leading and trailing occurrences of whitespaces from a string and returns a new string. Each leading and trailing trim operation is stopped when a character other than a whitespace is encountered.

Syntax

public string Trim ();

Code example

using System;

public class StringTrimmer
{
    public static void Main()
    {
        string input1 = "1111hello1111111";
        Console.WriteLine("original : {0} \nAfter Trim('1') : {1}",input1, input1.Trim('1'));
        // output : hello

        string input2 = "www.testwwtest.ew";
        Console.WriteLine("original : {0} \nAfter Trim('w') : {1}",input2, input2.Trim('w'));
        // output : .testwwtest.e

         string input3 = "    hello   ";
        Console.WriteLine("original : {0} \nAfter Trim() : {1}", input3, input3.Trim());
        // output : hello
    }
}

The Trim() method is very useful for removing additional whitespaces from user input.

RELATED TAGS

c#
string
communitycreator
RELATED COURSES

View all Courses

Keep Exploring