You need to sign in to do that
Don't have an account?

line breaks not allowed in string literals error
if I write
string s= '\';
I get the error "line breaks not allowed in string literals".
However, if I find a text field in an object and fill it with only a '\' and do like so:
string s = [select fld from obj where id = 123abc].fld;
Everything is just swell. What gives?
Tyler
This is a compilation/parsing feature I think.
When you type:
this cannot be parsed as you have escaped the closing single quote, so the expectation is that there will be another one.
When you type:
This is then allowed to be parsed, as you have escaped the backslash, so the compiler actually stores the string literal of a single backslash in the string.
and when you type:
this is allowed as there is a single quote closing the string. Again, the actual string stored in s2 is This is Bob's string - the opening and closing single quotes don't get stored, they are simply a way of specifying a string literal to the compiler.
Thus in your final piece of code
s2 actually contains a single backslash character at runtime.
All Answers
Backslash is the escape character in apex. If you want a single character string with a backslash in it you need:
String s= '\\';
Best, Steve.
Thanks Steve - you pointed me in the right direction as I was curious why the first statement would error and not the second. It appears that Apex escapes the contents of the text fields for us as demonstrated by:
This is a compilation/parsing feature I think.
When you type:
this cannot be parsed as you have escaped the closing single quote, so the expectation is that there will be another one.
When you type:
This is then allowed to be parsed, as you have escaped the backslash, so the compiler actually stores the string literal of a single backslash in the string.
and when you type:
this is allowed as there is a single quote closing the string. Again, the actual string stored in s2 is This is Bob's string - the opening and closing single quotes don't get stored, they are simply a way of specifying a string literal to the compiler.
Thus in your final piece of code
s2 actually contains a single backslash character at runtime.
Excellent information, professor. Thanks! :smileyvery-happy: