Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove non printable characters C# multilanguage

Tags:

c#

I have a multi-language application in asp.net C#. Here I have to create a zip file and use some items from the database to construct file name. I strip out special characters from file name. However if the language is German for example my trimming algorithm will remove some german characters like Umlaut.

Could someone provide me with a language adaptable trimming algorithm.

Here is my code:

private string RemoveSpecialCharacters(string str)
{
    return str;
    StringBuilder sb = new StringBuilder();
    foreach (char c in str)
    {
        if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') | c == '.' || c == '_' || c == ' ' || c == '+')
        {
            sb.Append(c);
        }
    }
    return sb.ToString();
}

thanks

like image 343
josephj1989 Avatar asked Jul 14 '12 13:07

josephj1989


People also ask

How do I remove hidden characters from a string?

These hidden character(s) are coming from end users who copy and paste html that makes up the newsletters into a form and submits it. A c# trim() removes these hidden chars if they occur at the end or beginning of the string. When the newsletter is viewed in gmail, gmail does a good job ignoring them.

How can we view non printable characters in a file?

[3] On BSD, pipe the ls -q output through cat -v or od -c ( 25.7 ) to see what the non-printing characters are. This shows that the non-printing characters have octal values 13 and 14, respectively. If you look up these values in an ASCII table ( 51.3 ) , you will see that they correspond to CTRL-k and CTRL-l.


1 Answers

Assuming you mean the name of the ZIP file, instead of the names inside the ZIP file, you probably want to check if the character is valid for a filename, which will allow you to use more than just letters or digits:

char[] invalid = System.IO.Path.GetInvalidFileNameChars();

string s = "abcöü*/";
var newstr = new String(s.Where(c => !invalid.Contains(c)).ToArray()); 
like image 145
Monroe Thomas Avatar answered Oct 21 '22 10:10

Monroe Thomas