This discussion is archived
1 2 Previous Next 26 Replies Latest reply: Jan 15, 2008 9:05 AM by 3004 Go to original post
• ###### 15. Re: Reading a String Literally - Finding the "\" Character with a Regex
Currently Being Moderated
Martin_J wrote:
jverd,

String text

oops...
String text = "Temp\temp.txt"
System.out.println(text)

yields...

Temp emp.txt

And

System.out.println("Temp\temp.txt")

yields

Temp emp.txt

Edited by: Martin_J on Jan 14, 2008 11:38 PM
Yes, because those are string literals. You didn't read my first post clearly enough.

Again, what exactly are you trying to accomplish?
• ###### 16. Re: Reading a String Literally - Finding the "\" Character with a Regex
Currently Being Moderated
Martin_J wrote:
The question is not whether I want a "\" in the string, but whether there is a "\" in the string.
Why? What are you trying to accomplish? If a string comes from reading a file or user input or something and it has a \ in it, it's NOT an escape.
So, how can I write code that splits the string at the right places if it does not contain a "\\" but a "\"? Paraphrased, I want the regex to look for a "\", but not to interpret it as an escape character.
What are you trying to accomplish? Splitting a string on "\"?
String str = "abc\\def\\ghi\\ttt"; // String holds abc\def\ghi\ttt, no tab characters, same as if user input was abc\def\ghi\ttt
String[] pieces = str.split("\\\\"); // ["abc", "def", "ghi", "ttt"]
Edited by: jverd on Jan 15, 2008 9:17 AM
• ###### 17. Re: Reading a String Literally - Finding the "\" Character with a Regex
Currently Being Moderated
Martin_J wrote:
Thanks Darryl,

How come BSTest returns "Temp\test.txt" (which it should), but System.out.println("Temp\test.txt") returns..

Temp emp.txt

??
Again, it's the difference between a string literal in Java source code and a String object.

If you prompt the user for input and he types 'a' '\' 't', the String object created will be a\t, three characters, no tab.

Likewise, if you have a string literal in your Java code that's
"a\\t"
the String object that's created will be b]a\t, three characters, no tab.

If you have the string literal
"a\t"
then the String object that's created will be two characters--'a' and a tab.

\ is an escape in string literals in Java source code, NOT in String objects in the VM.
• ###### 18. Re: Reading a String Literally - Finding the "\" Character with a Regex
Currently Being Moderated
Thanks to all the folks how have posted above, but I'm still struggling.

To recap, and to respond to JVerd, I am trying to find a way to test if there is a "\" in a string when the context says that it is not intended to be an escape character. In the example, "Temp\text.txt", the "\" is intended to be a filepath separator.

So, I thought, lets set up a regex to look for "\" in a string and lets test it to see if it gives the desired result. I can then use the regex to split "Temp\text.txt" into the right names for a directory and a file. For example...

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BackSlashTest {
public static void main (String [] args) {
//Input a test string
String testString1     = "Temp\temp.txt";
String testString2     = "Temp/temp.txt";
String backSlashString     = "\\\\|\\/";

//Set up a Regex Pattern
Pattern backSlash          = Pattern.compile(backSlashString);
Matcher backSlashMatch1     = backSlash.matcher(testString1);
Matcher backSlashMatch2     = backSlash.matcher(testString2);

System.out.println("Does testString1 include a " + backSlashString + " string? " + backSlashMatch1.find());
System.out.println("Does testString2 include a " + backSlashString + " string? " + backSlashMatch2.find());
}
}
However, I dont get the 'desired' result because the regex does not 'see' a single "\" character, it 'sees' an escape and a nmenomic "\t", which it inerpets as a tab, thus returning 'false' from find().

Perhaps I cant use testString1 as written above. If I read it in from a text file, will Java 'insert' the 'extra' "\" so that it conforms with the "Temp\\temp.txt" convention as suggested in previous posts? I had just assumed that I could write it into the String definition in the same way that I would write it into a text file, i.e. "Temp\temp.txt".
• ###### 19. Re: Reading a String Literally - Finding the "\" Character with a Regex
Currently Being Moderated
Martin_J wrote:
Thanks to all the folks how have posted above, but I'm still struggling.

To recap, and to respond to JVerd, I am trying to find a way to test if there is a "\" in a string
if (str.indexOf('\\')) != 0)
OR
if (str.contains("\\"))
when the context says that it is not intended to be an escape character.
That is irrelevant. The string contains a \ or it does not.
In the example, "Temp\text.txt", the "\" is intended to be a filepath separator.
You still seem to not be making the distinction between a source-file string literal and a runtime String object. I cannot help you until you do.
• ###### 20. Re: Reading a String Literally - Finding the "\" Character with a Regex
Currently Being Moderated
Martin_J wrote:
Thanks to all the folks how have posted above, but I'm still struggling.

To recap, and to respond to JVerd, I am trying to find a way to test if there is a "\" in a string when the context says that it is not intended to be an escape character. In the example, "Temp\text.txt", the "\" is intended to be a filepath separator.

So, I thought, lets set up a regex to look for "\" in a string and lets test it to see if it gives the desired result. I can then use the regex to split "Temp\text.txt" into the right names for a directory and a file. For example...

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BackSlashTest {
public static void main (String [] args) {
//Input a test string
String testString1     = "Temp\temp.txt";
That String does not contain a backslash.
• ###### 21. Re: Reading a String Literally - Finding the "\" Character with a Regex
Currently Being Moderated
Java is not Perl. IMHO there is no way to redefine the intentional semantics of the backspace character. In an literal string, an introductory backspace is interpreted as an escape character. You have to use 2 backspaces to yield 1 in your memory model. It's that easy.
• ###### 22. Re: Reading a String Literally - Finding the "\" Character with a Regex
Currently Being Moderated
Sorry, backslash vs. backspace
• ###### 23. Re: Reading a String Literally - Finding the "\" Character with a Regex
Currently Being Moderated
So the user puts in a string, possibly including a backslash. And you need to check to see where it exists, and keep its position if it does?
import java.io.*;

class Test {
public static void main(String[] args) {
Console console = System.console();
System.out.println(testString);

for (int i = 0; i < testString.length(); i++) {
if (testString.charAt(i) == '\\')
System.out.println("found");
}
}
}
}
Something like that?

Edited by: newark on Jan 16, 2008 11:43 AM
• ###### 24. Re: Reading a String Literally - Finding the "\" Character with a Regex
Currently Being Moderated
Martin, let me to explain what others have said above in a different way.

The problem you are having is with the way you try to test your program using String literals in Java code.

When you type a String literal value in Java source code, you have to use escape characters to enter certain special characters. Once the source has been compiled, those escape characters are gone: the escape sequence has been replaced by the compiler with a single character.

So when your Java source code contains
String string = "somes\tring";
there is NO '\' character in the String object. There is no 't' character in the String either. The \t in represents a single "tab" character. Only String literals inside Java source code have to be escaped this way. I repeat: this issue only exists in Java source code.

If you retrieve a String in any other way (e.g. when you read text from a file, when you read user input from a JTextField, or when you parse command line parameters) those escape characters DO NOT APPLY.

So when you read the text "somes\tring" from a file, or when the user enters that text into a JTextField, the String object you get WILL contain both a '\' character and a 't' character. Only when YOU (the programmer) enter that text into Java source code do you have to worry about escape characters.

In other words: the text
somes\tring
is represented in Java source code by the String literal
"somes\\tring"
.

Hope this helps.
• ###### 25. Re: Reading a String Literally - Finding the "\" Character with a Regex
Currently Being Moderated
Herko_ter_Horst wrote:
If you retrieve a String in any other way (e.g. when you read text from a file, when you read user input from a JTextField, or when you parse command line parameters) those escape characters DO NOT APPLY.
I don't want to further confuse the issue, but the OP may encounter this, so I'll try to explain one subtle point here.

In some unix shells, if you type
java MyClass a\tb
the shell may interpret the backslash as an escape character, so the string that gets passed to the VM is a"tab"b. Note that this is the shell escaping \t as a tab character, before Java ever even sees it.

On the other hand, if you type
java MyClass 'a\tb'
or possibly
java MyClass a\\tb
then the shell does NOT escape \t as a tab, and what the VM sees is a\tb, which is four characters: 'a', '\', 't', 'b' and no escape, no tab.

The point is that in certain contexts, before the VM sees the string, the \ may be an escape, but in String objects in the VM, it is not.

Edited by: jverd on Jan 16, 2008 1:57 PM

Edited by: jverd on Jan 16, 2008 1:59 PM
• ###### 26. Re: Reading a String Literally - Finding the "\" Character with a Regex
Currently Being Moderated
Yup, I think that answers the question (or the question I should have asked at the start)

I was tripping up on an assumption that typing "Temp\temp.txt" in a text editor was the same as typing it into Java code. As the above posts point out, "Temp\temp.txt" in NotePad (for example) == "Temp\\temp.txt" in Java code (and other shells). Perhaps using the System.out.println() is one way of testing whether the code is giving the desired result.

So, some contexts require "\" and some require "\\".

I might have been thrown by my limited experience in Perl, which provides the option of reading a string literally or interpretively (by single quotes and double quotes).
1 2 Previous Next