Last active
August 29, 2015 14:17
-
-
Save VenkataRaju/0501dc4ba347c505c888 to your computer and use it in GitHub Desktop.
Escapes specified character with another specified character, similarly unescapes and splits a string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.List; | |
public final class CharEscaper | |
{ | |
private final char escapeChar, escapeForEscapeChar; | |
public CharEscaper(char escapeChar, char escapeForEscapeChar) | |
{ | |
if (escapeChar == escapeForEscapeChar) | |
throw new IllegalArgumentException(); | |
this.escapeChar = escapeChar; | |
this.escapeForEscapeChar = escapeForEscapeChar; | |
} | |
public String escape(String str) | |
{ | |
StringBuilder sb = new StringBuilder(str.length() + (str.length() / 14)); | |
for (int i = 0; i < str.length(); i++) | |
{ | |
char c = str.charAt(i); | |
if (c == escapeChar) | |
sb.append(escapeForEscapeChar).append(escapeChar); | |
else if (c == escapeForEscapeChar) | |
sb.append(escapeForEscapeChar).append(escapeForEscapeChar); | |
else | |
sb.append(c); | |
} | |
return sb.toString(); | |
} | |
public String unEscape(String escapedStr) | |
{ | |
StringBuilder sb = new StringBuilder(escapedStr.length() - (escapedStr.length() / 14)); | |
for (int i = 0; i < escapedStr.length(); i++) | |
{ | |
char c = escapedStr.charAt(i); | |
if (c == escapeForEscapeChar) | |
{ | |
i++; | |
if (i < escapedStr.length() && escapedStr.charAt(i) == escapeForEscapeChar) | |
sb.append(escapeForEscapeChar); | |
else | |
sb.append(escapedStr.charAt(i)); | |
} | |
else | |
sb.append(c); | |
} | |
return sb.toString(); | |
} | |
/** | |
* @return un escaped strings obtained after splitting the provided | |
* {@code escapedStr} | |
*/ | |
public List<String> splitEscapedStr(String escapedStr) | |
{ | |
List<String> parts = new ArrayList<String>(); | |
int startPos = 0; | |
for (int i = 0; i < escapedStr.length(); i++) | |
{ | |
char c = escapedStr.charAt(i); | |
if (c == escapeForEscapeChar) | |
i++; | |
else if (c == escapeChar) | |
{ | |
parts.add(unEscape(escapedStr.substring(startPos, i))); | |
startPos = i + 1; | |
} | |
} | |
parts.add(unEscape(escapedStr.substring(startPos, escapedStr.length()))); | |
return parts; | |
} | |
public static void main(String[] args) throws Throwable | |
{ | |
String str = "a# b"; | |
CharEscaper strEscaper = new CharEscaper('#', '\\'); | |
String escapedStr = strEscaper.escape(str); | |
System.out.println(escapedStr); // a\# b | |
String unEscapedStr = strEscaper.unEscape(escapedStr); | |
System.out.println(unEscapedStr); // a# b | |
System.out.println(str.equals(unEscapedStr)); // true | |
String strstr = escapedStr + "#" + "#" + escapedStr; | |
System.out.println(strstr); // a\# b##a\# b | |
List<String> parts = strEscaper.splitEscapedStr(strstr); | |
System.out.println("No. of parts: " + parts.size()); // 3 | |
System.out.println(parts); // [a# b, , a# b] | |
System.out.println(parts.get(2).equals(str)); // true | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment