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?
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?
Suppose that you have that string saved in a variable named myString
.
String myString = "core/pages/viewemployee.jsff";
String newString = myString.substring(myString.lastIndexOf("/")+1, myString.indexOf("."));
But you need to make the same control before doing substring
in this one, because if there aren't those characters you will get a "-1" from lastIndexOf()
, or indexOf()
, and it will break your substring
invocation.
I suggest looking for the Javadoc documentation.
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+).*","$1");
System.out.println(str); //prints viewemployee
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.
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"
These are file paths. Consider using File.getName(), especially if you already have the File object:
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"
.
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 .
.
The below will get you viewemployee.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:
idx = fileNameWithExtn.lastIndexOf(".");
String filename = idx >= 0 ? fileNameWithExtn.substring(0,idx) : fileNameWithExtn;
什么是孤独症 | 晚上起夜尿多吃什么药 | 秦皇岛有什么特色美食 | 梦见玉米是什么意思 | 血象高是什么原因 |
ace是什么 | 四月28日是什么星座 | 叻叻是什么意思 | 忠实的什么 | 铁棍山药有什么功效 |
什么的狮子 | 肌酸激酶高是什么病 | 三维彩超和四维彩超有什么区别 | 湖北有什么山 | 天上的云像什么 |
冰箱eco是什么意思 | 谛听是什么意思 | 儿童结膜炎用什么眼药水 | 男人左眼皮跳是什么预兆 | 阿莫西林吃多了有什么副作用 |
发什么大成语hcv9jop0ns4r.cn | 孕反什么时候结束hlguo.com | 喝什么降血压hcv8jop9ns3r.cn | 共振是什么意思hcv9jop3ns9r.cn | 待寝什么意思hcv8jop7ns4r.cn |
人生苦短什么意思hcv8jop4ns7r.cn | 鱿鱼属于什么类hcv9jop5ns0r.cn | 嘴苦是什么原因引起的hcv8jop6ns7r.cn | cocoon是什么品牌hcv8jop4ns2r.cn | 什么是等位基因shenchushe.com |
今年高温什么时候结束hcv8jop3ns0r.cn | 什么不绝hcv9jop7ns3r.cn | 5月9号是什么星座hcv8jop2ns6r.cn | 血浓稠是什么原因引起的hcv8jop0ns2r.cn | 为什么会突然晕倒hcv8jop9ns7r.cn |
咳嗽吃什么药最好hcv9jop6ns3r.cn | 多囊性改变是什么意思hcv9jop6ns5r.cn | 感冒嗓子疼吃什么药hcv9jop3ns7r.cn | 水印是什么意思hcv9jop2ns7r.cn | 着相什么意思hcv8jop1ns6r.cn |