Android - concatenate two strings of different languages
I cannot find a solution to this "simple" action:
I am trying to add 2 lines to get the full file path (folders and filename):
String a = /storage/emulated/0/abc/לכ
/
this has non-english letters and
String b = 20141231_042822.jpg
String c = a + b
result:
/storage/emulated/0/abc/לכ/20141231_042822.jpg
(Tried also with StringBuilder)
source to share
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 ensures that each character is in the right place.
String
objects in Java are not encoded (*).The only one that has an encoding is
byte[]
. So if you need UTF-8 then you will needbyte[]
. If you haveString
contains unexpected data, then the problem is at some earlier location that incorrectly converted some binary data toString
(i.e. using incorrect encoding).(*), which is not entirely accurate. In fact, they are encoded, but that is UTF-16 and cannot be changed. source: answer
What you need to do is use byte[]
String instead
Try this
Charset.forName("UTF-8").encode(myString);
or this
byte[] ptext = String.getBytes("UTF-8");
source to share
Try using BidiFormatter
For example :
private static String text = "%s הוא עסוק";
private static String phone = "+1 650 253 0000";
String wrappedPhone = BidiFormatter.getInstance(true /* rtlContext */).unicodeWrap(phone);
String formattedText = String.format(text, wrappedPhone);
source to share