Using C# 9 I am removing new lines from the begin and end of a String:
myString = myString.Trim('\r', '\n');
However, I need to remove all new lines and spaces from string begin and end:
myString = " \n \n The text content \n of my string\n \n\n \n"
The result for this string would be:
myString = "The text content \n of my string"
How can I do this?
CodePudding user response:
As @Steve suggested, you need to use the String.Trim()
method on the myString
variable to get rid of such characters
using System;
public class Program
{
public static void Main()
{
string myString = " \n \n The text content \n of my string\n \n\n \n";
Console.WriteLine("Before Trim: \"{0}\"", myString);
myString = myString.Trim();
Console.WriteLine("After Trim: \"{0}\"", myString);
}
}
The output of the above example would be
Before Trim: "
The text content
of my string
"
After Trim: "The text content
of my string"
Check out this example: https://dotnetfiddle.net/KbTaHF