Issue
How to pass large no.of characters in string value. Usually we pass private final String title="Test String". But how to pass the below value in string.
**Test HTML code to insert**
<div id="dededededededede">
Fill out my <a href="https://google.google.com/forms/z1dededede43434niu3pq">frfrtgte form</a>.
</div>
'autoResize':true,
'height':'575',
'async':true,
'host':'dede.com',
'header':'sdedew',
'ssl':true};
s.src = ('https:' == d.state.protocol ? 'https://' : 'http://') + 'secure.deded.com/scripts/embed/form.js';
s.onload = s.onreadystatechange = function() {
var rs = this.readyState; if (rs) if (rs != 'complete') if (rs != 'loaded') return;
try { z1pmdedefr443434frfpq = new googleForm();z1pdedede4343434u3pq.initialize(options);z1pmkdedede434343niu3pq.display(); } catch (e) {}};
var scr = d.getElementsByTagName(t)[0], par = scr.parentNode; par.insertBefore(s, scr);
})(document, 'script');</script>
Solution
With newer Java1 you can use Text Blocks like:
String text = """
**Test HTML code to insert**
<div id="dededededededede">
Fill out my <a href="https://google.google.com/forms/z1dededede43434niu3pq">frfrtgte form</a>.
</div>
'autoResize':true,
'height':'575',
'async':true,
'host':'dede.com',
'header':'sdedew',
'ssl':true};
s.src = ('https:' == d.state.protocol ? 'https://' : 'http://') + 'secure.deded.com/scripts/embed/form.js';
s.onload = s.onreadystatechange = function() {
var rs = this.readyState; if (rs) if (rs != 'complete') if (rs != 'loaded') return;
try { z1pmdedefr443434frfpq = new googleForm();z1pdedede4343434u3pq.initialize(options);z1pmkdedede434343niu3pq.display(); } catch (e) {}};
var scr = d.getElementsByTagName(t)[0], par = scr.parentNode; par.insertBefore(s, scr);
})(document, 'script');</script>
""";
In short: allows quotes and line separators; indentation of closing """
is important - defines how many spaces at start of line will be deleted. See also JEP 368 or the corresponding chapter of the Java Language Specification.
Alternative if Text Blocks are not available (not-so-new Java versions):
- line separator must be added to text:
\n
, or\r\n
for Windows or just the result ofSystem.lineSeparator()
; - double quotes must be escaped:
\"
; - backslashes must also be escaped (if present):
\\
; - multiple lines can/should be used to improve readability
Example:
String text = "" // the empty string here is just for nicer formatting
+ "**Test HTML code to insert**\n"
+ "<div id=\"dededededededede\">\n"
+ " Fill out my <a href=\"https://google.google.com/forms/z1dededede43434niu3pq\">frfrtgte form</a>.\n"
+ "</div>\n"
+ ...
+ "})(document, 'script');</script>";
Note: the compiler creates one single (big) string literal, no concatenation is executed at runtime despite +
being used
1 Java 15 or later, Preview Feature in Java 14
Answered By - user15793316