vyeluri5
vyeluri5

Reputation: 487

Java encode special character in a String with UTF-8 character

 String original = "This is my string valúe";

I'm trying to encode the above string to UTF-8 equivalent but to replace only special character (ú) with -- "&#250 ;" in this case.

I've tried using the below but I get an error:

Input is not proper UTF-8, indicate encoding !Bytes: 0xFA 0x20 0x63 0x61

Code:

    String original = new String("This is my string valúe");

    byte ptext[] = original.getBytes("UTF-8");
    String value = new String(ptext, "UTF-8"); 

    System.out.println("Output : " + value);

    This is my string valúe

Upvotes: 5

Views: 29767

Answers (4)

kishore enumula
kishore enumula

Reputation: 11

Could you please try the below lines:

 byte ptext[] = original.getBytes("UTF8");
 String value = new String(ptext, "UTF8"); 

Upvotes: -3

Walker Case
Walker Case

Reputation: 1

Im trying to encode the above string to UTF-8 equivalent but to replace only >special character ( ú ) with -- "&#250 ;" in this case.

I'm not sure what encoding "&#250 ;" is but have you tried looking at the URLEncoder class? It won't encode the string exactly the way you asked but it gets rid of the spooky character.

Upvotes: 0

bmargulies
bmargulies

Reputation: 100050

You seem to want to use XML character entities.

Appache Commons Lang has a method for this (in StringEscapeUtils).

Upvotes: 0

Elliott Frisch
Elliott Frisch

Reputation: 201447

You could use String.replace(CharSequence, CharSequence) and formatted io like

String original = "This is my string valúe";
System.out.printf("Output : %s%n", original.replace("ú", "ú"));

Which outputs (as I think you wanted)

Output : This is my string valúe

Upvotes: 3

Related Questions