Content deleted Content added
m Bot: link syntax and minor changes |
Aadirulez8 (talk | contribs) m v2.05 - Fix errors for CW project (Reference list missing / disambiguation page with disallowed <ref>) |
||
(46 intermediate revisions by 27 users not shown) | |||
Line 1:
{{ProgLangCompare}}
Line 8 ⟶ 7:
===Common variants===
{| class="wikitable"
|-
Line 15 ⟶ 13:
|-
| +
| [[ALGOL 68]], [[
|-
| ++
| [[
|-
| $+
| [[
|-
| &
| [[Ada (programming language)|Ada]], [[AppleScript]], [[COBOL]] (for literals only), [[Curl (programming language)|Curl]], [[
|-
| concatenate
| [[Common Lisp]]
|-
| .
| Autohotkey, [[
|-
| ~
| [[
|-
| <nowiki>||</nowiki>
| [[Icon (programming language)|Icon]],
|-
| <>
| [[Mathematica]], [[Wolfram Language]], [[Elixir (programming language)|Elixir]]
|-
| ..
| [[Lua (programming language)|Lua]]
|-
| :
| [[Pick Basic]]
|-
| ,
| [[
|-
| ^
| [[
|-
| //
| [[Fortran]]
|-
| *
| [[Julia (programming language)|Julia]]
|}
===Unique variants===
* [[
* [[C (programming language)|C]] (along with [[Python (programming language)|Python]] and [[Dart (programming language)|Dart]]<ref>{{Cite web |title=Built-in types |url=https://dart.dev/language/built-in-types/ |access-date=2025-07-22 |website=dart.dev |language=en}}</ref>) allows juxtaposition for string literals, however, for strings stored as [[Character (computing)|character]] [[Array data structure|arrays]], the <code>[[strcat]]</code> function must be used.
* [[COBOL]] uses the <code>STRING</code> statement to concatenate string variables.
* [[MATLAB]] and [[GNU Octave
* [[Visual Basic]]
* [[Microsoft Excel]] allows both "<code>&</code>" and the function "<code>=CONCATENATE(X,Y)</code>".
* [[Rust (programming language)|Rust]] has the <code>concat!</code> macro and the <code>format!</code> macro, of which the latter is the most prevalent throughout the documentation and examples.
== String literals ==
This section compares styles for declaring a [[string literal]].
=== Quoted
An expression is "interpolated" into a string when the compiler/interpreter evaluates it and inserts the result in its place.
<!-- Before you add your favorite language to this table, make sure it's not listed in another section. -->
{| class="wikitable"
|-
Line 76 ⟶ 79:
! Language(s)
|-
| <
| C#,
|-
| <
| [[Bourne shell]], Dart, Perl, PHP, Windows PowerShell
|-
| <
| Perl (alternate)
|-
| <
| PHP (alternate)
|-
| <
| CoffeeScript, Ruby, [[Elixir (programming language)|Elixir]]
|-
| <
| Ruby (alternate)
|-
| <code>(format nil "Hello, ~A" name)</code>
|[[Common Lisp]]
|-
| <
| JavaScript (ECMAScript 6)
|-
| <
| Swift
|-
| <
|
|}
=== Escaped quotes ===
"Escaped" quotes means that a 'flag' symbol is used to warn that the character after the flag is used in the string rather than ending the string.
<!-- Before you add your favorite language to this table, make sure it's not listed in another section. -->
{| class="wikitable"
|-
Line 131 ⟶ 118:
! Language(s)
|-
| <
| C, C++, C#, D, Dart, F#, Java, JavaScript, Mathematica, Ocaml, Perl, PHP, Python,
|-
| {{nowrap|<
| CoffeeScript, Dart (alternate), JavaScript (alternate), Python (alternate)
|-
| <
| Windows Powershell
|-
| <
| REBOL
|-
| <code>{I said "Hello, world!"}</code>
| REBOL (alternate)
|-
| <
| Eiffel
|-
| <
| FreeBASIC
|-
| {{nowrap|<code>r#"I said "Hello, world!""#</code>}}
|Rust (alternate)
|-
| {{nowrap|<code>R"("I said "Hello, world!")"</code>}}
|C++ (alternate)
|}
=== Dual quoting ===
"Dual quoting" means that whenever a quote is used in a string, it is used twice, and one of them is discarded and the single quote is then used within the string.
<!-- Before you add your favorite language to this table, make sure it's not listed in another section. -->
{| class="wikitable"
|-
Line 157 ⟶ 154:
! Language(s)
|-
| <
| Ada, ALGOL 68, COBOL, Excel, Fortran, FreeBASIC, Visual Basic
|-
| <
|
|}
=== Quoted raw ===
"Raw" means the compiler treats every character within the literal exactly as written, without processing any escapes or interpolations.
<!-- Before you add your favorite language to this table, make sure it's not listed in another section. -->
{| class="wikitable"
|-
Line 176 ⟶ 169:
! Language(s)
|-
| <
| APL, Bourne shell, Fortran, Object Pascal, Pascal, Perl, PHP, Pick Basic, Ruby, Smalltalk, Windows PowerShell
|-
| <
| Perl (alternate)
|-
| <
| Ruby (alternate)
|-
| <code>R"(Hello, world!)"</code>
| C++11
|-
| <code>@"Hello, world!"</code>
| C#, F#
|-
| <code>r"Hello, world!"</code>
| Cobra, D, Dart, Python, Rust
|-
| <code>r'Hello, world!'</code>
| Dart (alternate)
|-
| <code>"Hello, world!"</code>
| Cobol, FreeBASIC, Pick Basic
|-
| <code>`Hello, world!`</code>
| D, Go
|-
| <code>raw"Hello, world!"</code>
| Scala
|-
| <code>String.raw`Hello, World!`</code>
| JavaScript (ECMAScript 6) {{ref|es6rawstr}}
|}
===
Many languages have a syntax specifically intended for strings with multiple lines. In some of these languages, this syntax is a [[here document]] or "heredoc": A token representing the string is put in the middle of a line of code, but the code continues after the starting token and the string's content doesn't appear until the next line. In other languages, the string's content starts immediately after the starting token and the code continues after the string literal's terminator.
<!-- Before you add your favorite language to this table, make sure it's not listed in another section. -->
{| class="wikitable" border="1"
|-
! Syntax
! Here<br/>document
! Language(s)
|-
| <pre><<EOF
I have a lot of things to say
and so little time to say them
EOF</pre>
| {{Yes}}
| Bourne shell, Perl, Ruby
|-
| <
I have a lot of things to say and so little time to say them EOF</ | {{Yes}}
| PHP
|-
| <pre>@"
I have a lot of things to say
and so little time to say them
"@</pre>
| {{No}}
| Windows Powershell
|-
| <pre>"[
I have a lot of things to say
and so little time to say them
]"</pre>
| {{No}}
| Eiffel
|-
| <pre>"""
I have a lot of things to say
and so little time to say them
"""</pre>
| {{No}}
| CoffeeScript, Dart, Groovy, Kotlin, Python, Swift
|-
|<pre>"
I have a lot of things to say
and so little time to say them
"</pre>
| {{No}}
|[[Common Lisp]] (all strings are multiline), Rust (all strings are multiline), Visual Basic .NET (all strings are multiline)
|-
| <pre>R"(
I have a lot of things to say
and so little time to say them
)"
</pre>
| {{No}}
| C++
|-
| <pre>r"
I have a lot of things to say
and so little time to say them
"</pre>
| {{No}}
| Rust
|-
| <pre>[[
I have a lot of things to say
and so little time to say them
]]</pre>
| {{No}}
| Lua
|-
| <pre>`
I have a lot of things to say
and so little time to say them
`</pre>
| {{No}}
| JavaScript (ECMAScript 6)
|}
=== Unique quoting variants ===
<!-- Before you add your favorite language to this table, make sure it's not listed in another section. -->
{| class="wikitable"
|-
Line 218 ⟶ 292:
! Language(s)
|-
| <
| Hollerith notation
| Fortran 66
Line 230 ⟶ 300:
| YAML
|}
== Notes ==
:1. {{note|es6rawstr}} <code>String.raw``</code> still processes string interpolation.
==References==
{{Reflist}}
:1. {{note|es6rawstr}} https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw
[[Category:Programming language comparisons|*Strings]]
|