Solved

Convert string by from "\\u0064" to "\u0064" or "d"?

Posted on 2007-03-17
9
519 Views
Last Modified: 2013-12-29
how can I convert a string like this:

String result = "unde\\146ine\\u0064";

into a string like this:

String result = "unde\146ine\u0064";
(this mean that new string result = "undefined";      \\146 --> \146 or d,    \\u0064 --> \u0064 or d)

thanks!
0
Comment
Question by:hieukhtn
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 18739763
result = result.replaceAll("\\\\\\\\", "\\\\");
0
 
LVL 92

Assisted Solution

by:objects
objects earned 125 total points
ID: 18740004
try something like:

            StringBuilder result = new StringBuilder();
            for (int i=0; i<s.length(); i++)
            {
               if (s.substring(i).startsWith("\\u"))
               {
                   result.append((char) Integer.parseInt(s.substring(i+2, i+6), 16));
                   i += 5;
               }
               else if (s.substring(i).startsWith("\\"))
               {
                   result.append((char) Integer.parseInt(s.substring(i+1, i+4), 8));
                   i += 3;
               }
               else
               {
                  result.append(s.charAt(i));
               }
            }
0
 
LVL 23

Expert Comment

by:Siva Prasanna Kumar
ID: 18740278
I didn't get u.
Why do u want to convert like that.

String result = "unde\\146ine\\u0064";

just see what it prints by using System.out.println(result);

it prints  "unde\146ine\u0064";

Thats what u want if i am getting it wrong please clarify :)
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 30

Expert Comment

by:Mayank S
ID: 18746291
Yeah by the looks of your question, aren't you already doing what you want to do?

>> this mean that new string result = "undefined";  

What does that mean?
0
 
LVL 13

Accepted Solution

by:
Webstorm earned 125 total points
ID: 18748310
try object's solution
or this one if you want to manage errors, and other escaped characters :

public static string unescape(String s)
{
      StringBuffer result = new StringBuffer();
      int j=0,i;
      while (  (i = s.indexOf('\\', j)) >=0 )
      {
           result.append( s.substring(j,i) );
           try
           {
               if (s.startsWith("\\u", i))
               {
                    result.append( (char) Integer.parseInt(s.substring(i+2, i+6), 16) );
                    j = i + 6;
               }
               else if (s.startsWith("\\x", i))
               {
                    result.append( (char) Integer.parseInt(s.substring(i+2, i+4), 16) );
                    j = i + 4;
               }
               else if (s.startsWith("\\n", i)) result.append('\n');
               else if (s.startsWith("\\r", i)) result.append('\r');
               else if (s.startsWith("\\t", i)) result.append('\t');
               else if (s.startsWith("\\b", i)) result.append('\b');
               else if (s.startsWith("\\f", i)) result.append('\f');
               else
               {
                    result.append((char) Integer.parseInt(s.substring(i+1, i+4), 8));
                    j = i + 4;
               }
           } catch(Exception ex)
           {
                  if (i+1 <s.length())
                  {
                       j=i+2;
                       result.append( s.charAt(i+1) );
                  }
                  else result.append( '\\' );
           }
     }
      result.append( s.substring(j) );
      return result.toString();
}
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18768963
Welcome back Webstorm :)
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 18779729
Thanks mayankeagle :)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question