C# 如何去掉string中所有转义字符(特殊符号)?

2024-12-01 03:39:21
推荐回答(4个)
回答1:

1、去掉字符串中的转义等特殊字符:

stringinputString=@”helloworld]\“;

StringBuildersb=newStringBuilder();

string[]parts=inputString.Split(newchar[]{’‘,‘\n’,‘\t’,‘\r’,‘\f’,‘\v’,’\’},StringSplitOptions.RemoveEmptyEntries);

intsize=parts.Length;

for(inti=0;i

sb.AppendFormat(“{0}“,parts[i]);

2、删除字符串头尾的转义等特殊字符串:

使用SubString和Remove来操作

比如去掉结尾的转义字符,可以使用

inputString.SubString(0,inputString.Length-1);

inputString.SubString(0,inputString.Length-2);

inputString.SubString(0,inputString.Length-3);

扩展资料

C#字符串取消转义字符的转义作用,使其正常显示

usingSystem;

usingSystem.Collections.Generic;

usingSystem.ComponentModel;

usingSystem.Data;

usingSystem.Drawing;

usingSystem.Linq;

usingSystem.Text;

usingSystem.Windows.Forms;

namespacetest1

{

publicpartialclassForm2:Form

{

publicForm2()

{

InitializeComponent();

}

privatevoidbutton1_Click(objectsender,EventArgse)

{

stringstr=@"D:\document\test.txt";

stringstr1="D:\\document\\test.txt";

MessageBox.Show(str+"---"+str1);

}

}

}

回答2:

using System.Linq;

string st = "\b\0\0XXXX-AP\0\0\0\0\0\0";
string s = new string((from c in st.ToCharArray() where char.IsControl(c) select c).ToArray());

回答3:

string st = "\b\\\0\0XXXX-AP\0\0\0\0\0\0";
st = st.Trim(new char[] { '\'', '\"', '\\', '\0', '\a', '\b', '\f', '\n', '\r', '\t', '\v' });

回答4:

-----------
-----------
这个我估计必须得把所有的转义字符先存起来,
然后再一个个来比较