none
Filter a string -no control characters- RegEx RRS feed

  • Question

  • Hello,
    Is there a way to solve this faster?
    public string FilterCleanASCII(string s)
    {
    	StringBuilder sb = new StringBuilder(s.Length);
    	foreach (char c in s)
    	{
    		if (c > 127) // no 127 either
    			continue;
    		if (c < 48)  // no control characters 
    			continue;
    		if (c == ',')
    			continue;
    		if (c == '"')
    			continue;
    		sb.Append(c);
    	}
    	return sb.ToString();
    }
    Here I look at every sign, when it's big, that takes time.
    I have no experience with RegEx, how could I solve it?
    Maybe a function where I hand over the allowed characters.
     Thank you for good examples.
     
    With best regards Markus


    Wednesday, November 21, 2018 5:10 PM

Answers

  • Hello,

    In the event you have not seen this, you might find this helpful.

    https://stackoverflow.com/questions/15259275/removing-hidden-characters-from-within-strings


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Markus Freitag Friday, November 23, 2018 4:43 PM
    Wednesday, November 21, 2018 5:15 PM
    Moderator
  • Check two minor manual improvements too, not based on Regular Expressions:

    public static string FilterCleanASCII2( string s )

    {

        StringBuilder sb = new StringBuilder( s.Length );

        foreach( char c in s )

        {

            if( c > 127 )

                continue;

            if( c < 48 )

                continue;

            sb.Append( c );

        }

        return sb.ToString();

    }

     

    public static string FilterCleanASCII3( string s )

    {

        char[] a = s.ToCharArray();

     

        int n = a.Length;

        int j = 0;

     

        for( int i = 0; i < n; ++i )

        {

            char c = a[i];

            if( c < 48 || c > 127 ) continue;

            a[j++] = c;

        }

     

        return new string( a, 0, j );

    }

     



    Wednesday, November 21, 2018 8:15 PM

All replies

  • Hello,

    In the event you have not seen this, you might find this helpful.

    https://stackoverflow.com/questions/15259275/removing-hidden-characters-from-within-strings


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by Markus Freitag Friday, November 23, 2018 4:43 PM
    Wednesday, November 21, 2018 5:15 PM
    Moderator
  • Check two minor manual improvements too, not based on Regular Expressions:

    public static string FilterCleanASCII2( string s )

    {

        StringBuilder sb = new StringBuilder( s.Length );

        foreach( char c in s )

        {

            if( c > 127 )

                continue;

            if( c < 48 )

                continue;

            sb.Append( c );

        }

        return sb.ToString();

    }

     

    public static string FilterCleanASCII3( string s )

    {

        char[] a = s.ToCharArray();

     

        int n = a.Length;

        int j = 0;

     

        for( int i = 0; i < n; ++i )

        {

            char c = a[i];

            if( c < 48 || c > 127 ) continue;

            a[j++] = c;

        }

     

        return new string( a, 0, j );

    }

     



    Wednesday, November 21, 2018 8:15 PM