-4

I am unable to search and find specific text from the below string text".

"Size: 0-0 — 9 otto @ z . t. — fig se REYfAR, S l ' WELCOME BACK, ELLEN TEST! ..$FREE $l PICK YOUR " " . @lotto POWER ' ’-; A . . . . - Q9 / '- 32-5 #63333? .— MILLION I ' /\ FFfA fl:( MEGAPRIZE @011 €011M$0wm I ;. .- h ‘ Glmf/immm £95191 ¢4 0 """ \nA-—~-"“"'/ _/ W Match all 8 #'5 plus your Power # to win theJackpot! €® lOttO 391%}; “a 4 5g] PLAY Now 500.00 ‘(AY\ Ecuo"

Below is the code i have used which doesn't search for the required text.

import java.io.*; 
import java.lang.*; 

class GFG 
{ 
  public static void main (String[] args) 
  { 
    // This is a string in which substring 
    // to be searched. 
    String text = "Above text ^"; 

    CharSequence seq = "WELCOME BACK"; 
    boolean bool = text.contains(seq); 
    System.out.println(bool); 


  } 
}
Rkandanuru
  • 3
  • 1
  • 3

1 Answers1

0

All you have to do is add escape characters for \ and ", like this... \\ and \".

"Size: 0-0 — 9 otto @ z . t. — fig se REYfAR, S l ' WELCOME BACK, ELLEN TEST! ..$FREE $l PICK YOUR \" \" . @lotto POWER ' ’-; A . . . . - Q9 / '- 32-5 #63333? .— MILLION I ' /\\ FFfA fl:( MEGAPRIZE @011 €011M$0wm I ;. .- h ‘ Glmf/immm £95191 ¢4 0 \"\"\" \nA-—~-\"“\"'/ _/ W Match all 8 #'5 plus your Power # to win theJackpot! €® lOttO 391%}; “a 4 5g] PLAY Now 500.00 ‘(AY\\ Ecuo"; 

Edit

Escape characters are need when a character is used as part of java, so the compiler knows that it is not part of the code however it is part of the String. For instance ", it it used by the compiler to indicate the beginning or end of a String. So you need to use an escape character to allow the " to be apart of the string. That escape character is the '\' and you do that with a \". Which leads to the second use of escape characters. Say you need to use a \ in the string, well then you use \\.

The String above should contain the corrected escape sequences, that's what worked for me.

What are all the escape characters?

  • Can you throw some light on where escape characters need to be added? or can you refine my code? – Rkandanuru Feb 19 '19 at 14:27
  • I updated my answer to include the answers to the questions you just asked me, if you have more questions, please ask. I love answering peoples questions. – Jonathan Arendt Feb 20 '19 at 01:06