'How do I escape a semicolon inside of a String? [closed]
I need to initialize complex javascript code as an string. But that javascript code contains semicolon and " mark. I know I can escape " mark using \". But I don't know how to escape semicolon.
<script type="text/javascript"><!--
google_ad_client = "8888888888888";
/* Error Page Ads */
google_ad_slot = "8888888";
google_ad_width = 336;
google_ad_height = 280;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
I need to initialize above code as,
String complexString = "Above code here";
I can't concatenate slices of code, because that way ; will be removed. Right?
This is for Servlet. I may be foolish, but pardon me.
Solution 1:[1]
As long as the ; is inside your double quotes you shouldn't need to escape it.
Solution 2:[2]
Semicolons are not special characters. There's no need of escaping them when they're in a String. You can always test a string's behavior by using the little old System.out.println() method.
Solution 3:[3]
You don't need to escape your ; when they are inside " or ', as they are literals and are perfectly safe.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Baz |
| Solution 2 | Fritz |
| Solution 3 | Zach Jensz |
