Comparison of programming languages (strings): Difference between revisions

Content deleted Content added
No edit summary
 
m v2.05 - Fix errors for CW project (Reference list missing / disambiguation page with disallowed <ref>)
 
(234 intermediate revisions by more than 100 users not shown)
Line 1:
{{ProgLangCompare}}
{| cellpadding="1" style="float: right; border: 1px solid #8888aa; background: #f7f8ff; padding: 5px; font-size: 85%; margin: 0 15px 0 15px;"
 
| style="background: #ccf; text-align: center;" | '''This article is part of'''<br> '''the [[Comparison of programming languages|Programming Language Comparison]]'''<br>'''series.'''
This '''comparison of programming languages (strings)''' compares the features of [[string (computer science)|string]] [[data structure]]s or [[text string|text-string]] processing for over 52 various computer [[programming language]]s.
 
== Concatenation ==
Different languages use different symbols for the concatenation operator. Many languages use the "+" symbol, though several deviate from this.
 
===Common variants===
{| class="wikitable"
|-
! Operator
| style="text-align: center;"|[[Comparison of programming languages|General Comparison]]
! Languages
|-
| +
| style="text-align: center;"|[[Comparison of programming languages (syntax)|Basic Syntax]]
| [[ALGOL 68]], [[BASIC]], [[C++]], [[C Sharp (programming language)|C#]], [[Cobra (programming language)|Cobra]], [[Dart (programming language)|Dart]], [[Eiffel (programming language)|Eiffel]], [[F Sharp (programming language)|F#]], [[Go (programming language)|Go]], [[Java (programming language)|Java]], [[JavaScript]], [[Object Pascal]], [[Objective-C]], [[Pascal (programming language)|Pascal]], [[Python (programming language)|Python]], [[Ruby (programming language)|Ruby]], [[Rust (programming language)|Rust]], [[Scala (programming language)|Scala]], [[Swift (programming language)|Swift]], [[Turing (programming language)|Turing]], [[Windows PowerShell]], Ya
|-
| ++
| style="text-align: center;"|[[Comparison of programming languages (strings)|String Operations]]
| [[Erlang (programming language)|Erlang]], [[Haskell]]
|-
| $+
| style="text-align: center;"|[[___blank___|___blank___]]
| [[mIRC scripting language]]
|-
| &
| style="text-align: center;"|[[___blank___|___blank___]]
| [[Ada (programming language)|Ada]], [[AppleScript]], [[COBOL]] (for literals only), [[Curl (programming language)|Curl]], [[Microsoft Excel|Excel]], [[FreeBASIC]], [[HyperTalk]], [[Nim (programming language)|Nim]], [[Seed7]], [[VHDL]], [[Visual Basic]], [[Visual Basic .NET]]
|-
| concatenate
| style="text-align: center;"|[[Compatibility of C and C++|Compatibility of C and C++]]
| [[Common Lisp]]
|-
| .
| style="text-align: center;"|[[Comparison of C Sharp and Java|Comparison of C Sharp and Java]]
| Autohotkey, [[Maple (software)|Maple]] (up to version 5), [[Perl]], [[PHP]]
|-
| ~
| style="text-align: center;"|[[Comparison of Pascal and C|Comparison of Pascal and C]]
| [[D (programming language)|D]], [[Raku (programming language)|Raku]], [[Symfony]] (Expression Language component)
|-
| <nowiki>||</nowiki>
| style="text-align: center;"|[[___blank___|___blank___]]
| [[Icon (programming language)|Icon]], [[Maple (software)|Maple]] (from version 6), [[PL/I]], [[Rexx]], Standard [[SQL]]
|-
| <>
| [[Mathematica]], [[Wolfram Language]], [[Elixir (programming language)|Elixir]]
|-
| ..
| [[Lua (programming language)|Lua]]
|-
| :
| [[Pick Basic]]
|-
| ,
| [[APL (programming language)|APL]], [[J (programming language)|J]], [[Smalltalk]]
|-
| ^
| [[F Sharp (programming language)|F#]], [[OCaml]], [[Rc (Unix shell)|rc]], [[Standard ML]]
|-
| //
| [[Fortran]]
|-
| *
| [[Julia (programming language)|Julia]]
|}
<noinclude>[[Category:Programming language comparisons]]</noinclude>
 
===Unique variants===
== Concatenation ==
 
todo
* [[AWK]] 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|Octave]] use the syntax "<code>[x y]</code>" to concatenate x and y.
* [[Visual Basic]] and [[Visual Basic .NET]] can also use the "<code>+</code>" sign but at the risk of ambiguity if a string representing a number and a number are 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 interpolated ===
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"
|-
! Syntax
! Language(s)
|-
| <code>$"hello, {name}"</code>
| C#, Visual Basic .NET
|-
| <code>"Hello, $name!"</code>
| [[Bourne shell]], Dart, Perl, PHP, Windows PowerShell
|-
| <code>qq(Hello, $name!)</code>
| Perl (alternate)
|-
| <code>"Hello, {$name}!"</code>
| PHP (alternate)
|-
| <code>"Hello, #{name}!"</code>
| CoffeeScript, Ruby, [[Elixir (programming language)|Elixir]]
|-
| <code>%Q(Hello, #{name}!)</code>
| Ruby (alternate)
|-
| <code>(format nil "Hello, ~A" name)</code>
|[[Common Lisp]]
|-
| <code>`Hello, ${name}!`</code>
| JavaScript (ECMAScript 6)
|-
| <code>"Hello, \(name)!"</code>
| Swift
|-
| <code>f'Hello, {name}!'</code>
| Python
|}
 
=== 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"
|-
! Syntax
! Language(s)
|-
| <code>"I said \"Hello, world!\""</code>
| C, C++, C#, D, Dart, F#, Java, JavaScript, Mathematica, Ocaml, Perl, PHP, Python, Rust, Swift, Wolfram Language, Ya
|-
| {{nowrap|<code><nowiki>'I said \'Hello, world!\''</nowiki></code>}}
| CoffeeScript, Dart (alternate), JavaScript (alternate), Python (alternate)
|-
| <code>"I said `"Hello, world!`""</code>
| Windows Powershell
|-
| <code>"I said ^"Hello, world!^""</code>
| REBOL
|-
| <code>{I said "Hello, world!"}</code>
| REBOL (alternate)
|-
| <code>"I said, %"Hello, World!%""</code>
| Eiffel
|-
| <code>!"I said \"Hello, world!\""</code>
| 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"
|-
! Syntax
! Language(s)
|-
| <code>"I said ""Hello, world!"""</code>
| Ada, ALGOL 68, COBOL, Excel, Fortran, FreeBASIC, Visual Basic (.NET)
|-
| <code><nowiki>'I said ''Hello, world!'''</nowiki></code>
| APL, COBOL, Fortran, Object Pascal, Pascal, rc, Smalltalk, SQL
|}
 
=== Quoted raw ===
"Raw" means the compiler treats every character within the literal exactly as written, without processing any escapes or interpolations.
<![CDATA[ TheQuickBrownFox ]]> ;; CDATA section ;; XML
<!-- Before you add your favorite language to this table, make sure it's not listed in another section. -->
{| class="wikitable"
|-
! Syntax
! Language(s)
|-
| <code>'Hello, world!'</code>
| APL, Bourne shell, Fortran, Object Pascal, Pascal, Perl, PHP, Pick Basic, Ruby, Smalltalk, Windows PowerShell
|-
| <code>q(Hello, world!)</code>
| Perl (alternate)
|-
| <code>%q(Hello, world!)</code>
| 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}}
|}
 
=== QuotedMultiline interpolatedstring ===
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.
todo
<!-- 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
|-
| <pre><<<EOF
I have a lot of things to say
and so little time to say them
EOF</pre>
| {{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"
|-
! Syntax
! Variant name
! Language(s)
|-
| <code>13HHello, world!</code>
| Hollerith notation
| Fortran 66
|-
| (indented with whitespace)
| Indented with whitespace and newlines
| YAML
|}
 
=== Dual quotingNotes ===
:1. {{note|es6rawstr}} <code>String.raw``</code> still processes string interpolation.
todo
 
==References==
=== Multiple quoting ===
{{Reflist}}
todo
:1. {{note|es6rawstr}} https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw
 
[[Category:Programming language comparisons|*Strings]]
=== Unique ===
[[Category:String (computer science)|Programming language comparison]]
16HTheQuickBrownFox ;; Hollerith notation ;; Fortran
(indented with whitespace) ;; Indented with whiteapce and newlines ;; YAML