Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/377.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java需要帮助吗_Java - Fatal编程技术网

Java需要帮助吗

Java需要帮助吗,java,Java,嘿,伙计,我正在尝试替换所有字符和数字,以获得/hello/what/仅在没有REMOVEThis4.PNG的情况下,我不想使用string.replacetremovethis4.PNG;因为我想把它用在其他琴弦上 任何帮助都是我的代码 String sFile = "/hello/what/REMOVEThis4.PNG"; if (sFile.contains("/")){ String Replaced = sFile.replaceAll("(?s)", ""); Sy

嘿,伙计,我正在尝试替换所有字符和数字,以获得/hello/what/仅在没有REMOVEThis4.PNG的情况下,我不想使用string.replacetremovethis4.PNG;因为我想把它用在其他琴弦上

任何帮助都是我的代码

String sFile = "/hello/what/REMOVEThis4.PNG";
if (sFile.contains("/")){
    String Replaced = sFile.replaceAll("(?s)", "");
    System.out.println(Replaced);
}
我希望输出是

/你好/什么/


非常感谢

如果您试图解析路径,我建议查找/的最后一个索引,并将此索引的子字符串加上一。所以

string = string.substring(0, string.lastIndexOf("/") + 1);

在您的情况下不需要使用正则表达式:

String sFile = "/hello/what/REMOVEThis4.PNG";
// TODO check actual last index of "/" against -1
System.out.println(sFile.substring(0, sFile.lastIndexOf("/") + 1));
输出

在处理实际文件的情况下,您可能不用再进行字符串操作,而是使用File.getParent:

输出可能会因系统而异

\hello\what
使用Java的文件API:


我正要提出这个建议,但你抢先一步。如果你说使用文件API,你本可以使用它:Hint:File.getParent。使用这个文件如何比使用substring/lastIndexOf技术的其他答案更好?@creditenson首先,你获得了平台独立性;第二,无论如何,您希望路径访问实际文件。
File file = new File("/hello/what/REMOVEThis4.PNG");
System.out.println(file.getParent());
\hello\what
String example = "/hello/what/REMOVEThis4.PNG";
File file = new File(example);
System.out.println(example);

String absolutePath = file.getAbsolutePath();
String filePath = absolutePath.substring(0, absolutePath.lastIndexOf(File.separator));
System.out.println(filePath);