remove specified character [] from string

Hi,

I need to know How can I excluding any characters of a string except a specified set of characters defined by Char[ ].

Example:

if I have

Char[ ] x = {..,..,..,.........};

string y;

and y is a mixture of characters in x and other characters not in x

so how can I return a string z that contains only the characters belong to x and excluding all others

Thanks in advance for any help.

Aya.



Answer this question

remove specified character [] from string

  • Fahd

    No problem. These are forums with a buffet of answers to pick from.

    I got confused a few times myself.



  • DecaysChampion

    So, indeed! I only readed the first section of the topic start post and started writing a sample. But indeed in the seconds section he/she asks something else.

    Sorry for my reply Andreas, i looks like we both answered the question
    Thank you very much for the notice!


  • rbedard

    Some self-explaing code:


    public void ExcludeCharacters( string y, char[] x );
    {
        StringBuilder buffer = new StringBuilder( y.Lenght );
       
        for( int i = 0; i < y.Lenght; i++ )
        {
            bool isBad = false;
            char toCheck = y[ i ];
           
            foreach(Char badCharacter in x )
            {
                if( toCheck.Equals( badCharacter ) )
                {
                    isBad = true;
                    break;
                }
            }
           
            if( !isBad )
            {
                buffer.Append( toCheck );
            }
        }
       
        return buffer.ToString();
    }

     



  • Shaneisasuper

    I also wrote an example so now you can pick and choose. When I saw the PJs post I rewrote it to be a similar function.

    private String ExcludeCharacters(String strText, Char[] arrValidChars)
    {
    // Create a string of the char array to be able to use IndexOf
    String strValidChars = new String
    (arrValidChars);

    // The filtered result
    StringBuilder strResult = new StringBuilder();

    // Check every character in the source string
    foreach (Char c in strText.ToCharArray())
    {
    // Is it part of the allowed characters
    if
    (strValidChars.IndexOf(c) >= 0)
    {
    // Yes, add it to the result
    strResult.Append(c);
    }
    }

    // Done
    return
    strResult.ToString();
    }

    I use a bit less own code, result is the same.



  • jjm

    Thank you very much andreas for you sample code!

    But you use a valid character array, sinds the topic starter want a invalid character array you example wouldn't work.


  • newbieToVJS --Now C&amp;#35;

    Thank you too much Andreas and PJ for your help . I am sorry for the confusion , I have corrected the post subject .

    Aya.


  • sarrafi

    Ehm, now I am confused, or wait!

     Ayooya wrote:

    I need to know How can I excluding any characters of a string except a specified set of characters defined by Char[ ].

    That is a double negation, exclude all except array, leads to include only array of chars

     Ayooya wrote:

    so how can I return a string z  that contains only the characters belong to x and excluding all others

    The subject is opposite of the post.



  • remove specified character [] from string