4

I can't find a solution to this "simple" action:

I'm trying to append 2 strings to get a full file path (folders and file name):

String a = /storage/emulated/0/abc/לכ/
this has non-English letters and

String b = 20141231_042822.jpg

String c = a + b

the result:

/storage/emulated/0/abc/לכ/20141231_042822.jpg

(Tried with StringBuilder as well)

Nischal Hada
  • 3,230
  • 3
  • 27
  • 57
Miko Diko
  • 944
  • 1
  • 13
  • 33

2 Answers2

1

Try to use 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);
NickF
  • 5,637
  • 12
  • 44
  • 75
0

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");
Community
  • 1
  • 1
Sami Eltamawy
  • 9,874
  • 8
  • 48
  • 66
  • I've fixed your code to do what you meant it to do, the result char[] has the right sequence of chars, but when I new String(result) --> it twists the string once again. – Miko Diko May 10 '15 at 06:31
  • I'm not quite sure, after calling generatePath() , how do I convert the char[] into a "good" String ? – Miko Diko May 10 '15 at 07:02
  • use `new String(generatePath()a,b)` @MikoDiko – Sami Eltamawy May 10 '15 at 07:09
  • You have to be more specific @MikoDiko to allow us help you if you want. And also don;t forget if you find any or our answers somehow useful for you, vote it up as a appreciation for the time – Sami Eltamawy May 10 '15 at 07:21
  • added a small piece of info to the main post -- > Just to make it clear, the non-English language is written Right-to-Left, unlike English which is Left-to-Right – Miko Diko May 10 '15 at 07:23