我有一个类似INI文件格式的数据文件,需要被一些C代码和一些C#代码读取。C代码期望字符串值被引号包围。C#等效的代码正在使用某些底层类或其他我无法控制的东西,但基本上它将引号作为输出字符串的一部分。也就是说,数据文件的内容如下:
MY_VAL="Hello World!"
给了我
"Hello World!"
在我的C#字符串中,当我真正需要它包含时
Hello World!
如何根据条件(即首尾字符为引号)去除引号并获取所需的字符串内容。
我有一个类似INI文件格式的数据文件,需要被一些C代码和一些C#代码读取。C代码期望字符串值被引号包围。C#等效的代码正在使用某些底层类或其他我无法控制的东西,但基本上它将引号作为输出字符串的一部分。也就是说,数据文件的内容如下:
MY_VAL="Hello World!"
给了我
"Hello World!"
在我的C#字符串中,当我真正需要它包含时
Hello World!
如何根据条件(即首尾字符为引号)去除引号并获取所需的字符串内容。
在你的字符串上使用Trim方法,并将参数设置为双引号字符:
.Trim('"')
MY_VAL="c:\Users;\"c:\Program Files\""
时,Trim()将从字符串末尾删除两个引号字符。 - Paweł Bulwan我通常使用String.Trim()来实现这个目的:
string source = "\"Hello World!\"";
string unquoted = source.Trim('"');
我的实现会检查引号是否在两侧
public string UnquoteString(string str)
{
if (String.IsNullOrEmpty(str))
return str;
int length = str.Length;
if (length > 1 && str[0] == '\"' && str[length - 1] == '\"')
str = str.Substring(1, length - 2);
return str;
}
作为一个有点固执的人,(这是我;不要评论你自己),你可能想考虑
.Trim(' ').Trim('"').Trim(' ')
这样可以去除引号外的任何空格,然后去除引号,最后去除所包含字符串的任何边界空格。
只需获取返回的字符串并执行 Trim('"');
即可。
s = s.Substring(1, s.Length - 2);
public static class StringExtensions
{
public static string UnquoteString(this string inputString) => inputString.TrimStart('"').TrimEnd('"');
}
这只是在开头和结尾进行修剪...
string str = "\"HelloWorld\"";
string result = str.replace("\"", string.Empty);
string myworld = "\"Hello World!\"";
string start = myworld.Substring(1, (myworld.Length - 2));
string s = @"""Hey ""Mikey""!";
s = s.Trim('"').Replace(@"""""", @"""");
或者在使用撇号时:
string s = @"'Hey ''Mikey''!";
s = s.Trim('\'').Replace("''", @"'");
有时候,根本不需要引用值(即不包含空格),因此可能根本不需要引用。这就是在修剪之前检查引号字符的原因。
考虑创建一个帮助函数,以以下示例中的首选方式执行此操作。
public static string StripQuotes(string text, char quote, string unescape)a
{
string with = quote.ToString();
if (quote != '\0')
{
// check if text contains quote character at all
if (text.Length >= 2 && text.StartsWith(with) && text.EndsWith(with))
{
text = text.Trim(quote);
}
}
if (!string.IsNullOrEmpty(unescape))
{
text = text.Replace(unescape, with);
}
return text;
}
using System;
public class Program
{
public static void Main()
{
string text = @"""Hello World!""";
Console.WriteLine(text);
// That will do the job
// Output: Hello World!
string strippedText = text.Trim('"');
Console.WriteLine(strippedText);
string escapedText = @"""My name is \""Bond\"".""";
Console.WriteLine(escapedText);
// That will *NOT* do the job to good
// Output: My name is \"Bond\".
string strippedEscapedText = escapedText.Trim('"');
Console.WriteLine(strippedEscapedText);
// Allow to use \" inside quoted text
// Output: My name is "Bond".
string strippedEscapedText2 = escapedText.Trim('"').Replace(@"\""", @"""");
Console.WriteLine(strippedEscapedText2);
// Create a function that will check texts for having or not
// having citation marks and unescapes text if needed.
string t1 = @"""My name is \""Bond\"".""";
// Output: "My name is \"Bond\"."
Console.WriteLine(t1);
// Output: My name is "Bond".
Console.WriteLine(StripQuotes(t1, '"', @"\"""));
string t2 = @"""My name is """"Bond"""".""";
// Output: "My name is ""Bond""."
Console.WriteLine(t2);
// Output: My name is "Bond".
Console.WriteLine(StripQuotes(t2, '"', @""""""));
}
}