Java String Unescape unescape(String s)

Here you can find the source of unescape(String s)

Description

Removes escaping from an RFC 5545 "text".

License

Apache License

Declaration

public static String unescape(String s) 

Method Source Code

//package com.java2s;
/*/*from w w w . ja  v a  2  s. c o  m*/
 * Copyright (C) 2010 The Android Open Source Project
 * 
 * Licensed under the Apache License, Version 2.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.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class Main {
    /**
     * Removes escaping from an RFC 5545 "text".
     * 
     *        ESCAPED-CHAR = ("\\" / "\;" / "\," / "\N" / "\n")
     *        ; \\ encodes \
     *        ; \N or \n encodes newline
     *        ; \; encodes ;
     *        ; \, encodes ,
     */
    public static String unescape(String s) {
        if (s.indexOf('\\') == -1) {
            return s;
        }
        StringBuilder result = new StringBuilder();
        for (int i = 0; i < s.length(); ++i) {
            char ch = s.charAt(i);
            if (ch == '\\' && i < s.length() - 1) {
                char ch2 = s.charAt(++i);
                if (ch2 == 'n' || ch2 == 'N') {
                    ch2 = '\n';
                }
                result.append(ch2);
            } else {
                result.append(ch);
            }
        }
        return result.toString();
    }
}

Related

  1. unescape(String s)
  2. unescape(String s)
  3. unescape(String s)
  4. unescape(String s)
  5. unescape(String s)
  6. unescape(String s)
  7. unescape(String s)
  8. unescape(String s, char[] charsToEscape)
  9. unescape(String s, int i)