function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
MrTheTylerMrTheTyler 

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

 

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

This is a compilation/parsing feature I think.

 

When you type:

 

 

string s2 ='\';

 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:

 

 

string s2 ='\\';

 

 

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:

 

 

string s2 ='This is Bob\'s string';

 

 

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

 

 

string s1 = [select description from case where casenumber = '34252'].description;
string s2 ='\\';
system.debug(s1==s2);  //evaluates to true

 

s2 actually contains a single backslash character at runtime.

 

 

 

 

All Answers

SteveBowerSteveBower

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.

MrTheTylerMrTheTyler

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:

 

 

//note that I have the description field for this test case is set to '\'
string s1 = [select description from case where casenumber = '34252'].description;
string s2 ='\\';
system.debug(s1==s2);  //evaluates to true

 

 

 

bob_buzzardbob_buzzard

This is a compilation/parsing feature I think.

 

When you type:

 

 

string s2 ='\';

 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:

 

 

string s2 ='\\';

 

 

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:

 

 

string s2 ='This is Bob\'s string';

 

 

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

 

 

string s1 = [select description from case where casenumber = '34252'].description;
string s2 ='\\';
system.debug(s1==s2);  //evaluates to true

 

s2 actually contains a single backslash character at runtime.

 

 

 

 

This was selected as the best answer
MrTheTylerMrTheTyler

Excellent information, professor.  Thanks!  :smileyvery-happy: