CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: String Filter

  1. #1
    Guest

    String Filter

    Who can give me some idea how to write efficient string filter? work like this:
    String before filtered=" This is a **** **** **** test ", string after filter=" This is a **** **** **** test".
    Many thanks!

    email to me: [email protected]


  2. #2
    Join Date
    Aug 1999
    Posts
    10

    Re: String Filter

    In order to do this, you need to create a StringTokenizer to parse the string. StringTokenizer st = new StringTokenizer(string, " ");


    What this statement does is create a new tokenizer for the String object string. This would obviously be the string that you wanted to filter. The " " tells the parser that you want to use the space character as its key, so it will separate things based on the space character. Then you will use a while loop to cycle through the string, and check each word that it parses out against a list.while(st.hasMoreToken())
    {
    String word = st.nextToken(); // get the next word from the string
    filter(word); // call the method filter and pass in the word
    }



    You would then have to make a method called filter to check the string that you pass to it. All this method would contain is a bunch of calls to the String method equals(). This method is used like a.equals(b);

    which compares string a to string b and returns true if they are equal. After you check the word for a comparison to the words you want to filter, you would have to break the string before the filtered word. You can do this by calling the method indexOf(String). You pass in the word you wanted filtered out and it will give you the index of the first occurance of this word. Then you can use the substring() method to break up the string, to remove the word, and finally you'd use the concat() method to put the two substrings together with a string of stars '*' or whatever character you wanted.
    Hope this helps


  3. #3
    Guest

    Re: String Filter

    I want to say thank you. Your's idea show me the way to solve the problem


  4. #4
    Guest

    Re: String Filter

    It looks to me as though you would like a replace() method in the String class?

    Like
    String myStr = "aa a a aa a";
    myStr.replace("aa","b");

    would then give "b a a b a"

    Is this so, send me an e-mail at [email protected] and I can send you a StringUtility class that does this and others.




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured