C# 如何使用封装和转义拆分已清除的字符串
我想分割一个由诸如“&”这样的字符消除的字符串,但是在某些值包含消除符的情况下,我想用双引号转义。什么是一种优雅的拆分方法,同时忽略已转义的除名字符,同时也考虑转义字符的转义 例如,正确拆分此字符串C# 如何使用封装和转义拆分已清除的字符串,c#,C#,我想分割一个由诸如“&”这样的字符消除的字符串,但是在某些值包含消除符的情况下,我想用双引号转义。什么是一种优雅的拆分方法,同时忽略已转义的除名字符,同时也考虑转义字符的转义 例如,正确拆分此字符串 var1=asdfasdf&var2=contain””quote&var3=”contain&delim”&var4=”contain””both&” 进入: 顺便说一句,我正在考虑Regex…我的解决方案,包括测试: void TestCharl
var1=asdfasdf&var2=contain””quote&var3=”contain&delim”&var4=”contain””both&”
进入:
顺便说一句,我正在考虑Regex…我的解决方案,包括测试:
void TestCharlesParse()
{
string s = @"var1=asdfasdf&var2=contain""""quote&var3=""contain&delim""&var4=""contain""""both&""";
string[] os = CharlesParse(s);
for (int i = 0; i < os.Length; i++)
System.Windows.Forms.MessageBox.Show(os[i]);
}
string[] CharlesParse(string inputString)
{
string[] escapedQuotes = { "\"\"" };
string[] sep1 = inputString.Split(escapedQuotes, StringSplitOptions.None);
bool quoted = false;
ArrayList sep2 = new ArrayList();
for (int i = 0; i < sep1.Length; i++)
{
string[] sep3 = ((string)sep1[i]).Split('"');
for (int j = 0; j < sep3.Length; j++)
{
if (!quoted)
{
string[] sep4 = sep3[j].Split('&');
for (int k = 0; k < sep4.Length; k++)
{
if (k == 0)
{
if (sep2.Count == 0)
{
sep2.Add(sep4[k]);
}
else
{
sep2[sep2.Count - 1] = ((string)sep2[sep2.Count - 1]) + sep4[k];
}
}
else
{
sep2.Add(sep4[k]);
}
}
}
else
{
sep2[sep2.Count - 1] = ((string)sep2[sep2.Count - 1]) + sep3[j];
}
if (j < (sep3.Length-1))
quoted = !quoted;
}
if (i < (sep1.Length - 1))
sep2[sep2.Count - 1] = ((string)sep2[sep2.Count - 1]) + "\"";
}
string[] ret = new string[sep2.Count];
for (int l = 0; l < sep2.Count; l++)
ret[l] = (string)sep2[l];
return ret;
}
void TestCharlesParse()
{
字符串s=@“var1=asdfasdf&var2=contain”“quote&var3=”“contain&delim”“&var4=”“contain”“both&”“;
字符串[]os=CharlesParse;
for(int i=0;i
是否有特殊原因要将双引号用作转义字符?在你的例子中,你似乎只是用它来逃避自己。此外,是否应该在“delim”和“var4=”之间设置一个&between?这似乎是两个非常相似的问题在一个小时内的重复。这是某人博客上的编程挑战吗?他们自己的角色实际上是任意的,这是默认的。是的,他们应该是介于两者之间的,ty!:)
void TestCharlesParse()
{
string s = @"var1=asdfasdf&var2=contain""""quote&var3=""contain&delim""&var4=""contain""""both&""";
string[] os = CharlesParse(s);
for (int i = 0; i < os.Length; i++)
System.Windows.Forms.MessageBox.Show(os[i]);
}
string[] CharlesParse(string inputString)
{
string[] escapedQuotes = { "\"\"" };
string[] sep1 = inputString.Split(escapedQuotes, StringSplitOptions.None);
bool quoted = false;
ArrayList sep2 = new ArrayList();
for (int i = 0; i < sep1.Length; i++)
{
string[] sep3 = ((string)sep1[i]).Split('"');
for (int j = 0; j < sep3.Length; j++)
{
if (!quoted)
{
string[] sep4 = sep3[j].Split('&');
for (int k = 0; k < sep4.Length; k++)
{
if (k == 0)
{
if (sep2.Count == 0)
{
sep2.Add(sep4[k]);
}
else
{
sep2[sep2.Count - 1] = ((string)sep2[sep2.Count - 1]) + sep4[k];
}
}
else
{
sep2.Add(sep4[k]);
}
}
}
else
{
sep2[sep2.Count - 1] = ((string)sep2[sep2.Count - 1]) + sep3[j];
}
if (j < (sep3.Length-1))
quoted = !quoted;
}
if (i < (sep1.Length - 1))
sep2[sep2.Count - 1] = ((string)sep2[sep2.Count - 1]) + "\"";
}
string[] ret = new string[sep2.Count];
for (int l = 0; l < sep2.Count; l++)
ret[l] = (string)sep2[l];
return ret;
}