public class StringFilter extends Object
Usage:
StringFilter sf = new StringFilter(); // remove 1st comment sf.addReplaceFilter("/*", "*/", ""); // replace all multiple spaces sf.addReplaceAllFilter(" ", " "); // change author name sf.addReplaceFilter("author: ", "", "author: spl@sun.com"); String string = "/* comment */ 4s 2s 3s author: xxx@sun.com"; String result = sf.filter(string);
Constructor and Description |
---|
StringFilter() |
Modifier and Type | Method and Description |
---|---|
void |
addReplaceAllFilter(String find,
String replace)
Adds replace pattern into Filter.
|
void |
addReplaceFilter(String begin,
String end,
String replace)
Adds replace pattern into Filter.
|
String |
filter(String str)
Filters string.
|
void |
filterLinesToStream(String input,
PrintStream output)
Filters input string to a PrintStream.
|
static String |
replaceString(String original,
String begin,
String end,
String replace)
Finds substring which starts with first occurrence of 'begin' and ends
with nearest next occurrence of 'end' and replaces it with 'replace '.
|
static String |
replaceStringAll(String original,
String find,
String replace)
Finds and replace all substrings.
|
public void addReplaceFilter(String begin, String end, String replace)
begin
- the begin of substring to be findend
- the end of substring to be findreplace
- text to replaceStringFilter.replaceString(String original, String begin, String end, String replace )
public void addReplaceAllFilter(String find, String replace)
find
- text to findreplace
- text to replaceStringFilter.replaceString(String original, String begin, String end, String replace )
public String filter(String str)
str
- text to filterpublic static String replaceString(String original, String begin, String end, String replace)
Usage:
replaceString("a-bcd-ef", "b", "d", "") => a--ef replaceString("abc-def", "", "c", "") => -def replaceString("ab-cdef", "c", "", "") => ab- replaceString("ab-cdef-ab", "ab", "ab", "AB") => AB-cdef-AB replaceString("abcdef", "", "", "AB") => abcdef
replace
- text to replaceoriginal
- the original stringbegin
- the begin of substring to be findend
- the end of substring to be findpublic static String replaceStringAll(String original, String find, String replace)
original
- the original stringfind
- text to findreplace
- text to replaceStringFilter.replaceString(String original, String begin, String end, String replace)
public void filterLinesToStream(String input, PrintStream output)
input
- string to be filteredoutput
- stream to write results in