samedi 30 mai 2015
Alles außer Muster in String ersetzen
Posted on 06:56 by verona
Hi,
diese Funktion funktioniert, aber kann man das auch "besser" lösen?
diese Funktion funktioniert, aber kann man das auch "besser" lösen?
Java Code:
-
-
package sax.test;
-
-
import java.util.ArrayList;
-
import java.util.Arrays;
-
-
public class Regex {
-
-
-
-
/**
-
* input:
-
* random string, a-z, random length
-
* and a pattern "abc" for example
-
* pattern can be a-y, length min: 3 max: string.length()
-
* output:
-
* input string with anything but the pattern replaced with Z
-
*
-
* example:
-
* input: "aaaddd", pattern "ddd", output "ZZZddd"
-
* input: "adddb", pattern "ad", output "ZZddb"
-
* input: "ccchhhccc", pattern "hhhc", output: "ZZZhhhcZZ"
-
* input: "jjjiiikkk", pattern "jjj", output: "jjjZZZZZZ"
-
*
-
* @param sax_result
-
* @return
-
*/
-
-
-
StringBuilder tmp = new StringBuilder();
-
-
for (int n=0; n<= input.length()-1; n++) {
-
tmp.append('Z');
-
}
-
-
-
-
ArrayList<Integer> positions_of_pattern = new ArrayList<Integer>();
-
-
// find all positions of the pattern in string
-
for (int n=0; n <= input.length()-1-pattern.length()-1; n++) {
-
int index = input.indexOf(pattern, n);
-
-
if (index != -1) {
-
if (! positions_of_pattern.contains(index)) {
-
positions_of_pattern.add(index);
-
}
-
}
-
-
}
-
-
-
char[] tmpCharArray = tmp.toString().toCharArray();
-
char[] patternCharArray = pattern.toCharArray();
-
-
-
for (int n=0; n<= tmpCharArray.length; n++) {
-
-
if (positions_of_pattern.size() >= 1) {
-
if (n == positions_of_pattern.get(0)) {
-
for (char x: patternCharArray) {
-
tmpCharArray[n] = x;
-
n++;
-
}
-
positions_of_pattern.remove(0);
-
}
-
-
}
-
}
-
-
}
-
-
-
ArrayList<String> inputStrings = new ArrayList<String>(Arrays.asList( "bdddb", "dddhhh", "aaadddaddd"));
-
//System.out.println(replaceString(x));
-
replaceString2(x, pattern);
-
}
-
}
-
}
Alles außer Muster in String ersetzen
Categories: Alles außer Muster in String ersetzen
Inscription à :
Publier les commentaires (Atom)
0 commentaires:
Enregistrer un commentaire