String: "hello to the very tall person I am about to meet"
我希望它能变成这样:
String: hello to the very tall person I am about to meet
我只能找到截取开头的代码?
String: "hello to the very tall person I am about to meet"
我希望它能变成这样:
String: hello to the very tall person I am about to meet
我只能找到截取开头的代码?
使用 String.Substring 方法。
因此,如果您的字符串存储在变量 mystr
中,请按如下方式操作:
mystr = mystr.Substring(1, mystr.Length - 2);
myStr = myStr.Trim('"');
注意:这将删除所有开头和结尾的引号(文档)。
string
。 - Jan Paolo Go[]
应用于字符串变量会像char[]
一样打开它以通过索引进行访问,因此范围对于字符串类型是公平的游戏。 - Narish如果你想从字符串中删除特定的字符,比如你的例子中的引号,你可以使用 Trim
来同时去除开头和结尾的字符,或者使用 TrimStart
和 TrimEnd
如果你想要从开头和结尾去除不同的字符。将一个字符(或字符数组)传递给这些方法,以便从字符串开头和结尾去除它们。
var quotedString = "\"hello\"";
var unQuotedString = quotedString.TrimStart('"').TrimEnd('"');
// If the characters are the same, then you only need one call to Trim('"'):
unQuotedString = quotedString.Trim('"');
Console.WriteLine(quotedString);
Console.WriteLine(unQuotedString);
输出:
"你好"
你好
或者,您可以使用 Skip
和 Take
与 Concat
一起使用,从字符串的开头和结尾删除字符。这将即使对于空字符串也有效,节省了任何计算字符串长度的烦恼:
var original = "\"hello\"";
var firstAndLastRemoved = string.Concat(original.Skip(1).Take(original.Length - 2));