Solved

Java/Pattern: Search/Replace for invalid characters

Posted on 2004-08-17
8
843 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 
LVL 86

Assisted Solution

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

Author Comment

by:go4java
ID: 11818738
ok. and the file solution?
0
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 11818745
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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 86

Expert Comment

by:CEHJ
ID: 11818754
This will clean that particular String:


s = s.replaceAll("[\\.:;\\-}\\[()'\"/]", " ");
0
 
LVL 92

Expert Comment

by:objects
ID: 11818861
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
ID: 11818866
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
ID: 11818943
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 11818976
8-)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

739 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