Escape, unescape strings

JavaScript have escape(), unescape() functions.
How i can in JDeveloper make unescape()?

In Java?
The Java equivalent to these Javascript functions is:
import java.net.URLEncoder;
import java.net.URLDecoder;
String unescapedString = URLEncoder.encode(
  "The string ü@foo-bar
String escapedString = URLDecoder.decode(
  "The+string+%C3%BC%40foo-bar"
);If you're using Java 1.4, you should specify an encoding like this:
import java.net.URLEncoder;
import java.net.URLDecoder;
String unescapedString = URLEncoder.encode(
  "The string ü@foo-bar", "UTF-8"
String escapedString = URLDecoder.decode(
  "The+string+%C3%BC%40foo-bar", "UTF-8"
);

Similar Messages

  • Escape /unescape oddity in arrays

    HI,
    i have a data array that im breaking up correctly for what i
    need, but ive started adding smaller arrays 'escaped' into this
    array so that when i break it up i can unescape the sub arrays for
    my purposes.
    eg:
    dataarray=1,2,f,2,4,%20ghgj%kglf,2,3 ( the escaped %% bit
    altho not proper escaped is just to show as example where it
    stands)
    so when i extract the %20ghgj%kglf from the data array i can
    unescape it into a further array of
    subarray=20,ghgj,kglf
    now this seems to work fine on testing but on publishing
    doesnt work and by deduction sems to be the problem, ( if i remove
    the escaped section of the array and replace with a normal variable
    things work)
    are there any known problems with escape/unescape in arrays
    that i am missing?
    many thanks for your time
    shane

    I don't understand what you're doing. Are you trying to
    represent complex data in flashvars or external textfiles - e.g for
    LoadVars?
    If you are trying to represent an array as text so you can
    recreate it in flash then you can do it with different delimiters.
    So top level could be a comma and elements that are arrays
    could be delimited with whatever you want e.g. a pipe character (|)
    or maybe 2 pipe characters (||)
    That way you just use String.split at each level as required.
    But you can't easily specify the type of data.
    For a more generic way of representing complex data in
    flashvars or in loaded config files (i.e. from external text files
    ) you could try using a JSON implementation - check this out if so:
    http://tinyurl.com/2a55z6

  • Escape XML Strings with JDK class

    Hi,
    can anyone tell me how to escape XML-Strings with classes of the JDK?
    When searching I only was pointed to StringEscapeUtils from apache.commons.lang, but we would prefer to use the JDK instead of integrating an external lib.
    Our aim is to escape an XML attribute, so a CDATA is not applicable for us in this case.
    Thanks
    Jan

    I implemented it by myself:
    public static String escapeXmlAttribute(String attributeValue) {
            StringBuffer result = new StringBuffer();
            for (int c = 0; c < attributeValue.length(); ++c) {
                if (attributeValue.charAt(c) == '"') {
                    result.append("&#34;");
                } else if (attributeValue.charAt(c) == '&') {
                    result.append("&#38;");
                } else if (attributeValue.charAt(c) == '<') {
                    result.append("<");
                } else if (attributeValue.charAt(c) == '>') {
                    result.append(">");
                } else {
                    result.append(attributeValue.charAt(c));
            return result.toString();
        }{code}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • ReplaceAll / regexp / escaping a string

    Hi, I have a question about escaping a string for use with replaceAll. My objective is to replace all instances of string1 with string2 so I have been doing so using replaceAll. This works well however with it relying on regexp's I have some questions about escaping.
    I want to, for example, replace [%PARAM%] with '100%' however this starts to get in to the murky (to me) worlf of regular expressions. After some testing I realize I have lots of characters that are important to the regexp and I can avoid problems by escaping them like this:
    \\[\\%PARAM\\%\\] with '100\\%'
    This works however the values will be parsed out of strings the user enters so clearly I don't want them to have to add the escape characters, is there a way I can:
    a) Tell it to ignore special characters so it is treated like a basic searcha nd replace or
    b) Autmatically escape any problematic characters in the string before calling replaceAll?
    If there is another approach you would recommend I'd be very interested to hear it too.
    Thanks in advance,
    Chris.

    Don't get me started on the evils of writing Java inside JSP!
    Anyway, if you look at the documentation I helpfully linked for you, you'll see that the replace method that can take two Strings (two CharSequences to be precise) versus just two chars was introduced in version 1.5, so you must be using an older compiler.
    You should consider upgrading to 1.6. Even 1.5, let alone the version you must be using, is in its Java Technology End of Life (EOL) transition period.
    [http://java.sun.com/javase/downloads/index_jdk5.jsp]

  • Escaping a string for Javascript

    Hi!
    I've been using a lot of time on how to escape special characters in order for Javascript to accept a string. It seems like I need to escape single quotes, double quotes, backslash and newline.
    I've written some code, but I just can't get it to work.
    public static String editorSafeFilter(String text)
            if(text == null){
                return null;
            StringBuffer buffer = new StringBuffer(text.length());
             for (int i = 0; i < text.length(); i++) {
                char ch = text.charAt(i);
                 switch (ch) {
                   case 10: // '\n'
                        buffer.append(" ");
                        break;
                    case 13:
                         buffer.append(" ");     // '\r'
                         break;
                    case '\'':
                        buffer.append('\\');
                        buffer.append("\'");
                        break;
                    case '"':
                        buffer.append('\\');
                        buffer.append('\"');
                        break;
                    case '\\':
                        buffer.append('\\');
                        buffer.append('\\');
                        break;
                    default :
                        buffer.append(ch);
                        break;
            return buffer.toString();
        }Anyone have any links or sample code that works?
    thanks!
    Vidar

    It looks like you want the actual escape sequences... public static String editorSafeFilter(String text)
            if(text == null){
                return null;
            StringBuffer buffer = new StringBuffer(text.length());
             for (int i = 0; i < text.length(); i++) {
                char ch = text.charAt(i);
                 switch (ch) {
                   case '\012': // '\n'
                        buffer.append("\\n");     // '\n'
                        break;
                    case '\015':
                        buffer.append("\\r");     // '\r'
                        break;
                    case '\'':
                        buffer.append("\\\'");
                        break;
                    case '\"':
                        buffer.append("\\\"");
                        break;
                    case '\\':
                        buffer.append("\\\\");
                        break;
                    default :
                        buffer.append(ch);
                        break;
            return buffer.toString();
        }

  • Escaping xml string

    how to escape <,> in xml string to &lt; ,
    &gt;.is it possible to convert the entire xml string into this
    format
    ex:
    <root> <sample><id> 89
    </id></sample></root>
    this has to be converted into
    &lt; root&gt;. &lt; sample&gt;. &lt;
    id&gt;. 89 &lt; /id&gt;. &lt; /sample&gt;.
    &lt; /root&gt;.

    myString="<root> <sample><id> 89
    </id></sample></root>";
    myString.split("<").join("&lt;");

  • Escaping entire String in a Regular Expression

    How does one escape an entire String in a Pattern? I thought that prefixing it with "\\Q" and postfixing with "\\E" would do the trick but this is functioning strangely. It also ignores the possibility of a "\\Q" and/or "\\E" within the String. I guess one could escape every meta-character but this seems like overkill. Is there a utility method somewhere? Why is it that:
    System.out.println("xx\\xx".replaceAll("\\Qx\\x\\E", "a"));yields xax but
    System.out.println("xx\\xx".replaceAll("\\Q\\\\E", "a"));outputs xx\xx?

    It looks like you've uncovered a bug. Specifically, when the Pattern parser sees the backslash that you're trying to match, it looks at the next character to see if it's an 'E'. It isn't, so the parser consumes both characters, then starts looking for the \E sequence again. The next character, of course, is the 'E', but the parser just sees it as a literal 'E' now. End result: instead of a Pattern for a single backslash, you get a Pattern for a backslash, followed by a backslash, followed by an 'E', as demonstrated here:    System.out.println("xx\\\\Exx".replaceAll("\\Q\\\\E", "a")); prints xxaxx.
    Since you're escaping the whole regex, you can work around the bug by leaving off the \E:    System.out.println("xx\\xx".replaceAll("\\Q\\", "a")); prints xxaxx.
    Or you can do what I do and use this method to escape strings instead of \B ... \E:  public static String quotemeta(String str)
        if (str.length() == 0)
          return "";
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < str.length(); i++)
          char c = str.charAt(i);
          if ("\\[](){}.*+?$^|".indexOf(c) != -1)
            buf.append('\\');
          buf.append(c);
        return buf.toString();
      }(This is why I've never run afoul of this bug, though I use regexes a lot.)
    I'll submit this to BugParade if you like. While I'm at it, I can submit an RFE to have them add a quotemeta method to Pattern.

  • XML Diff escape/unescape

    When performing a diff between two xml files that may contain an escaped character i.e.
    <FIELD>This &amp; That</FIELD>
    The resulting xsl does not leave the & escaped producing invalid xml.
    <FIELD>This & That</FIELD>
    Is there some way to instruct the method to escape reserved characters or is the only solution to parse through resulting xsl and escape characters after the fact.

    Hi,
    You might want to try CDATA or the attribute <disable-output-escaping="yes"/>.
    Thanks,
    rajat

  • Escape for string will add to db what should i do ??

    Hi,I want to escape for html and xml to store in db
    the blocks { and } doing problem and get me runtime error
    any idea to store it in mysql

    I'm not clear you are trying to say, but are you at least using a PreparedStatement?
    [http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html]

  • Flex - Convert html escapes to string

    i want to convert this
    Error #1083: The prefix  &quot;http &quot; for element  &quot;//www.c.com::a &quot; is not bound.
    to this
    Error #1083: The prefix "http" for element "//www.c.com::a" is not bound.
    thanks

    Regular expressions.

  • Parsing escape sequences

    Hey,
    I've been stumped on this for a while; I have an application which reads the contents of a text file to obtain encrypted strings, which use escape sequences in them. I can only decrypt them once these escape sequences have been "parsed," per say.
    Most of the escape sequences are ASCII codes, like \035, and the Apache Commons package doesn't parse those....however, if I hardcode the encrypted strings into the application, they parse just fine. So is there a way to emulate java's parsing of these escape sequences?
    Thanks for your time.

    Sorry if this isn't allowed, but I wanted to post in case anyone else had this problem...i just ended up rewriting the parsing code in com.sun.tools.javac.scanner.Scanner. Actually, at first I didn't realize my escapes were octal sequences, but after that it became a lot clearer.
    public class StringEscapeUtils {
        public static String unEscape(String escapedString) {
            char[] chars = escapedString.toCharArray();
            StringBuffer outputString = new StringBuffer();
            try {
                for(int i = 0; i < chars.length; i++) {
                    if(chars[i] == '\\') {
                        switch(chars[i + 1]) {
                            case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7':
                                String octalValue = Character.toString(chars[i + 1]);
                                if('0' <= chars[i + 2] && chars[i + 2] <= '7')
                                    octalValue += Character.toString(chars[i + 2]);
                                if(chars[i + 1] <= '3' && '0' <= chars[i + 3] && chars[i + 3] <= '7')
                                    octalValue += Character.toString(chars[i + 3]);
                                outputString.append((char)Integer.parseInt(octalValue, 8));
                                i += 3;
                                break;
                            case 'u':
                                String unicodeChars = new String(new char[] {chars[i + 2], chars[i + 3], chars[i + 4], chars[i + 5]});
                                outputString.append((char)Integer.parseInt(unicodeChars, 16));
                                i += 5;
                                break;
                            case 'b':
                                outputString.append('\b');
                                i++;
                                break;
                            case 't':
                                outputString.append('\t');
                                i++;
                                break;
                            case 'n':
                                outputString.append('\n');
                                i++;
                                break;
                            case 'f':
                                outputString.append('\f');
                                i++;
                                break;
                            case 'r':
                                outputString.append('\r');
                                i++;
                                break;
                            case '\'':
                                outputString.append('\'');
                                i++;
                                break;
                            case '\"':
                                outputString.append('\"');
                                i++;
                                break;
                            case '\\':
                                outputString.append('\\');
                                i++;
                                break;
                            default:
                                outputString.append(chars);
    break;
    } else {
    outputString.append(chars[i]);
    } catch(ArrayIndexOutOfBoundsException e) {
    System.err.println("Mallformed escape sequence.");
    return null;
    return outputString.toString();

  • XML String encoding - anyone have the the code?

    I need to encode strings for use in XML (node values) and replace
    items like ampersands, < and > symbols, etc with the proper escaped strings.
    My code will be installed on systems where I CANNOT add additional libraries to whatever they may already have.
    So, I cannot use JAXP, for example.
    Does anyone have the actual Java code for making strings XML compatible ?
    I am particularly concerned that the if the string already contains a valid encoding that it is NOT 're-processed' so that this (excuse the extra -'s):
    'Hello &-amp-;'
    does not become this:
    'Hello &-amp-;-amp-;'
    Thanks.

    It isn't especially difficult code. Here's what you have to do:
    1. Replace & by &amp;
    2. Replace < by &lt;
    3. Replace > by &gt;
    4. Replace " by &quot;
    5. Replace &apos; by &apos;
    Note that it's important that #1 come first, otherwise you will be incorrectly processing things twice. The order of the rest doesn't matter. (Technically you don't have to do all of these things in all situations -- for example attribute values have different escaping rules than text nodes do -- but it isn't wrong to do them all.)
    And note that this is called "escaping", not "encoding".
    I am particularly concerned that the if the string already contains a valid encoding that it is NOT 're-processed'This isn't a valid design criterion. You have to set up your design so that you have unescaped strings and you are creating escaped strings, or vice versa. If you have a string and you don't know if it has already been escaped or not, then that's a design failure.

  • Find & replace part of a string in Numbers using do shell script in AppleScript

    Hello,
    I would like to set a search-pattern with a wildcard in Applescript to find - for example - the pattern 'Table 1::$*$4' for use in a 'Search & Replace script'
    The dollar signs '$' seem to be a bit of problem (refers to fixed values in Numbers & to variables in Shell ...)
    Could anyone hand me a solution to this problem?
    The end-goal - for now - would be to change the reference to a row-number in a lot of cells (number '4' in the pattern above should finally be replaced by 5, 6, 7, ...)
    Thx.

    Hi,
    Here's how to do that:
    try
        tell application "Numbers" to tell front document to tell active sheet
            tell (first table whose selection range's class is range)
                set sr to selection range
                set f to text returned of (display dialog "Find this in selected cells in Numbers " default answer "" with title "Find-Replace Step 1" buttons {"Cancel", "Next"})
                if f = "" then return
                set r to text returned of (display dialog "Replace '" & f & "' with " default answer f with title "Find-Replace Step 2")
                set {f, r} to my escapeForSED(f, r) -- escape some chars, create back reference for sed
                set tc to count cells of sr
                tell sr to repeat with i from 1 to tc
                    tell (cell i) to try
                        set oVal to formula
                        if oVal is not missing value then set value to (my find_replace(oVal, f, r))
                    end try
                end repeat
            end tell
        end tell
    on error number n
        if n = -128 then return
        display dialog "Did you select cells?" buttons {"cancel"} with title "Oops!"
    end try
    on find_replace(t, f, r)
        do shell script "/usr/bin/sed 's~" & f & "~" & r & "~g' <<< " & (quoted form of t)
    end find_replace
    on escapeForSED(f, r)
        set tid to text item delimiters
        set text item delimiters to "*" -- the wildcard 
        set tc1 to count (text items of f)
        set tc2 to count (text items of r)
        set text item delimiters to tid
        if (tc1 - tc2) < 0 then
            display alert "The number of wildcard in the replacement string must be equal or less than the number of wildcard in the search string."
            error -128
        end if
        -- escape search string, and create back reference for each wildcard (the wildcard is a dot in sed) --> \\(.\\)
        set f to do shell script "/usr/bin/sed -e 's/[]~$.^|[]/\\\\&/g;s/\\*/\\\\(.\\\\)/g' <<<" & quoted form of f
        -- escape the replacement string, Perl replace wildcard by two backslash and an incremented integer, to get  the back reference --> \\1 \\2
        return {f, (do shell script "/usr/bin/sed -e 's/[]~$.^|[]/\\\\&/g' | /usr/bin/perl -pe '$n=1;s/\\*/\"\\\\\" . $n++/ge'<<<" & (quoted form of r))}
    end escapeForSED
    For what you want to do, you must have the wildcard in the same position in both string. --> find "Table 1::$*$3", replace "Table 1::$*$4"
    Important, you can use no wildcard in both (the search string and the replacement string) or you can use any wildcard in the search string with no wildcard in the replacement string).
    But, the number of wildcard in the replacement string must be equal or less than the number of wildcard in the search string.

  • How to insert a string containing a single quote to the msql database? help

    how can i insert a string which contains a single quote in to database... anyone help
    Message was edited by:
    sijo_james

    Absolutely, Positively use a PreparedStatement. Do not use sqlEscape() function unless you have some overriding need (and I don't know what that could possibly be).
    There are 1000's of posts on the positive aspects of using a PreparedStatement rather than using a Statement. The two primary positive attributes of using a PreparedStatement are automatic escaping of Strings and a stronger security model for your application.

  • Accessing a JSTL variable in a JSP Scriptlet (need to replace string )

    I have
    <c:set var="myVar" value="..." />
    I need to replace single quotes and double quotes to be escaped because I pass them into javascript functions and set them as ID for div sections
    ... onclick ="func('${myVar}')" ..
    <div id="${myVar}">
    but if the string contains ' single quotes it messes up the javascript or double quotes messes up the ID portion of the HTML tag
    I know there is the JSTL function fn but I can't figure out how to do it properly in JSTL
    <c:set var="myVar"
    value="${fn:replace(myVar, "'", "\"")"/>
    But that gets tricky since the value portion is enclosed in quotes
    So I was thinking of using a Scriptlet part instead.

A: Accessing a JSTL variable in a JSP Scriptlet (need to replace string )

escaping quotes within quotes within quotes.... ARGH!
Recipe for a headache if ever there was one.
However you must be strong and resist the temptations of the dark side (Scriptlet code)
My suggestion for cleaning this up - write your own static function for escaping javascript strings.
public static String escapeJavascriptString(String s){
  return .......
}Then define the function in a tld:
<function>
    <description>
      Escapes a string for javascript purposes
    </description>
    <name>escapeJavascript</name>
    <function-class>com.mypackage.ELFunctions</function-class>
    <function-signature>java.lang.String escapeJavascript(java.lang.String)</function-signature>
    <example>
      <c:out value="${myfunc:escapeJavascript(name)}">
    </example>
  </function>Cheers,
evnafets

escaping quotes within quotes within quotes.... ARGH!
Recipe for a headache if ever there was one.
However you must be strong and resist the temptations of the dark side (Scriptlet code)
My suggestion for cleaning this up - write your own static function for escaping javascript strings.
public static String escapeJavascriptString(String s){
  return .......
}Then define the function in a tld:
<function>
    <description>
      Escapes a string for javascript purposes
    </description>
    <name>escapeJavascript</name>
    <function-class>com.mypackage.ELFunctions</function-class>
    <function-signature>java.lang.String escapeJavascript(java.lang.String)</function-signature>
    <example>
      <c:out value="${myfunc:escapeJavascript(name)}">
    </example>
  </function>Cheers,
evnafets

Maybe you are looking for

  • Why won't my pc successfully load the latest version of iTunes?

    Upgraded from iphone 4 to 4S; iTunes said that I needed to upgrade to the latest version of iTunes in order to sync 4S (version 10.5 or higher).  Version available for download is 10.6.1.  Downloads and gets at least 3/4 of way through the install an

  • Word docs don't display fonts since upgrade to 10.6.

    Word docs don't display fonts since upgrade to 10.6.  Got this error message in Console: 6/14/11 9:38:14 AM          [0x0-0x11011].com.microsoft.Word[140]          Tue Jun 14 09:38:14 Michael-2.local FontCacheTool[144] <Error>: ATSFontGetPostScriptNa

  • IDX5 - System is not a central XI; cannot start program

    hi all - Did anyone come across this error message - "System is not a central XI; cannot start program" when trying to execute the tcode - IDX5 on the XI box? I pushed an IDOC - MATMAS05 from IDOC to XI. It doesn't come into XI. 1. I checked the SM58

  • HTTP Sender Problem

    Hello, We have an HTTP => XI => R3 scenario. Using the http client tool we can successfully send a request to XI. When the url http://<server>:<port>/sap/xi/adapter_plain?namespace=urn%3A%2FTransactionDataSAPInbound&interface=MI%2DRequest&service=GRM

  • MFP M127fw not installing properly on Windows 8.1

    I have a brand new HP All-in-One HP19 machine an I'm simply trying to get the printer / Scanner unit to install. I hear the computer respond when I plug in the USB connector.  The Autorun.exe file starts. The software install assistant errors out at