Here you can find the source of sanitizeString(String str)
Parameter | Description |
---|---|
str | The string to iterate through and escape character |
Parameter | Description |
---|---|
Exception | Throws an Exception if an invalid escaped character is found in the string. |
public static String sanitizeString(String str) throws Exception
//package com.java2s; //License from project: Open Source License public class Main { /**//w w w .j a v a2s . c o m * Escapes all escapable characters in a string. * * @param str The string to iterate through and escape character * @return A String, similar to the argument but with all appropriate character pairs escaped. * @throws Exception Throws an Exception if an invalid escaped character is found in the string. */ public static String sanitizeString(String str) throws Exception { String newStr = ""; boolean escape = false; for (int i = 0; i < str.length(); i++) { char c = str.charAt(i); if (escape) { switch (c) { case '\\': newStr += "\\"; break; case '\"': newStr += "\""; break; case 'n': newStr += "\n"; break; case 'r': newStr += "\r"; break; case 'f': newStr += "\f"; break; case 'b': newStr += "\b"; break; case 't': newStr += "\t"; break; default: throw new Exception("Invalid escape character in string."); } escape = false; } else if (c == '\\') { escape = true; } else { newStr += c; } } return newStr; } }