Java examples for java.lang:String New Line
Returns a string that, when interpreted as a literal, represents the original string.
/*/*from www .j a v a 2 s .co m*/ The contents of this file are subject to the Electric Communities E Open Source Code License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.communities.com/EL/. Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. The Original Code is the Distributed E Language Implementation, released July 20, 1998. The Initial Developer of the Original Code is Electric Communities. Copyright (C) 1998 Electric Communities. All Rights Reserved. Contributor(s): ______________________________________. */ //package com.java2s; public class Main { public static void main(String[] argv) { String self = "java2s.com"; System.out.println(quote(self)); } /** * Returns a string that, when interpreted as a literal, represents the * original string. */ static public String quote(String self) { int len = self.length(); StringBuffer buf = new StringBuffer(len * 2); buf.append('\"'); for (int i = 0; i < len; i++) { char c = self.charAt(i); if ('\n' == c || '\'' == c) { buf.append(c); } else { escapedInto(c, buf); } } buf.append('\"'); return buf.toString(); } /** * Just the part of a character's quoted form that encodes the character. * <p/> * In other words, everything except the enclosing quote signs. This is * used for composing a quoted {@link org.erights.e.meta.java.lang.CharacterSugar * #escaped(char) character}, {@link org.erights.e.develop.format.StringHelper#quote(String) * string}, or {@link org.erights.e.elib.tables.Twine#quote()} Twine. * <p/> * In order to be efficiently reusable, this method takes a StringBuffer * argument. Since this doesn't make much sense from E, this method is * tamed away. * * @noinspection UnnecessaryFullyQualifiedName */ static public boolean escapedInto(char self, StringBuffer buf) { switch (self) { case '\b': { buf.append("\\b"); return true; } case '\t': { buf.append("\\t"); return true; } case '\n': { buf.append("\\n"); return true; } case '\f': { buf.append("\\f"); return true; } case '\r': { buf.append("\\r"); return true; } case '\"': { buf.append("\\\""); return true; } case '\'': { buf.append("\\\'"); return true; } case '\\': { buf.append("\\\\"); return true; } default: { if (32 > self || 126 < self) { String num = "0000" + Integer.toHexString(self); int len = num.length(); num = num.substring(len - 4, len); buf.append("\\u").append(num); return true; } else { buf.append(self); return false; } } } } }