Reputation: 487
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 -- "ú ;" 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
Reputation: 11
Could you please try the below lines:
byte ptext[] = original.getBytes("UTF8");
String value = new String(ptext, "UTF8");
Upvotes: -3
Reputation: 1
Im trying to encode the above string to UTF-8 equivalent but to replace only >special character ( ú ) with -- "ú ;" in this case.
I'm not sure what encoding "ú ;" 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
Reputation: 100050
You seem to want to use XML character entities.
Appache Commons Lang has a method for this (in StringEscapeUtils).
Upvotes: 0
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