Solved

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

Posted on 2007-03-17
9
493 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 30

Expert Comment

by:mayankeagle
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:mayankeagle
ID: 18768963
Welcome back Webstorm :)
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 18779729
Thanks mayankeagle :)
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

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 Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
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:

708 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

20 Experts available now in Live!

Get 1:1 Help Now