我正在从.csv文件中大量上传信息,我需要将此字符替换为ascii"�"以获取正常空间"".
对于C/C++/JAVA,字符"�"对应于"\ uFFFD",它似乎称为REPLACEMENT CHARACTER.其他如C#官方文档中的空格类型如U + FEFF,205F,200B,180E,202F.
我正在尝试以这种方式替换
public string Errors="";
public void test(){
string textFromCsvCell= "";
string validCharacters="^[0-9A-Za-z().:%-/ ]+$";
textFromCsvCell="This is my text from csv file"; //ALl spaces aren't normal space " "
string cleaned = textFromCsvCell.Replace("\uFFFD", "\"")
if (Regex.IsMatch(cleaned, validCharacters ))
//All code for insert
else
Errors=cleaned;
//print Errors
}
Run Code Online (Sandbox Code Playgroud)
测试方法给我看这个文字:
"这是来自csv文件的my�texto"
我尝试了一些解决方案
尝试解决方案1:使用修剪
Regex.Replace(value.Trim(), @"[^\S\r\n]+", " ");
Run Code Online (Sandbox Code Playgroud)
尝试解决方案2:使用替换
System.Text.RegularExpressions.Regex.Replace(str,@"\s+"," ");
Run Code Online (Sandbox Code Playgroud)
尝试解决方案3:使用修剪
String.Trim(new char[]{'\uFEFF','\u200B'});
Run Code Online (Sandbox Code Playgroud)
尝试解决方案4:将[\ S\r \n]添加到validCharacters
string validCharacters="^[\S\r\n0-9A-Za-z().:%-/ ]+$";
Run Code Online (Sandbox Code Playgroud)
什么都行不通
有人有想法吗?我怎样才能更换它?我非常感谢你的帮助,谢谢
资料来源:
http://www.fileformat.info/info/unicode/char/0fffd/index.htm