如何在Java中.(点)之前和/(最后一个)斜杠之后获取字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23557870/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get string before .(dot) and after /(last) slash in Java?
提问by user3619225
I have a string like this:
我有一个这样的字符串:
"core/pages/viewemployee.jsff"
From this code, I need to get "viewemployee". How do I get this using Java?
从这段代码中,我需要获得“viewemployee”。我如何使用 Java 获得这个?
回答by Mehmet Sedat Güng?r
You can split the string first with "/" so that you can have each folder and the file name got separated. For this example, you will have "core", "pages" and "viewemployee.jsff". I assume you need the file name without the extension, so just apply same split action with "." seperator to the last token. You will have filename without extension.
您可以先用“/”分割字符串,这样您就可以将每个文件夹和文件名分开。对于此示例,您将拥有“core”、“pages”和“viewemployee.jsff”。我假设您需要没有扩展名的文件名,因此只需使用“。”应用相同的拆分操作。分隔符到最后一个标记。您将拥有没有扩展名的文件名。
String myStr = "core/pages/viewemployee.bak.jsff";
String[] tokens = myStr.split("/");
String[] fileNameTokens = tokens[tokens.length - 1].split("\.");
String fileNameStr = "";
for(int i = 0; i < fileNameTokens.length - 1; i++) {
fileNameStr += fileNameTokens[i] + ".";
}
fileNameStr = fileNameStr.substring(0, fileNameStr.length() - 1);
System.out.print(fileNameStr) //--> "viewemployee.bak"
回答by Mehmet Sedat Güng?r
You can solve this with regex (given you only need a group of word characters between the last "/" and "."):
您可以使用正则表达式解决此问题(假设您只需要最后一个“/”和“.”之间的一组单词字符):
String str="core/pages/viewemployee.jsff";
str=str.replaceFirst(".*/(\w+).*","");
System.out.println(str); //prints viewemployee
回答by Nishanthi Grashia
Below will get you viewemployee.jsff
下面会让你查看employee.jsff
int idx = fileName.replaceAll("\", "/").lastIndexOf("/");
String fileNameWithExtn = idx >= 0 ? fileName.substring(idx + 1) : fileName;
To remove the file Extension and get only viewemployee, similarly
要删除文件扩展名并仅获取viewemployee,类似地
idx = fileNameWithExtn .lastIndexOf(".");
String filename = idx >= 0 ? fileNameWithExtn.substring(0,idx) : fileNameWithExtn ;
回答by Alessandro Suglia
Suppose that you have that string saved in a variable named myString
.
假设您将该字符串保存在名为 的变量中myString
。
String myString = "core/pages/viewemployee.jsff";
String newString = myString.substring(myString.lastIndexOf("/")+1, myString.indexOf("."));
But you need to make same control before doing substring
in this one because, if there aren't those characters you will get from lastIndexOf()
, or indexOf()
a "-1" that will break your substring
invocation.
但是您需要在执行substring
此操作之前进行相同的控制,因为如果没有您将从中获得的那些字符lastIndexOf()
,或者indexOf()
会中断您的substring
调用的“-1” 。
I suggest to you to look for the Javadoc documentation.
我建议您查找Javadoc 文档。
回答by johncip
These are file paths, right? Consider using File.getName(), especially if you already have the File object:
这些是文件路径,对吧?考虑使用 File.getName(),特别是如果你已经有了 File 对象:
File file = new File("core/pages/viewemployee.jsff");
String name = file.getName(); // --> "viewemployee.jsff"
And to remove the extension:
并删除扩展名:
String res = name.split("\.[^\.]*$")[0]; // --> "viewemployee"
With this we can handle strings like "../viewemployee.2.jsff"
.
有了这个,我们可以处理像"../viewemployee.2.jsff"
.
The regex matches the last dot, zero or more non-dots, and the end of the string. Then String.split() treats these as a delimiter, and ignores them. The array will always have one element, unless the original string is .
.
正则表达式匹配最后一个点、零个或多个非点以及字符串的结尾。然后 String.split() 将这些视为分隔符,并忽略它们。该数组将始终只有一个元素,除非原始字符串是.
。