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
.But you need to make same control before doing
substring
in this one because, if there aren't those characters you will get fromlastIndexOf()
, orindexOf()
a "-1" that will break yoursubstring
invocation.I suggest to you to look for the Javadoc documentation.
These are file paths, right? Consider using File.getName(), especially if you already have the File object:
And to remove the extension:
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
.
.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.
Below will get you viewemployee.jsff
To remove the file Extension and get only viewemployee, similarly
You can solve this with regex (given you only need a group of word characters between the last "/" and "."):