Solved

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

Posted on 2007-03-17
9
503 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
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:

914 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

13 Experts available now in Live!

Get 1:1 Help Now