Solved

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

Posted on 2007-03-17
9
512 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Checkbox and ListView in Android Layout 4 65
diffSum example 4 36
java jdbc batch example 8 31
hibernate example for saving data 19 37
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
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…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

813 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now