Split the string on forward slash

后端 未结 4 1759
傲寒
傲寒 2020-12-04 01:47

I have a code which I wanted to split based on the forward slash \"/\".

Whenever I have a regex split based on \"////\" it never splits and gives me the whole string

相关标签:
4条回答
  • 2020-12-04 01:57

    Java might return a null pointer so you need to wrap this with a try catch

    try {
            String[] temp = imageFilenameOriginal.split("/");
    
        } catch (Exception ex){
            errorMessage = ex.getMessage();
        }
    

    The compiler is much happier this way.

    0 讨论(0)
  • 2020-12-04 02:03

    There is no need to escape forward slashes. Your code works fine if you just do:

    String[] paths = path.split("/");
    
    0 讨论(0)
  • 2020-12-04 02:05
    String[] paths = path.split("\\");
    
    0 讨论(0)
  • 2020-12-04 02:10

    i wanted to check validation of input date in the format dd/mm/yyyy so need to split my string around / You can do it simply by:

    String spl[]=str.split("/");
    int date=Integer.parseInt(spl[0]);
    int month=Integer.parseInt(spl[1]);
    int year=Integer.parseInt(spl[2]);
    
    0 讨论(0)
提交回复
热议问题