Java String Unescape unescape(String str)

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

Description

Unescapes string, that can be used in .ini file

License

Apache License

Parameter

Parameter Description
str escaped string

Return

Unescaped value

Declaration

public static String unescape(String str) 

Method Source Code

//package com.java2s;
/*//from  www .j  a v  a  2  s. c  o  m
 * Copyright 2015 Nuke Sparrow <nukesparrow@bitmessage.ch>.
 *
 * 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 {
    /**
     * Unescapes string, that can be used in .ini file
     * @param str escaped string
     * @return Unescaped value
     */
    public static String unescape(String str) {
        StringBuilder b = new StringBuilder(str.length());
        int i = 0;
        int l = str.length();
        while (l > 0) {
            char c = str.charAt(i);
            if (c == '+') {
                b.append(' ');
                i++;
                l--;
            } else {
                if (l >= 3 && c == '%') {
                    try {
                        b.append((char) Short.parseShort(str.substring(i + 1, i + 3), 16));
                        i += 3;
                        l -= 3;
                    } catch (NumberFormatException ex) {
                        b.append(c);
                        i++;
                        l--;
                    }
                } else {
                    b.append(c);
                    i++;
                    l--;
                }
            }
        }
        b.trimToSize();
        return b.toString();
    }
}

Related

  1. unescape(String str)
  2. unescape(String str)
  3. unescape(String str)
  4. unescape(String str)
  5. unescape(String str)
  6. unescape(String str)
  7. unescape(String str)
  8. unescape(String str, char escapeChar, char[] targetChars, char[] escapedChars)
  9. unescape(String string)