Splitting a string using "\" in android

I am trying to split the string into "\", but it does not work for me, the string contains alphanumeric data and some Japanese characters.

Here is the code I'm trying

String [] folder = null;
String [] files= null;
for (int i =0; i<listFile_Names.size();i++) 
{

String  filesList = listFile_Names.get(i);
filesList = filesList.substring(1);
Log.v("Fullpath",filesList );
try{
String[] parts = filesList.split("\\");
folder[i] = parts[0]; 
files[i] = parts[1]; 
}
catch(Exception e){
e.printStackTrace();

}
}
for(int j=0; j< folder.length; j++)
{
Log.v("Folders", folder[j].toString());
Log.v("Files", files[j].toString());

}

Here's what LogCat says

06-11 11:19:03.300: V/Fullpath(14600): A40-002(D155AX-8_DUAL仕様チェックシート)\sheet001.html
06-11 11:19:03.300: W/System.err(14600): java.util.regex.PatternSyntaxException: Unrecognized backslash escape sequence in pattern near index 1:
06-11 11:19:03.300: W/System.err(14600): \
06-11 11:19:03.300: W/System.err(14600):  ^
06-11 11:19:03.300: W/System.err(14600):    at java.util.regex.Pattern.compileImpl(Native Method)
06-11 11:19:03.300: W/System.err(14600):    at java.util.regex.Pattern.compile(Pattern.java:400)
06-11 11:19:03.300: W/System.err(14600):    at java.util.regex.Pattern.<init>(Pattern.java:383)
06-11 11:19:03.300: W/System.err(14600):    at java.util.regex.Pattern.compile(Pattern.java:374)
06-11 11:19:03.300: W/System.err(14600):    at java.lang.String.split(String.java:1842)
06-11 11:19:03.300: W/System.err(14600):    at java.lang.String.split(String.java:1823)
06-11 11:19:03.300: W/System.err(14600):    at jp.co.komatsu.android.xlez.webservice.AsynTaskGetUpdatedFiles.doInBackground(AsynTaskGetUpdatedFiles.java:202)
+4
source share
6 answers

Since the backslash is particularly significant in a regular expression, you need to avoid it twice:

String[] parts = filesList.split("\\\\");

This is due to the fact that Java interprets a string literal \\like \. You can easily see this execution of this code:

String s = "123\\";
System.out.println(s); // Output: 123\
+10
source

, 4 , 2, , .

String[] arrA = str.split("\\\\");
+2

filesList.split("\\");

filesList.split("\\\\");

+1

int idx = filesList.replaceAll("\\", "/").lastIndexOf("/");
String fileNameWithExtn =  idx >= 0 ? filesList.substring(idx + 1) : fileName;

To remove the file extension and get only the file path , similarly

idx = fileNameWithExtn .lastIndexOf(".");
String filename = idx >= 0 ? fileNameWithExtn.substring(0,idx) : fileNameWithExtn ;
0
source

Try with the template,

String[] input = { "320/10.50/Dec 11 2002/39.95",
        "110-4.25-Dec 15 2002-39.95",
        "8%54.00%Dec 4 2002%0" };

    public SplitData() {
        for (int i = 0; i < input.length; i++) {
            String[] piece = input[i].split("[-/%]");
            for (int j = 0; j < piece.length; j++)
                System.out.print(piece[j] + "\t");
            System.out.print("\n");
        }
0
source

if you want to replace only one standing backslash, try this.

String url = "http://192.168.1.5:8080\smth";
if (url .contains("\\")){
  url = url .replaceAll("\\\\", "/");
}
0
source

Source: https://habr.com/ru/post/1544098/


All Articles