Link to home
Start Free TrialLog in
Avatar of jukkauusi
jukkauusi

asked on

URLEncoded to String

How can I convert Urlencoded string back 'normal' string?
something like:

String s = URLEncoder.encode("T S");
String s1 = IneedThis.toString();

and s1 should be "T S".

 
ASKER CERTIFIED SOLUTION
Avatar of mbormann
mbormann

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mbormann
mbormann

oops ,i forgot to add this ,soory for that .
see from https://www.experts-exchange.com/jsp/qShow.jsp?ta=java&qid=10223004 

use this and name this class as URLDecoder

public class URLDecoder
{
public static String decode(String s) {
             
ByteArrayOutputStream out = new ByteArrayOutputStream(s.length());
             
                for (int i = 0; i < s.length(); i++) {
                  int c = (int) s.charAt(i);
                  if (c == '+') {
                    out.write(' ');
                  }
                  else if (c == '%') {
                    int c1 = Character.digit(s.charAt(++i), 16);
                    int c2 = Character.digit(s.charAt(++i), 16);
                    out.write((char) (c1 * 16 + c2));
                  }
                  else {
                    out.write(c);
                  }
                } // end for

                return out.toString();
               
              }
}
Download :
            ftp://ftp.oreilly.com/published/oreilly/java/java.netprog/javanetexamples.zip 

Once deziped :
javanetexamples\05\URLDecoder.java

......
courtesy of 'pyxide'
Avatar of jukkauusi

ASKER

Thanks,

I made same kind of method as this URLDecoder.decode. My version is

private String MakeCleanString(String s) {
    String rc="";
    for(int i=0;i<s.length();i++) {
      if(s.charAt(i)=='+') {
        rc+=' ';
        continue;
        }
      if(s.charAt(i)=='%') {
        rc+=(char) Integer.decode("0x"+s.substring(i+1,i+3)).intValue();
        i+=2;
        continue;
        }
      rc+=s.charAt(i);
      }
    return rc;
    }

Do You find any problems with that one?

But You got points, anyway.
hey it's very nice!