当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Java split giving opposite order of arabic characters

问题描述:

I am splitting the following string using \\| in java (android) using the IntelliJ 12 IDE.

Everything is fine except the last part, somehow the split picks them up in the opposite order :

As you can see the real positioning 34,35,36 is correct and according to the string, but when it gets picked out into split part no 5 its in the wrong order, 36,35,34 ...

Any way I can get them to be in the right order?

My Code:

public ArrayList<Book> getBooksFromDatFile(Context context, String fileName)

{

ArrayList<Book> books = new ArrayList<Book>();

try

{

// load csv from assets

InputStream is = context.getAssets().open(fileName);

try

{

BufferedReader reader = new BufferedReader(new InputStreamReader(is));

String line;

while ((line = reader.readLine()) != null)

{

String[] RowData = line.split("\\|");

books.add(new Book(RowData[0], RowData[1], RowData[2], RowData[3], RowData[4], RowData[5]));

}

}

catch (IOException ex)

{

Log.e(TAG, "Error parsing csv file!");

}

finally

{

try

{

is.close();

}

catch (IOException e)

{

Log.e(TAG, "Error closing input stream!");

}

}

}

catch (IOException ex)

{

Log.e(TAG, "Error reading .dat file from assets!");

}

return books;

}

网友答案:

The characters in the String should always be in linguistic order, regardless of whether they're right-to-left or left-to-right characters. So we should see [34] = '١', [35] = '-', [36] = '٧'. It is up to rendering engines to display them using the correct right-to-left or left-to-right layout.

网友答案:

In Unicode world, there are strong and weak characters. These are the list of weak characters:

 "\\", "/", "+", "-", "=", ";", "$" 

They are called "weak" characters because they do not contain any directional information. So it is up to the software to decide in which direction these "weak" characters will be placed, more info here. To fix this issue you need to set the Directional Formatting, for example:

RightToLeftEmbedding + weakCharacter + PopDirectionalFormatting

using these constant values

 char RightToLeftEmbedding = (char)0x202B;
 char PopDirectionalFormatting = (char)0x202C;
分享给朋友:
您可能感兴趣的文章:
随机阅读: