Use char[]
instead and add them one by one using this method:
public char[] generatePath(String a, String b){
if(a==null || b==null)
return null;
char[] result = new char[a.length() +b.length()];
for(int i=0;i<a.length();i++)
result[i]=a.charAt(i);
for(int i=a.length();i<a.length()+b.length();i++)
result[i]=a.charAt(i);
return result;
}
This would ensure that each character is in the right place.
String
objects in Java don't have an encoding (*).
The only thing that has an encoding is a byte[]
. So if you need
UTF-8 data, then you need a byte[]
. If you have a String
that
contains unexpected data, then the problem is at some earlier place
that incorrectly converted some binary data to a String
(i.e. it was
using the wrong encoding).
(*) that's not entirely accurate. Actually they have an encoding, but
that's UTF-16 and can't be modified.
source: answer
What you have to do is to use Byte[]
instead of String
Try this
Charset.forName("UTF-8").encode(myString);
or this
byte[] ptext = String.getBytes("UTF-8");