String Tokenizer Problem

Hello everyone,

I have this string:

String frase ="c:\\movies\\animals\\crazy cat.avi";

What I want is to get as a final string, in this case, "crazy cat.avi". The problem is that the String Tokenizer counts the spaces as well...

Please help me creating a cycle that looks for the the last "\" and creates a string from then on.

Thanks for any help you might provide!

XonaDick

[419 byte] By [xonadicka] at [2007-9-29 21:05:57]
# 1

No need to use a string tokenizer for this task.

Different operating system but you get the idea.

$ cat Test.java

import java.io.*;

public class Test

{

public static void main(String[] args)

{

File f = new File("/var/tmp/some file.txt");

System.out.println(f.getName());

}

}

$ javac Test.java

$ java Test

some file.txt

ectosphenoa at 2007-7-16 1:18:47 > top of Java-index,Other Topics,Algorithms...
# 2
Hi,Thanks for the help, but I really need the input from a String rather from a file. If it were to be from a file your reply would be perfect.Thanks anyway!XonaDickPS - Anyone one else can help?
xonadicka at 2007-7-16 1:18:47 > top of Java-index,Other Topics,Algorithms...
# 3

StringTokenizer st = new StringTokenizer(myString,"\\");

then get the last token with something like

String iWant = null;

int tokens = st.countTokens();

int c=0;

while(c<tokens-1) iWant = st.nextToken();

or something like this.>

rafaelsantosa at 2007-7-16 1:18:47 > top of Java-index,Other Topics,Algorithms...
# 4
> Thanks for the help, but I really need the input from> a String rather from a file. If it were to be from a> file your reply would be perfect.The file doesn't have to actually exist. Still gives youthe right answer.:)
ectosphenoa at 2007-7-16 1:18:47 > top of Java-index,Other Topics,Algorithms...
# 5
Perfect!Thank you Rafael! Or should I say Obrigado?Thanks!XonaDick
xonadicka at 2007-7-16 1:18:48 > top of Java-index,Other Topics,Algorithms...
# 6
De nada :-)Just to be sure, I guess the other answer (using File) was also correct - wonder what's the performance of both since I suggested using a loop...
rafaelsantosa at 2007-7-16 1:18:48 > top of Java-index,Other Topics,Algorithms...
# 7
frase.substring(frase.lastIndexOf('\\')+1)Will do it too.Pete
Pete_Kirkhama at 2007-7-16 1:18:48 > top of Java-index,Other Topics,Algorithms...
# 8
> frase.substring(frase.lastIndexOf('\\')+1)> ill do it too.The File method has the advantage of working regardless of what your path seperator character happens to be on your current platform. Not everyone runs Windows you know.
ectosphenoa at 2007-7-16 1:18:48 > top of Java-index,Other Topics,Algorithms...
# 9
I like the File approach but here is "yet another method (tm)"String[] results = frase.split("\\\\"); // may need a few more \\'s in thereString answer = results[results.length-1];
matfuda at 2007-7-16 1:18:48 > top of Java-index,Other Topics,Algorithms...
# 10

> The File method has the advantage of working regardless of what your path seperator character

> happens to be on your current platform. Not everyone runs Windows you know.

Odd, I'm running OS X, and get the following:import java.io.File;

public class LastName {

public static String lastName1 (String str) {

return new File(str).getName();

}

public static String lastName2 (String str) {

String[] parts = str.split("\\\\ "); // added extra space so rest of post doesn't go blue

return parts[parts.length-1];

}

public static String lastName3 (String str) {

return str.substring(str.lastIndexOf('\\')+1);

}

public static void main(String[] args) {

String frase ="c:\\movies\\animals\\crazy cat.avi";

System.out.println(lastName1(frase));

System.out.println(lastName2(frase));

System.out.println(lastName3(frase));

}

}

pete% java LastName

c:\movies\animals\crazy cat.avi

crazy cat.avi

crazy cat.avi

So mine and matfud's methods generate the specified output from the specified input when not on Windows, whereas using the File class doesn't. For all we know he's running VMS and has to process a file that someone on Windows mailed him.

If the OP had asked for way of splitting a file name from the path in the current OS, I'd have posted frase.substring(frase.lastIndexOf(File.pathSeparatorChar)+1)

> was also correct - wonder what's the performance of both since I suggested using a loop...

Testing the File version, the substring version and the split version yields the following:

Filemean of (10 tests, excluding 6 outliers): 2259.446756ms

Splitmean of (10 tests, excluding 6 outliers): 8568.243067ms

Substring mean of (10 tests, excluding 6 outliers): 612.146675ms

each test being 1000000 calls to the method, so about 10% confidence.

Pete

Pete_Kirkhama at 2007-7-16 1:18:48 > top of Java-index,Other Topics,Algorithms...
# 11

It does not surprise me that lastIndexOf is the fastest. split is a regexp and

there is a lot of work to be done internally. It is strange that File does not

work though. I would have thought that File would have methods to do just thing

on all platforms in a uniform manner.

matfud

matfuda at 2007-7-16 1:18:48 > top of Java-index,Other Topics,Algorithms...
# 12

File splits up the path with the OS's path separator-the OP asked to split with a specific char, that happens to be the Windows path separator. As I'm not on Windows, then the results for using File.getName() on the string in the OP aren't what they would be on Windows.

If the string had used the unix path separator, then File would 'work' on my system, and also (IIRC) on Windows as Java automatically converts / to \ in the Win runtime. The OS specific behaviour that File gives may be what the OP wanted, or maybe not.

Pete

Pete_Kirkhama at 2007-7-16 1:18:48 > top of Java-index,Other Topics,Algorithms...
# 13

> Odd, I'm running OS X, and get the

> following:

I'd suppose thats because the file, "c:\\whatever" isn't

valid on your os ... (i.e. File.exists() would fail) ... the File

class doesn't seem (other than the 'normalize' function

which is native) to check the validitiy of the filename you

pass it for you os.

silk.ma at 2007-7-16 1:18:48 > top of Java-index,Other Topics,Algorithms...