Java String Unescape unescape(String s)

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

Description

Resolve escape sequences in a String.

License

Open Source License

Parameter

Parameter Description
s the String to unescape

Return

resolved String

Declaration

public static String unescape(String s) 

Method Source Code

//package com.java2s;
/* Copyright (c) 2001 - 2007 TOPP - www.openplans.org.  All rights reserved.
 * This code is licensed under the GPL 2.0 license, availible at the root
 * application directory.//from  w  w  w.j  av  a  2  s  .c om
 */

public class Main {
    /**
     * Resolve escape sequences in a String. 
     * 
     *  @param s the String to unescape
     *  
     *  @return resolved String
     */
    public static String unescape(String s) {
        if (s == null) {
            throw new IllegalArgumentException(
                    "The String to unescape may not be null.");
        }
        StringBuilder sb = new StringBuilder();
        boolean escaped = false;

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (escaped) {
                escaped = false;
                sb.append(c);
            } else if (c == '\\') {
                escaped = true;
            } else {
                sb.append(c);
            }
        }
        if (escaped) {
            throw new IllegalArgumentException(
                    "The specified String ends with an incomplete escape sequence.");
        }
        return sb.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)
  9. unescape(String s)