Solved

Java/Pattern: Search/Replace for invalid characters

Posted on 2004-08-17
8
834 Views
Last Modified: 2008-02-01
Input:

testString = 'this is/[ a .-test s'tring wi"th in)valid ch}aracters'
invalidChars = ".:;-{]()... (ideally to be read from a file/xml)
toBeReplacedBy = ' ' (one white space)

Output:

validatedString = 'this is a test string with invalid characters'


Any sample method with file access (for reading invalidChars)?

Best Rgds.
0
Comment
Question by:go4java
  • 4
  • 3
8 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 125 total points
Comment Utility
s = s.replaceAll("[\\.:;\\-\\{\\]\\(\\)]", " ");
0
 

Author Comment

by:go4java
Comment Utility
ok. and the file solution?
0
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
Comment Utility
BufferedReader in = new BufferedReader(new FileReader(file));
String line = null;
while (null!=(line=in.readLine())
{
   line = line.replaceAll("[\\.:;\\-\\{\\]\\(\\)]", " ");
   // do what you need with line
}
in.close();

0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
This will clean that particular String:


s = s.replaceAll("[\\.:;\\-}\\[()'\"/]", " ");
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 92

Expert Comment

by:objects
Comment Utility
The following will do what you need:

      public static String strip(String s, String invalid)
      {
         char[] chars = s.toCharArray();
         StringBuffer result = new StringBuffer();
         for(int i = 0;i<chars.length;i++)
         {
            if (invalid.indexOf(chars[i]))
            {
                // invalid char, do as required
            }
            else
            {
               result.append(chars[i]);
            }
         }
         return result.toString();
      }
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
to read the invalid chars just use something like:

BufferedReader in = new BufferedReader(new FileReader(file));
String line = in.readLine();
in.close();
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
8-)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now