It is recommended to increase template strings in Java

Ruidong Pu pruidong at gmail.com
Thu Mar 17 09:15:16 UTC 2022


This feature has been around for a long time in other languages. Mainly for automatic insertion of values for string formatting, such as template strings in Python3 or JavaScript (maybe with different names).<br/><br/>Below are some examples of template strings.<br/><br/>For example, after Python 3.6+, Python can format strings like this:<br/><br/>>>> name = "Fred"<br/>>>> f"He said his name is {name!r}."<br/>"He said his name is 'Fred'."<br/>>>> f"He said his name is {repr(name)}."  # repr() is equivalent to !r<br/>"He said his name is 'Fred'."<br/>>>> width = 10<br/>>>> precision = 4<br/>>>> value = decimal.Decimal("12.34567")<br/>>>> f"result: {value:{width}.{precision}}"  # nested fields<br/>'result:      12.35'<br/>>>> today = datetime(year=2017, month=1, day=27)<br/>>>> f"{today:%B %d, %Y}"  # using date format specifier<br/>'January 27, 2017'<br/>>>> f"{today=:%B %d, %Y}" # using date format specifier and debugging<br/>'today=January 27, 2017'<br/>>>> number = 1024<br/>>>> f"{number:#0x}"  # using integer format specifier<br/>'0x400'<br/>>>> foo = "bar"<br/>>>> f"{ foo = }" # preserves whitespace<br/>" foo = 'bar'"<br/>>>> line = "The mill's closed"<br/>>>> f"{line = }"<br/>'line = "The mill\'s closed"'<br/>>>> f"{line = :20}"<br/>"line = The mill's closed   "<br/>>>> f"{line = !r:20}"<br/>'line = "The mill\'s closed" '<br/><br/>output {}:<br/><br/>>>> f"Out: {{0}}"<br/><br/>Multi-line text:<br/><br/>count=1<br/>out=f"""<br/>Count is: {count}<br/>Count is: {count}<br/>Count is: {count}<br/>"""<br/><br/>example from: <a href="https://docs.python.org/3/reference/lexical_analysis.html#f-strings">https://docs.python.org/3/reference/lexical_analysis.html#f-strings</a><br/><br/>JavaScript:<br/><br/>Template strings also exist in JavaScript, e.g.:<br/><br/>// Untagged, these create strings:<br/>`string text`<br/><br/>`string text line 1<br/> string text line 2`<br/><br/>let expression=",";<br/><br/>`string text ${expression} string text`<br/><br/>Reference address: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals</a><br/><br/>If you want Java to optimize the way of formatting strings in Text Blocks, you can refer to the implementation of Python or JavaScript.<br/><br/><html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>

</body>
</html>


More information about the build-dev mailing list