Remove blank spaces from string in C# and VB.Net

Generally, we use the string keyword to store the text in c#. In C#, the string keyword is an alias for System.String class. String objects are immutable which cannot be changed after they have been created. In this article you will see the several ways to remove unnecessary blank spaces from the string.

Remove spaces from beginning or ending from string

C# provides Trim(), TrimStart() and TrimEnd() functions to remove spaces or new lines from the beginning or ending of string. Trim() function removes spaces or new lines from the beginning or ending of string where TrimStart() removes all leading occurrences of a set of white-space characters from the current String object and TrimEnd() removes all trailing occurrences of a set of white-space characters from the current String object. See the example below:

C#

// Input String
String st = “      Hello World       ”;
// Call Trim instance method.
String st_Trim = st.Trim();
String st_TrimStart = st.TrimStart();
String st_TrimEnd = st.TrimEnd();
 
Console.WriteLine("st_Trim = " + st_Trim);
Console.WriteLine("st_TrimStart = " + st_TrimStart);
Console.WriteLine("st_TrimEnd = " + st_TrimEnd);

VB.Net

Private st As String = "      Hello World       "
Private st_Trim As String = st.Trim()
Private st_TrimStart As String = st.TrimStart()
Private st_TrimEnd As String = st.TrimEnd()
 
Console.WriteLine("st_Trim = " + st_Trim)
Console.WriteLine("st_TrimStart = " + st_TrimStart)
Console.WriteLine("st_TrimEnd = " + st_TrimEnd)

Output

st_Trim = “Hello World”
st_TrimStart = “Hello World ”
st_TrimEnd = “ Hello World”

Replace multiple spaces within string

If you want to remove the unnecessary multiple spaces with in your string, you can take help of the regular expression like as:

C#

string myString = "Welcome   to         authorcode";
string newString = Regex.Replace(oldStr, " {2,}", " ");
 
Console.WriteLine("Old String = " + myString);
Console.WriteLine("New String = " + newString);

VB.Net

Private myString As String = "Welcome   to         authorcode"
Private newString As String = Regex.Replace(oldStr, " {2,}", " ")
 
Console.WriteLine("Old String = " + myString)
Console.WriteLine("New String = " + newString)

Output

Old String = “Welcome to authorcode”
New String = “Welcome to authorcode”

Remove specified characters from beginning or ending from string

There is another variations of the trim methods: Trim(chars[]), TrimStart(chars[]) and TrimEnd(chars[]).

Trim(chars[]) method

The Trim(chars[]) method removes all leading and trailing characters (which are given as parameter) from the string. For example if your string is ” [[*authorcode@1234*]]” and the parameter chars contains the ” “,[,*, and ] characters, the trim method will return “authorcode@1234”. See the below code:

C#>

char[] chars = { ' ', '[', '*', ']'};
string myString = "  [[*authorcode@1234*]]";
string newString = myString.Trim(chars);
 
Console.WriteLine("Old String = " + myString);
Console.WriteLine("New String = " + newString);

VB.Net

Private chars As Char() = {" "c, "["c, "*"c, "]"c}
Private myString As String = "  [[*authorcode@1234*]]"
Private newString As String = myString.Trim(chars)
 
Console.WriteLine("Old String = " + myString)
Console.WriteLine("New String = " + newString)

Output
Old String = ” [[*authorcode@1234*]]”
New String = “authorcode@1234”

TrimStart(chars[]) and TrimEnd(chars[]) methods

Similarly TrimStart(chars[]) removes all leading occurrences of a set of characters specified in an array parameter from the string and TrimEnd(chars[]) removes the all trailing occurrences of a set of characters specified in an array parameter.

Author: Ankur

Have worked primarily in the domain of Calling, CRM and direct advertisers services. My technological forte is Microsoft Technologies especially Dot Net (Visual Studio 2003, 2005, 2008, 2010 and 2012) and Microsoft SQL Server 2000,2005 and 2008 R2. My Area of Expertise is in C#. Net, VB.Net, MS-SQL Server, ASP. Net, Silverlight, HTML, XML, Crystal Report, Active Reports, Infragistics, Component Art, ComponeOne, Lead Tools etc.

One thought on “Remove blank spaces from string in C# and VB.Net”

Comments are closed.