Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Java/Pattern: Search/Replace for invalid characters

Posted on 2004-08-17
8
Medium Priority
?
852 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 500 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 500 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
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

 
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

715 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