best way to replace forbidden characters

G

gabriel

greetings,

I got a string whch can contains some forbidden characters 'é' or '/'
this kind of stuff.

so far I've done this :

myString = myString.replace('é', '_');
and so on.

Now I want something better.

I thought of putting my forbidden chars into an array and loop over my
string char by char and search for it in the array :

this was definitively not a better solution :)

Can someone suggest anything better please ?

Many thx in advance !
 
J

jonck

Regular expressions are made for what you want.

For example:

<code>
import org.apache.regexp.RE;

public String replaceNonAlphaNumericCharacters(String text) {
String pattern = "[^0-9a-zA-Z]";
RE regex = new RE(pattern);
return regex.subst(text, "");
}
</code>
Kind regards, Jonck
 
G

gabriel

jonck a écrit :
Regular expressions are made for what you want.

For example:

<code>
import org.apache.regexp.RE;

public String replaceNonAlphaNumericCharacters(String text) {
String pattern = "[^0-9a-zA-Z]";
RE regex = new RE(pattern);
return regex.subst(text, "");
}
</code>
Kind regards, Jonck
of course !!!!
sorry for asking something that stupid !
thank you very much !

(yes, I had discarded the regular expressions idea)
 
J

Juha Laiho

gabriel said:
I got a string whch can contains some forbidden characters 'é' or '/'
this kind of stuff.

Note that characters in Java are quite a large set -- so it might be
more efficient to define a set of acceptable characters, and only
allow them.
 
T

Thomas G. Marshall

gabriel coughed up:
jonck a écrit :
Regular expressions are made for what you want.

For example:

<code>
import org.apache.regexp.RE;

public String replaceNonAlphaNumericCharacters(String text) {
String pattern = "[^0-9a-zA-Z]";
RE regex = new RE(pattern);
return regex.subst(text, "");
}
</code>
Kind regards, Jonck
of course !!!!
sorry for asking something that stupid !
thank you very much !

(yes, I had discarded the regular expressions idea)

This is probably because the boolean /not/ is less intuitive when usings
regex's.

I don't have regex experience in java, however I used to have a great deal
of it in my unix days (eons). The problem I think is that when looking
through the syntax, people hear the word "find" in their heads when they
should be hearing "pattern match". {shrug}
 
D

Dale King

gabriel said:
greetings,

I got a string whch can contains some forbidden characters 'é' or '/'
this kind of stuff.

so far I've done this :

myString = myString.replace('é', '_');
and so on.

Now I want something better.

I thought of putting my forbidden chars into an array and loop over my
string char by char and search for it in the array :

this was definitively not a better solution :)

Can someone suggest anything better please ?

If you are doing several different characters you probably want to do
this on an instance of StringBuffer (or StringBuilder if using JDK1.5)
or maybe even just a char array. Using a String object for multiple
replacements like that will create lots of String objects.

I would suggest a String of size 256 that is what each of the first 256
characters in Unicode are replaced with. So the 233rd character in that
array (é is character 0xE9 which is 233 decimal) would be _. The
characters that are not replaced would have the same character at that
position.

Then your code would be like:

String translation = ...your string of replacements...;
char[] buffer = new char[ myString.length() ];

for( int i = 0; i < myString.length(); i++ )
{
char c = myString.charAt( i );
if( c < translation.length() )
{
buffer[ i ] = translation.charAt( c );
}
else
{
buffer[ i ] = replacementForOtherUnicodeChars;
}
}

myString = new String( buffer );

That is likely to be faster than most other methods.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top