Reputation: 5341
I have String variable with value- f.e.:
this is test-str-ing_łóśżćń
.
And I would like replace this chars:
,
-
, ł
,ó
,ś
,ż
,ć
,ń
with those:
_
,_
,l
,o
,s
,z
,c
,n
.
And I mean here, that if parser will found f.e.: char -
(which is second in first list) should be replaced with char that is in the same position/place in second list, which in this example is: _
.
The char ó
should be replaced with char o
.
The char ń
should be replaced with char n
.
In my case the list of characters to replace is quite long and parsing in loop for each char to replace would not be enought efficient.
I know method replaceAll()
. but it only accept one in String and one out String
So I am looking for method, that will allow me to work on arrays/list of Strings instead of single String.
Please give me some help.
Upvotes: 1
Views: 1520
Reputation: 2812
Make a static lookup table:
private static char[] substitutions = new char[65536];
static {
// Initialize
for (char c = 0; c < substitutions.length; c++) {
substitutions[c] = c;
}
// Now add mappings.
substitions['-'] = '_'; // Map source->target character
... // Add the rest
}
// LATER IN Code
char[] stringChars = inputString.toCharArray();
for (int i = 0; i < stringChars.length; i++) {
stringChars[i] = substitutions[stringChars[i]];
}
outputString = new String(stringChars);
Upvotes: 0
Reputation: 1906
char[] out = new char[src.length()];
for( j ...){
inputChar = src.charAt(j);
for (int i = 0; i < convertChars.length; i++) {
if (inputChar == convertChars[i]) {
inputChar = toChars[i];
}
}
}
out[j] = inputChar ;
}
out2 = new String(out);
Extracted from bigger code without IDE, not tested. Loop (I hope) don't allocate objects and should not degrade speed.
Upvotes: 1
Reputation: 109577
Use java.text.Normalizer
to Decompose accented letters in base letter plus "combining diacritical marks."
String base = Normalizer.normalize(accented, Form.NFKD)
.replaceAll("\\p{M}", "");
This does a decompose (D) normalization, and then removes Marks.
Some replacements still needed.
Upvotes: 4