Comparison of programming languages (strings): Difference between revisions

Content deleted Content added
FrescoBot (talk | contribs)
m Bot: link syntax and minor changes
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:
{{Unreferenced|date=February 2009}}
{{ProgLangCompare}}
 
Line 8 ⟶ 7:
 
===Common variants===
 
{| class="wikitable"
|-
Line 15 ⟶ 13:
|-
| +
| [[ALGOL 68]], [[BASIC programming language|BASIC]], [[C++]], [[C Sharp (programming language)|C#]], [[Cobra (programming language) |Cobra]], [[PascalDart (programming language)|PascalDart]], [[ObjectEiffel Pascal(programming language)|Eiffel]], [[EiffelF Sharp (programming language)|EiffelF#]], [[Go (programming language)|Go]], [[JavaScript]], [[Java (programming language)|Java]], [[PythonJavaScript]], [[Object Pascal]], [[Objective-C]], [[Pascal (programming language)|PythonPascal]], [[TuringPython (programming language)|TuringPython]], [[Ruby (programming language)|Ruby]], [[WindowsRust PowerShell(programming language)|Rust]], [[Objective-CScala (programming language)|Scala]], [[Swift (programming language)|Swift]], [[F SharpTuring (programming language)|F#Turing]], [[ScalaWindows (programming language)|ScalaPowerShell]], Ya
|-
| ++
| [[HaskellErlang (programming language)|HaskellErlang]], [[Erlang (programming language)|ErlangHaskell]]
|-
| $+
| [[MIRCmIRC scripting language|mIRC Scripting Language]]
|-
| &
| [[Ada (programming language)|Ada]], [[AppleScript]], [[COBOL]] (for literals only), [[Curl (programming language)|Curl]], [[Seed7Microsoft Excel|Excel]], [[VHDLFreeBASIC]], [[Visual BasicHyperTalk]], [[MicrosoftNim Excel(programming language)|ExcelNim]], [[FreeBASICSeed7]], [[VHDL]], [[Visual Basic]], [[Visual Basic .NET]]
|-
| concatenate
| nconc
| [[Common Lisp]]
|-
| .
| Autohotkey, [[PerlMaple (software)|Maple]] (beforeup to version 65), [[PHPPerl]], and [[Maple computer algebra system|MaplePHP]] (up to version 5), Autohotkey
|-
| ~
| [[PerlD 6(programming language)|D]] and, [[DRaku (programming language)|DRaku]], [[Symfony]] (Expression Language component)
|-
| <nowiki>||</nowiki>
| [[Icon (programming language)|Icon]], Standard [[SQLMaple (software)|Maple]] (from version 6), [[PL/I]], [[Rexx]], andStandard [[Maple computer algebra system|MapleSQL]] (from version 6)
|-
| <>
| [[Mathematica]], [[Wolfram Language]], [[Elixir (programming language)|Elixir]]
|-
| ..
| [[Lua (programming language)|Lua]]
|-
| :
| [[Pick Basic]]
|-
| ,
| [[JAPL (programming language)|APL]], [[Smalltalk]], [[APLJ (programming language)|APLJ]], [[Smalltalk]]
|-
| ^
| [[OCamlF Sharp (programming language)|F#]], [[Standard MLOCaml]], [[F SharpRc (programmingUnix languageshell)|F#rc]], [[rcStandard ML]]
|-
| //
| [[Fortran]]
|-
| *
| [[Julia (programming language)|Julia]]
|}
 
===Unique variants===
 
* [[AwkAWK]] uses the empty string: two expressions adjacent to each other are concatenated. This is called [[Juxtaposition (literary)|juxtaposition]]. [[Unix shell]]s have a similar syntax. [[Rexx]] uses this syntax for concatenation including an intervening space.
* [[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 programming language|Octave]] use the syntax "<code>[x y]</code>" to concatenate x and y.
* [[Visual Basic]] Versionsand 1[[Visual toBasic 6.NET]] can also use the "<code>+</code>" sign but, thisat leadsthe risk toof ambiguity if a string representing a number and a number is addedare together.
* [[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 rawinterpolated ===
An expression is "interpolated" into a string when the compiler/interpreter evaluates it and inserts the result in its place.
"Raw" meaning that the interpreter/compiler does not recognize any variable or constant identifiers located inside the string and the content of the identifier will not replace the identifier in the string.
<!-- 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)
|-
| <ttcode>@$"Hellohello, world!{name}"</ttcode>
| C#, F#Visual Basic .NET
|-
| <ttcode>"Hello, world$name!"</ttcode>
| [[Bourne shell]], Dart, Perl, PHP, Windows PowerShell
| Cobol, FreeBASIC, Java, JavaScript, Pick Basic
|-
| <ttcode>r"qq(Hello, world$name!")</ttcode>
| Perl (alternate)
| D, Python, Cobra
|-
| <ttcode>'"Hello, world{$name}!'"</ttcode>
| PHP (alternate)
| Fortran, JavaScript, Object Pascal, Pascal, Perl, PHP, Windows PowerShell, Smalltalk, APL, Pick Basic
|-
| <ttcode>`"Hello, world#{name}!`"</ttcode>
| CoffeeScript, Ruby, [[Elixir (programming language)|Elixir]]
| D, Go
|-
| <ttcode>R"%Q(Hello, world#{name}!)"</ttcode>
| Ruby (alternate)
| C++11
|}
 
=== Quoted interpolated ===
"Interpolated" means that the interpreter/compiler does recognize a variable or constant identifier located inside the string and the content of the identifier will replace the identifier in the string.
{| class="wikitable"
|-
| <code>(format nil "Hello, ~A" name)</code>
! Syntax
|[[Common Lisp]]
! Language(s)
|-
| <ttcode>$"hello`Hello, ${name}"!`</ttcode>
| JavaScript (ECMAScript 6)
| C#
|-
| <ttcode>"Hello, $\(name)!"</ttcode>
| Swift
| PHP, Perl, Windows PowerShell, Bash shell
|-
| <ttcode>"f'Hello, {$name}!"'</ttcode>
| PHPPython
|-
| <tt>"Hello, #{name}!"</tt>
| Ruby, CoffeeScript
|-
| <tt>(format t "Hello, ~A" name)</tt>
|Common Lisp
|-
| <tt>`Hello, ${name}!`</tt>
| JavaScript (ECMAScript 6)
|-
| <tt>"Hello, \(name)!"</tt>
|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)
|-
| <ttcode>"I said \"Hello, world!\""</ttcode>
| C, C++, C#, D, Dart, F#, Java, JavaScript, Mathematica, Ocaml, Perl, PHP, Python, SwiftRust, JavaScript, MathematicaSwift, Wolfram Language, Ya
|-
| {{nowrap|<ttcode><nowiki>'I said '\'Hello, world!'\''</nowiki></ttcode>}}
| CoffeeScript, Dart (alternate), JavaScript (alternate), Python (alternate)
| Smalltalk, APL
|-
| <ttcode>"I said `"Hello, world!`""</ttcode>
| Windows Powershell
|-
| <ttcode>"I said ^"Hello, world!^""</ttcode>
| REBOL
|-
| <code>{I said "Hello, world!"}</code>
| REBOL (alternate)
|-
| <ttcode>"I said, %"Hello, World!%""</ttcode>
| Eiffel
|-
| <ttcode>!"I said \"Hello, world!\""</ttcode>
| 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)
|-
| <ttcode>"I said ""Hello, world!"""</ttcode>
| Ada, ALGOL 68, COBOL, Excel, Fortran, FreeBASIC, Visual Basic, FreeBASIC, COBOL(.NET)
|-
| <ttcode><nowiki>'I said ''Hello, world!'''</nowiki></ttcode>
| FortranAPL, rcCOBOL, COBOLFortran, SQLObject Pascal, Pascal, Objectrc, PascalSmalltalk, APLSQL
|-
| <tt><nowiki>'I said "Hello, world!"'</nowiki></tt>
| Smalltalk
|}
 
=== Quoted raw ===
{{Expand section|date=June 2008}}
"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. -->
=== Multiple quoting ===
 
{| class="wikitable"
|-
Line 176 ⟶ 169:
! Language(s)
|-
| <ttcode>q(I said "'Hello, world!")'</code>
| APL, Bourne shell, Fortran, Object Pascal, Pascal, Perl, PHP, Pick Basic, Ruby, Smalltalk, Windows PowerShell
qq(I said "Hello, $name!")</tt>
| Perl (raw & interpolated)
|-
| <ttcode>%Qq(I said "Hello, world!")</code>
| Perl (alternate)
%(I said "Hello, world!")</tt>
| Ruby
|-
| <ttcode>{I said "%q(Hello, world!"})</ttcode>
| Ruby (alternate)
| REBOL
|-
| <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}}
|}
 
=== HereMultiline documentstring ===
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
| <tt><<EOF<br />I have a lot of things to say<br />and so little time to say them<br />EOF</tt>
I have a lot of things to say
| [[Bourne shell]], Perl, PHP, Ruby
and so little time to say them
EOF</pre>
| {{Yes}}
| Bourne shell, Perl, Ruby
|-
| <ttpre><<<EOF<br />
I have a lot of things to say<br />
and so little time to say them<br />
EOF</ttpre>
| {{Yes}}
| PHP
|-
| <pre>@"
| <tt>@"<br />I have a lot of things to say<br />and so little time to say them<br />"@</tt>
I have a lot of things to say
and so little time to say them
"@</pre>
| {{No}}
| Windows Powershell
|-
| <pre>"[
| <tt>"[<br />I have a lot of things to say<br />and so little time to say them<br />]"</tt>
I have a lot of things to say
and so little time to say them
]"</pre>
| {{No}}
| Eiffel
|-
| <pre>"""
| <tt>"""<br />I have a lot of things to say<br />and so little time to say them<br />"""</tt>
I have a lot of things to say
| CoffeeScript
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)
|-
| <ttcode>"""Hello13HHello, world!"""</ttcode>
| Triple quoting
| Python
|-
| <tt>13HHello, world!</tt>
| 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]]