[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 219
  • Last Modified:

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".

 
0
jukkauusi
Asked:
jukkauusi
  • 4
1 Solution
 
mbormannCommented:
>>>>>String s1 = IneedThis.toString();

String s1 = URLDecoder.decode(s);
0
 
mbormannCommented:
oops ,i forgot to add this ,soory for that .
see from http://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();
               
              }
}
0
 
mbormannCommented:
Download :
            ftp://ftp.oreilly.com/published/oreilly/java/java.netprog/javanetexamples.zip 

Once deziped :
javanetexamples\05\URLDecoder.java

......
courtesy of 'pyxide'
0
 
jukkauusiAuthor Commented:
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.
0
 
mbormannCommented:
hey it's very nice!
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now