2

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?

2

5 Answers 5

24

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.

21

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
4
  • 1
    You need to go through a regex tutorial. The comments field is too little to explain everything. The first part, i.e. ".*/" matches everything until the last /, then comes grouping (\\W+) which means a at least one word-character then it stops at . because dot is not a word character. The last part again .* means everything. In the replacement part I just refer to the group by its number $1. Since there is one group (\\w+) I use $1. Here is the regex api for java: docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
    – user508434
    Commented May 9, 2014 at 9:40
  • 2
    This will fail on e.g. "foo.exe" (no slash) and "/foo/bar.baz.exe" (two dots).
    – johncip
    Commented May 21, 2014 at 8:41
  • 1
    ...and "/2025-08-06.log" (or anything else with punctuation in the file name)
    – johncip
    Commented May 23, 2014 at 9:07
  • 1
    The answer is to the given example in the question. For numeric and other types of values, you will need to use appropriate character class.
    – user508434
    Commented May 23, 2014 at 19:53
8

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"
4
  • The regex will only return the first segment for filenames with more than one dot, e.g. "core/pages/viewemployee.jsff.bak".
    – johncip
    Commented May 23, 2014 at 9:10
  • You're right but even if that is the case, by this code he gets the filename without the extension. Even if that file is a backup file, he will still get the filename, "viewemployee", which he desires to get. Commented May 23, 2014 at 11:12
  • Well, typically, at least for program associations, the extension is whatever follows the last dot, not the first. And outside of those there really isn't a concept of extension as far as most filesystems are concerned. But I'll grant you that it's not really clear what the OP wants.
    – johncip
    Commented May 23, 2014 at 11:26
  • @johncip This answer got upvoted so I came in and saw that you are right, my answer should be updated. Commented Jun 10, 2016 at 20:36
8

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 ..

0
6

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.