Reputation: 306
Welcome,
I have a byte[]
which is the binary representation of a String
. And I wanted to replace a part of this String
and get back the new byte[]
!
I have tried:
String string = new String(array);
string = string.replace("#+#","SOME STRING");
array = string.getBytes();
The problem is that array is that the array afterwards is something different and not only because of the replacement.
The content of the array are serialized objects seperated with "#+#".
Upvotes: 1
Views: 7211
Reputation: 726
Be explicit about the character encoding you are using and use an encoding such as "Latin-1" where all byte sequences map to valid Unicode characters:
String string = new String(array, "Latin-1");
string = string.replace("#+#","SOME STRING");
array = string.getBytes("Latin-1");
Upvotes: 3