?
Solved

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

Posted on 2007-03-17
9
Medium Priority
?
548 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 86

Expert Comment

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

Assisted Solution

by:objects
objects earned 500 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
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…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.
Suggested Courses

719 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