Thursday, 4 July 2019

How to remove Escape sequence from String?

How to remove Escape sequence from String?

There are many ways to remove escape sequence from bulky string, specially I like to use RegEx from java side.

public class Test {
public static void main(String[] args) {
String s = "\n\n\tabcdtest";
String result = new Test().removeEscapeChars(s);
System.out.println("RESULT::"+result);
}
private String removeEscapeChars(String finalString) {
        Matcher matcher = Pattern.compile("\\&([^;]{6})", Pattern.CASE_INSENSITIVE).matcher(finalString);
        while (matcher.find()) {
            String before = finalString.substring(0, matcher.start());
            String after = finalString.substring(matcher.start() + 1);
            finalString = (before + after);
        }
        return finalString;
    }
}


Related Posts:

0 comments:

Post a Comment