使用c# 9,我将从字符串的开始和结束处删除新行:
myString = myString.Trim('r', 'n');
但是,我需要从字符串开始和结束处删除所有新行和空格:
myString = " n n The text content n of my stringn nn n"
这个字符串的结果将是:
myString = "The text content n of my string"
我该怎么做?
正如@Steve建议的那样,您需要在myString
变量上使用String.Trim()
方法来摆脱这些字符
using System;
public class Program
{
public static void Main()
{
string myString = " n n The text content n of my stringn nn n";
Console.WriteLine("Before Trim: "{0}"", myString);
myString = myString.Trim();
Console.WriteLine("After Trim: "{0}"", myString);
}
}
上面示例的输出将是
Before Trim: "
The text content
of my string
"
After Trim: "The text content
of my string"
看看这个例子:https://dotnetfiddle.net/KbTaHF