tokenizing a string with escape sequence
hello..
i'm trying to use string tokenizer on a string contains directory of a particular file..
for example, StringTokenizer stringTokenizer = new StringTokenizer("C:\Documents\image.jpg", ":\");
but i got error message from Eclipse : invalid escape sequence
is there a way to solve this?
thank you in advance.
Re: tokenizing a string with escape sequence
That is because \D and \i are not valid escape sequences.
The valid ones are:
Also note that since \" is, then ":\" will be seen as a missing quote to end the string
Re: tokenizing a string with escape sequence
Quote:
Originally Posted by
nosxlimit
... solve this....
The backslash is a very special character in Java strings (and in regular expressions, in general).
If you want a String to have a backslash, and you are initializing with a string literal (something in quotes " "), then inside the quotes, the backslash must be written as two backslashes.
So: For everything you have between quote marks, replace \ by \\:
Cheers!
Z
Re: tokenizing a string with escape sequence
Quote:
Originally Posted by
jps
That is because \D and \i are not valid escape sequences.
The valid ones are:
Also note that since \" is, then ":\" will be seen as a missing quote to end the string
Quote:
Originally Posted by
Zaphod_b
The backslash is a very special character in Java strings (and in regular expressions, in general).
If you want a String to have a backslash, and you are initializing with a string literal (something in quotes " "), then inside the quotes, the backslash must be written as two backslashes.
So: For everything you have between quote marks, replace \ by \\:
Cheers!
Z
thanks for the reply for both of you..
the thing is, i can't just change the backslash into double backslash because i'm obtaining the string by using string = file.getAbsolutePath();..
so is there another way, maybe? :-/
Re: tokenizing a string with escape sequence
Quote:
Originally Posted by
nosxlimit
...another way, maybe...
My response was an attempt to resolve a specific problem with the example that you posted. I mean, I tried to answer the question you asked. My crystal ball is cloudy today (marine layer down to ground level) and I probably won't have much luck with the question(s) you don't ask.
Anyhow...
As I mentioned, the double-backslash thing is for string literals (constant sequences of characters between quotation marks). Once it's in the String, it's in the String. How the String is used is another subject all together.
Post a complete program that you are trying to use that gives a specific problem.
Tell us what happens when you compile. (If there are compiler error messages, paste them into the post exactly as they appear. Don't paraphrase.)
If it compiles OK but doesn't execute properly, then tell us exactly what happened.
Cheers!
Z
Re: tokenizing a string with escape sequence
Quote:
Originally Posted by
nosxlimit
...i can't just change the backslash into double backslash...
Why not?
Code JAVA:
String filePathModified = myFilePathEditingMethod(filePathOriginal);
private String myFilePathEditingMethod(String filePathToBeDoubleSlashed){
//look for\
//if \ found, add extra \
//return editedVersionOfString
}
This should give you a push in the right direciton, though I wouldn't try to implement the method as it is drawn up here.
Re: tokenizing a string with escape sequence
Quote:
Originally Posted by
jps
...
Code java:
//look for\
//if \ found, add extra \
//return editedVersionOfString
}
Why the heck would you want to put extra stuff in the string? What possible value would that add?
Why not just something like
Code java:
// For test purposes we give it the path.
// A "real" program at this point would just be given the File object
// and could use getAbsolutePath() to discover the complete path.
//
String fileName = "F:\\home\\Zaphod\\java\\narrative.txt";
File infile = new File(fileName);
String str = infile.getAbsolutePath();
System.out.printf("str: %s\n", str);
StringTokenizer st = new StringTokenizer(str, ":\\");
while(st.hasMoreTokens())
{
System.out.printf("Token: %s\n", st.nextToken());
}
Output
str: F:\home\Zaphod\java\narrative.txt
Token: F
Token: home
Token: Zaphod
Token: java
Token: narrative.txt
Re: tokenizing a string with escape sequence
Quote:
Originally Posted by
Zaphod_b
Why the heck would you want to...
Quote:
Originally Posted by
jps
...
This should give you a push in the right direciton, though I wouldn't try to implement the method as it is drawn up here.
Because I don't intend to spoonfeed answers, but give something to think about.
Re: tokenizing a string with escape sequence
Quote:
Originally Posted by
jps
Why not?
Code JAVA:
String filePathModified = myFilePathEditingMethod(filePathOriginal);
private String myFilePathEditingMethod(String filePathToBeDoubleSlashed){
//look for\
//if \ found, add extra \
//return editedVersionOfString
}
This should give you a push in the right direciton, though I wouldn't try to implement the method as it is drawn up here.
i tried to do what you're suggesting, but intead of adding extra '\', i think it's better to replace it with something else (easier to be tokenized later on)..
so i used replace.. but still, it give me error because of the single backslash.. :-/
Quote:
Originally Posted by
Zaphod_b
Why the heck would you want to put extra stuff in the string? What possible value would that add?
Why not just something like
Code java:
// For test purposes we give it the path.
// A "real" program at this point would just be given the File object
// and could use getAbsolutePath() to discover the complete path.
//
String fileName = "F:\\home\\Zaphod\\java\\narrative.txt";
File infile = new File(fileName);
String str = infile.getAbsolutePath();
System.out.printf("str: %s\n", str);
StringTokenizer st = new StringTokenizer(str, ":\\");
while(st.hasMoreTokens())
{
System.out.printf("Token: %s\n", st.nextToken());
}
Output
str: F:\home\Zaphod\java\narrative.txt
Token: F
Token: home
Token: Zaphod
Token: java
Token: narrative.txt
How if the obtained string is like below (single backslash)?
Code java:
String fileName = "F:\home\Zaphod\java\narrative.txt";
Re: tokenizing a string with escape sequence
thanks Zaphod_b and jps..
thanks for replying..
i think i will just use the simple one, which is save the image temporarily, use it, then delete it again.. hehe..
thank you.. :)