-1

I'm fetching the inner text of an object from the screen and then I'm equating it with sometime.

String StrInnerText = Driver.findElement(By.xpath("").getText()
String strtext = '"+StrInnerText+"'

But, when this StrInnerText equates something like "30's" or "I'm", my value becomes something like

String strText = '30's' or 'I'm'

which throws an error. How do I handle this?

whydoieven
  • 589
  • 2
  • 7
  • 21
  • Duplicate from http://stackoverflow.com/questions/3559063/how-to-enter-quotes-in-a-java-string – Olav Trauschke Jun 24 '16 at 05:06
  • Is this code in a .java file? Why don't you use "30's"? Or, if for some reason you need to use single quotes, have you tried escaping them as '30\'s'? – ernest_k Jun 24 '16 at 05:07
  • 2
    Don't know what you want to do, but there is no problem with single quotes in Strings: `String s = "I'am here";` – Moonlit Jun 24 '16 at 05:08
  • Problem is I'm getting that 30's value from the screen! This piece of code is a part of a bigger function and it works fine unless and until the value that I'm fetching from the screen has apostrophes. – whydoieven Jun 24 '16 at 05:11
  • I'm using '"+StrInnerText+"' syntax to pass value of variable within a string. Is there some other way for me to do this? If then I can solve my problem! – whydoieven Jun 24 '16 at 05:16
  • you want to create a new String which contains another string (that contains a single quote)? Is this what you want? '"+StrInnerText+"' (first and last symbol as single quotes) is no valid java syntax. what do you mean by that? If you want to crate a new plain string which contains another this can be simply done by concatinating for example: `String s = "30's"; String ss = "20's "+ s + " 40's";` – Moonlit Jun 24 '16 at 05:32
  • Java String literals have no issues with apostrophes. Java String values have no issues with apostrophes. If you have an issue with apostrophes in your string, it somewhere else, not with Java. So *where* do you have an issue??? BTW: `String strtext = '"+StrInnerText+"'` and `String strText = '30's' or 'I'm'` is not valid Java, since Java string literals use quotes (`"`). – Andreas Jun 24 '16 at 05:33
  • *"to pass value of variable within a string"* What does that mean? `StrInnerText` is already a string. – Andreas Jun 24 '16 at 05:37
  • I'm doint this exactly. int ListValue = Driver.findelements(By.xpath(".//li[contains(text(),'"+ strInnerText +")]")).size() and if size is more than 1 i'm going inside my function. – whydoieven Jun 24 '16 at 09:01

1 Answers1

1

To pass value within a string you need to use

String strtext = "Part of your String"+StrInnerText+"rest of the part of your string";

If you want double quotes as well along with your string you need to use

String strtext = "\""+StrInnerText+"\"";