file path Windows format to java format

后端 未结 5 738
走了就别回头了
走了就别回头了 2020-11-30 03:49

I need to convert the file path in windows say C:\\Documents and Settings\\Manoj\\Desktop for java as C:/Documents and Settings/Manoj/Desktop .

Is there any utilit

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 04:35

    String path = "C:\\Documents and Settings\\Manoj\\Desktop";
    String javaPath = path.replace("\\", "/"); // Create a new variable
    

    or

    path = path.replace("\\", "/"); // Just use the existing variable
    

    Strings are immutable. Once they are created, you can't change them. This means replace returns a new String where the target("\\") is replaced by the replacement("/"). Simply calling replace will not change path.

    The difference between replaceAll and replace is that replaceAll will search for a regex, replace doesn't.

提交回复
热议问题