Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!eternal-september.org!feeder.eternal-september.org!mx04.eternal-september.org!.POSTED!not-for-mail From: Knute Johnson Newsgroups: comp.lang.java.programmer Subject: Re: Unicode escapes and String literals? Date: Thu, 13 Dec 2012 14:55:17 -0800 Organization: A noiseless patient Spider Lines: 43 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 13 Dec 2012 22:55:17 +0000 (UTC) Injection-Info: mx04.eternal-september.org; posting-host="9b3fcb0d22708969e4dc99e7aa0ef1f9"; logging-data="7309"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/JfQgO6aPE2XLy2Woboy9Z" User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/17.0 Thunderbird/17.0 In-Reply-To: Cancel-Lock: sha1:zBKO4LsrUoTpmh92AtUnYNf5c4A= Xref: csiph.com comp.lang.java.programmer:20306 On 12/13/2012 11:46 AM, Daniel Pitts wrote: > On 12/13/12 9:31 AM, Knute Johnson wrote: >> I just had a great revelation as I was putting together my SSCCE for the >> question I was going to ask. So it has changed my question. How do I >> do the conversion of unicode escape sequences to a String that are done >> by string literals? >> >> String s = "\u0066\u0065\u0064"; >> >> becomes "fed" but if you create a String with \u0066\u0065\u0064 in it >> without using the literal it stays \u0066\u0065\u0064. Is there a built >> in mechanism in Java for doing that translation to a String? >> > > Do you mean, you have a String, whose value is "\\u0066\\u0065\\u0064", > you want to pass that String to a method which will return fed. > > meaning > > String foo = "\\u0066\\u0065\\u0064"; > > System.out.println(foo); // prints \u0066\u0065\u0064 > System.out.println(magicFunction(foo)); // prints fed > > There might be such a function in Apache Commons library, but I don't > think there is one in the standard API. I could be wrong though. I obviously didn't explain it well the first time around, so let me try again. I understand that the compiler reads unicode escape sequences pretty much anywhere and converts them to characters. What I want to be able to do is to do that conversion on characters that are in a String. So if in my String I had the characters \u0066\u0065\u0064 I would like to convert those to a String of "fed". I did look at the apache commons link you sent and that would probably do it but if the compiler can translate them it must have a method already. Maybe it's not public but that is what I was asking. So thanks everybody for your answers. -- Knute Johnson