Java String Unescape unescape(String s)

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

Description

Unescape \n, \r, \t, and \0xxx unicode escapes

License

Open Source License

Declaration

public static String unescape(String s) 

Method Source Code

//package com.java2s;
/********************************************************************
Copyright (c) 2000-2008 Steven E. Hugg./*www  . jav  a2 s  .  co m*/
    
This file is part of FLCore.
    
FLCore is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
    
FLCore is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public License
along with FLCore.  If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/

public class Main {
    /**
      * Unescape \n, \r, \t, and \0xxx unicode escapes
      */
    public static String unescape(String s) {
        int l = s.length();
        StringBuffer dest = null;
        int i = 0;
        while (i < l - 1) {
            char c = s.charAt(i);
            if (c == '\\') {
                if (dest == null) {
                    dest = new StringBuffer(l);
                    dest.append(s.substring(0, i));
                }
                i++;
                c = s.charAt(i);
                switch (c) {
                case 'n':
                    dest.append('\n');
                    break;
                case 'r':
                    dest.append('\r');
                    break;
                case 't':
                    dest.append('\t');
                    break;
                case '*':
                    break;
                case '0':
                    i++;
                    int starti = i;
                    while (i < l && Character.isDigit(s.charAt(i)))
                        i++;
                    int octint = Integer.parseInt(s.substring(starti, i));
                    dest.append((char) octint);
                    i--;
                    break;
                default:
                    dest.append(c);
                    break;
                }
            } else {
                if (dest != null)
                    dest.append(c);
            }
            i++;
        }
        if (dest != null)
            while (i < l)
                dest.append(s.charAt(i++));
        if (dest != null)
            return dest.toString();
        else
            return s;
    }
}

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)