+ Start a Discussion
raj123raj123 

How to check whether the string contains singlequote, double quote, and blacklash (\)

How to check whether the string contains singlequote, double quote, and blacklash (\)

 

i am not able to use the sring contains function  for these 3 things.

Coco_SdyneyCoco_Sdyney

for a string contains single quote, use backslash to escape, for example string abccc'd, will need to write code as:

String s = 'abccc\'d';

system.debug(s.contains('\'')); ===> will print out as true

 

double quote should not bother, just take it as normal character, string abccc"d, just write as:

String s='abccc"d';

system.debug(s.contains('"')); ==> will print out as true

 

backslash, same use another backslash to escape, for example abccc\d, will need to write code as:

String s = 'abccc\\d';

system.debug(s.contains('\\'));  ===> wil print out as true

raj123raj123

Hi Coco sdyney, 

thanks for the fast reply 

 

 

what i am actuallu doing is 

 

if(substring.contains('/'')) { substring = substring.substringBefore('/''); }

 

and i am getting the following error 


Save error: expecting a right parentheses, found ')) { substring = substring.substringBefore(' 

 

for all of then i wan to break the string till  this character appear

Coco_SdyneyCoco_Sdyney

escape with backslash \, not slash /

 

change your code to:

if(substring.contains('\'')) { substring = substring.substringBefore('\''); }

raj123raj123

Checked both ways still not able to do it.

crop1645crop1645
String s = '\'';
system.debug('s contains single quote='+s.contains('\''));
String s2 = '"';
system.debug('s2 contains double quote='+s2.contains('"'));
String s3 = '\\';
system.debug('s3 contains backslash='+s3.contains('\\'));

 results in true  for all cases:

 

16:47:02.042 (42069000)|USER_DEBUG|[2]|DEBUG|s contains single quote=true
16:47:02.042 (42153000)|USER_DEBUG|[4]|DEBUG|s2 contains double quote=true
16:47:02.042 (42221000)|USER_DEBUG|[6]|DEBUG|s3 contains backslash=true
  •  In Apex, single quote needs to be escaped with \
  • Double quote need not be escaped
  • Backslash must be escaped with \

You could also do this with Matcher and Pattern classes

sathishsfdcsathishsfdc
Is this working??I tried but don't think it worked