Remove all non-ASCII characters from string

后端 未结 7 1161
野的像风
野的像风 2020-12-05 06:28

I have a C# routine that imports data from a CSV file, matches it against a database and then rewrites it to a file. The source file seems to have a few non-ASCII characters

7条回答
  •  南笙
    南笙 (楼主)
    2020-12-05 07:01

    Do it all at once

    public string ReturnCleanASCII(string s)
    {
        StringBuilder sb = new StringBuilder(s.Length);
        foreach(char c in s)
        {
           if((int)c > 127) // you probably don't want 127 either
              continue;
           if((int)c < 32)  // I bet you don't want control characters 
              continue;
           if(c == ',')
              continue;
           if(c == '"')
              continue;
           sb.Append(c);
        }
        return sb.ToString();
    }
    

提交回复
热议问题