Literals/String

From Rosetta Code
Revision as of 16:21, 4 July 2014 by rosettacode>Gerard Schildberger (→‎{{header|REXX}}: removed a blank line, added whitespace in assignments.)
Task
Literals/String
You are encouraged to solve this task according to the task description, using any language you may know.

Show literal specification of characters and strings. If supported, show how verbatim strings (quotes where escape sequences are quoted literally) and here-strings work. Also, discuss which quotes expand variables.

Ada

Single character literals require single quotes <lang Ada>ch : character := 'a';</lang> String literals use double quotes <lang Ada>msg : string := "hello world"; empty : string := ""; -- an empty string</lang> The length of a string in Ada is equal to the number of characters in the string. Ada does not employ a terminating null character like C. A string can have zero length, but zero length strings are not often used. Ada's string type is a fixed length string. It cannot be extended after it is created. If you need to extend the length of a string you need to use either a bounded string, which has a pre-determined maximum length, similar to C strings, or an unbounded string which can expand or shrink to match the data it contains.

Aime

Aime has no character representation, but it allows single quoted character constants. Their implied typed is integer.

<lang aime>integer c; c = 'z';</lang>

String literals are double quoted.

<lang aime>text s; s = "z";</lang>

ALGOL 68

In ALGOL 68 a single character (CHAR), character arrays ([]CHAR) and strings (STRING) are contained in double quotes. ALGOL 68 also has FORMAT strings which are contained between dollar ($) symbols. <lang algol68>CHAR charx = "z";</lang> Strings are contained in double quotes. <lang algol68>[]CHAR charxyz = "xyz"; STRING stringxyz = "xyz"; FORMAT twonewlines = $ll$, threenewpages=$ppp$, fourbackspaces=$bbbb$;</lang> Note: When only uppercase characters sets are available (eg on computers with only 6 bits per "byte") the single quote can used to denote a reserved word. eg <lang algol68>.PR QUOTE .PR []'CHAR' CHARXYZ = "XYZ";</lang> The STRING type is simply a FLEX array of CHAR. <lang algol68>MODE STRING = FLEX[1:0]CHAR;</lang> ALGOL 68 also has raw strings called BYTES, this type is a fixed width packed array of CHAR. <lang algol68>BYTES bytesabc = bytes pack("abc");</lang> A string quote character is inserted in a string when two quotes are entered, eg: <lang algol68>STRING stringquote = """I'll be back."" - The Terminator";</lang> A string can span lines, but cannot contain newlines. String literals are concatenated when compiled: <lang algol68>STRING linexyz := "line X;" +

"line Y;" + 
"line Z;";</lang>

ALGOL 68 uses FORMATs for doing more advanced manipulations. For example given: <lang algol68>FILE linef; STRING line; associate(linef, line);</lang> Instead of using preprocessor macros ALGOL 68 can do FORMAT variable replacement within FORMATs at run time. <lang algol68>FORMAT my_symbol = $"SYMBOL"$; FORMAT foo = $"prefix_"f(my_symbol)"_suffix"$; putf(linef ,foo);</lang> In standard ALGOL 68 a "book" is a file. A book is composed of pages and lines and therefore a FORMAT be used for inserting backspaces, space, newlines and newpages into books. <lang algol68>INT pages=100, lines=25, characters=80; FILE bookf; FLEX[pages]FLEX[lines]FLEX[characters]CHAR book; associate(bookf, book);

  1. following putf inserts the string " Line 4 indented 5" on page 3 #

putf(bookf, $3p"Page 3"4l5x"Line 4 indented 5"$)</lang> Note: ALGOL 68G does not implement newpage and backspace.

AutoHotkey

unicode <lang AutoHotkey>"c" ; character "text" ; string hereString =  ; with interpolation of %variables% ( "<>" the time is %A_Now% \! )

hereString2 =  ; with same line comments allowed, without interpolation of variables (Comments % literal %A_Now%  ; no interpolation here )</lang>

AWK

In awk, strings are enclosed using doublequotes. Characters are just strings of length 1 in awk. <lang awk> c = "x"

       str= "hello"

s1 = "abcd" # simple string s2 = "ab\"cd" # string containing a double quote, escaped with backslash print s1 print s2 </lang>

Concatenation, example output <lang awk>$ awk 'BEGIN{c="x"; s="hello";s1 = "abcd"; s2 = "ab\"cd"; s=s c; print s; print s1; print s2}' hellox</lang>

BASIC

Traditional BASIC implementations do not use literal character notation within a string or here document notation. However, literal characters can be referenced using their character code and these can be added to strings as required. Here we use the ASCII code for doublequotes to get the characters into a string:

<lang basic>10 LET Q$=CHR$(34): REM DOUBLEQUOTES 20 LET D$=Q$+Q$: REM A PAIR OF DOUBLEQUOTES 30 LET S$=Q$+"THIS IS A QUOTED STRING"+Q$ 40 PRINT Q$;"HELLO";Q$:REM ADD QUOTES DURING OUTPUT</lang>

Most modern BASIC implementations don't differentiate between characters and strings -- a character is just a string of length 1. Few (if any) BASIC implementations support variables inside strings; instead, they must be handled outside the quotes. Most BASICs don't support escaping inside the string, with the possible exception of the VB-style "" for a single quotation mark (not supported by most BASICs). To insert otherwise-unprintable characters requires the use of CHR$. (One notable exception is FreeBASIC, which supports C-style escaping with OPTION ESCAPE.)

Strings can optionally be declared as being a certain length, much like C strings.

<lang qbasic>DIM c AS STRING * 1, s AS STRING

c = "char" 'everything after the first character is silently discarded s = "string" PRINT CHR$(34); s; " data "; c; CHR$(34)</lang>

Output:

"string data c"

Applesoft BASIC

<lang ApplesoftBasic>M$ = CHR$(13) : Q$ = CHR$(34) A$ = "THERE ARE" + M$ A$ = A$ + "NO " + Q$ + "HERE" + Q$ + " STRINGS." ? A$</lang>

ZX Spectrum Basic

The ZX Spectrum supports the use of CHR$(34). Alternatively, it is possible to print the doublequotes, by adding an extra pair of doublequotes:

<lang basic> 10 REM Print some quotes 20 PRINT CHR$(34) 30 REM Print some more doublequotes 40 PRINT """" 50 REM Output the word hello enclosed in doublequotes 60 PRINT """Hello""" </lang>

BBC BASIC

Quoted (literal) strings consist of 8-bit characters and support both ANSI and UTF-8 encodings; they may not contain 'control' characters (0x00 to 0x1F). The only special character is the double-quote " which must be escaped as "". There is no special representation for a single character (it is just a string of length one). 'Here strings' are not supported. <lang bbcbasic> PRINT "This is a ""quoted string"""</lang> Output:

This is a "quoted string"

bc

The double-quote " starts a literal string which ends at the next double-quote. Thus strings can span multiple lines and cannot contain a double-qoute (there is no escaping mechanism).

Characters are just strings of length one.

Befunge

The double quote character (") enters a string literal mode, where ASCII values of characters encountered in the current instruction pointer direction up to the next quote are pushed onto the stack. Thus, any character may be used in a string except for a quote (ascii 34), which may be pushed using 57*1-. Note: since you are pushing the string onto a stack, you usually want to define the string in reverse order so that the first character is on top. <lang befunge>"gnirts">:#,_@</lang>

Bracmat

Strings of any length can always be surrounded by quotes. They must be surrounded by quotes if the string contains white space characters or one of the characters =.,|&:+*^'$_;{} or character sequences \D or \L. They must also be surrounded by quotes if they start with one or more characters from the set [~/#<>%@?!-. Inside strings the characters " and \ must be escaped with a backslash \. White space characters for carriage return, newline and tabulator can be expressed as \r, \n and \t, respectively. Escape sequences need not be enclosed in quotes. A string expression prepended with @ or % has no escape sequences: all characters except quotes are taken litterally. These are 10 examples of valid string expressions. (The last example is a multiline string.)

string
"string"
stri\t-\tng\r\n
"-10"
"+10"
"{a*b}"
".,|&:+*^'$_"
"[~/#<>%@?!-"
string[~/#<>%@?!-
"str; ing
.,|&:+*^'$_

   "

C

In C, single characters are contained in single quotes.

<lang c>char ch = 'z';</lang>

Strings are contained in double quotes.

<lang c>char str[] = "z";</lang>

This means that 'z' and "z" are different. The former is a character while the latter is a string, an array of two characters: the letter 'z' and the string-terminator null '\0'.

C has no raw string feature (please define). C also has no built-in mechanism for expanding variables within strings.

A string can span lines. Newlines can be added via backslash escapes, and string literals are concatenated when compiled: <lang c>char lines[] = "line 1\n"

"line 2\n"
"line 3\n";</lang>

C can use library functions such as sprintf for doing formatted replacement within strings at run time, or preprocessor concatenation to build string literals at compile time: <lang c>#define FOO "prefix_"##MY_SYMBOL##"_suffix"</lang>

C++

Quoting is essentially the same in C and C++.

In C++11, it is also possible to use so-called "Raw Strings":

<lang cpp>auto strA = R"(this is a newline-separated raw string)"; </LANG>

C#

C# uses single quotes for characters and double quotes for strings just like C.

C# supports verbatim strings. These begin with @" and end with ". Verbatim quotes may contain line breaks and so verbatim strings and here-strings overlap.

<lang csharp>string path = @"C:\Windows\System32"; string multiline = @"Line 1. Line 2. Line 3.";</lang>

Clojure

Character literals are prefixed by a backslash: <lang lisp>[\h \e \l \l \o] ; a vector of characters \uXXXX  ; where XXXX is some hex Unicode code point \\  ; the backslash character literal</lang> There are also identifiers for special characters: <lang lisp>\space \newline \tab \formfeed \return \backspace</lang> Clojure strings are Java Strings, and literals are written in the same manner: <lang lisp>"hello world\r\n"</lang>

COBOL

Strings can be enclosed in either single quotes or double quotes. There is no difference between them. <lang cobol>"This is a valid string." 'As is this.'</lang>

Character literals are strings of two-digit hexadecimal numbers preceded by an x. <lang cobol>X"00" *> Null character X"48656C6C6F21" *> "Hello!"</lang>

There are also figurative constants which are equivalent to certain string literals: <lang cobol>HIGH-VALUE HIGH-VALUES *> Equivalent to (a string of) X"FF". LOW-VALUE LOW-VALUES *> " " X"00". NULL *> " " X"00". QUOTE QUOTES *> " " double-quote character. SPACE SPACES *> " " space. ZERO ZEROS ZEROES *> " " zero.</lang>

Common Lisp

Character literals are referenced using a hash-backslash notation. Strings are arrays or sequences of characters and can be declared using double-quotes or constructed using other sequence commands. <lang lisp>(let ((colon #\:)

     (str "http://www.rosettacode.com/"))
  (format t "colon found at position ~d~%" (position colon str)))</lang>

D

Character literals:

<lang d>char c = 'a';</lang>

Regular strings support C-style escape sequences.

<lang d>auto str = "hello"; // UTF-8 auto str2 = "hello"c; // UTF-8 auto str3 = "hello"w; // UTF-16 auto str4 = "hello"d; // UTF-32</lang>

Literal string (escape sequences are not interpreted):

<lang d>auto str = `"Hello," he said.`; auto str2 = r"\n is slash-n";</lang>

Specified delimiter string:

<lang d>// Any character is allowed after the first quote; // the string ends with that same character followed // by a quote. auto str = q"$"Hello?" he enquired.$";</lang>

<lang d>// If you include a newline, you get a heredoc string: auto otherStr = q"EOS This is part of the string.

   So is this.

EOS";</lang>

Token string:

<lang d>// The contents of a token string must be valid code fragments. auto str = q{int i = 5;}; // The contents here isn't a legal token in D, so it's an error: auto illegal = q{@?};</lang>

Hex string:

<lang d>// assigns value 'hello' to str auto str = x"68 65 6c 6c 6f";</lang>

Delphi

<lang Delphi>var

 lChar: Char;
 lLine: string;
 lMultiLine: string;

begin

 lChar := 'a';
 lLine := 'some text';
 lMultiLine := 'some text' + #13#10 + 'on two lines';</lang>

Déjà Vu

<lang dejavu>local :s "String literal" local :s2 "newline \n carriage return \r tab \t" !print "backslash \\ quote \q decimal character \{8364}"</lang>

Output:
backslash \ quote " decimal character €

DWScript

Strings are either single or double quote delimited, if you want to include the delimiter in the string, you just double it. Specific character codes (Unicode) can be specified via # (outside of the string). <lang delphi> const s1 := 'quoted "word" in string'; const s2 := "quoted ""word"" in string"; // sames as s1, shows the doubling of the delimiter const s2 := 'first line'#13#10'second line'; // CR+LF in the middle </lang>

E

E has three sorts of quotes: strings, characters, and quasiliterals.

<lang e>'T' # character "The quick brown fox" # string `The $kind brown fox` # "simple" quasiliteral term`the($adjectives*, fox)` # "term" quasiliteral</lang>

Strings and characters use syntax similar to Java; double and single quotes, respectively, and common backslash escapes.

Quasiliterals are a user-extensible mechanism for writing any type of object "literally" in source, with "holes" for interpolation or pattern-matching. The fourth example above represents a Term object (terms are a tree language like XML or JSON), with the items from the variable adjectives spliced in.

Quasiliterals can be used for strings as well. The third example above is the built-in simple interpolator, which also supports pattern matching. There is also a regular-expression quasi-pattern:

<lang e>? if ("<abc,def>" =~ `<@a,@b>`) { [a, b] } else { null }

  1. value: ["abc", "def"]

? if (" >abc, def< " =~ rx`\W*(@a\w+)\W+(@b\w+)\W*`) { [a, b] } else { null }

  1. value: ["abc", "def"]</lang>

Emacs Lisp

Strings

The only string literal is a double-quote

<lang Lisp>"This is a string."</lang>

Backslash gives various special characters similar to C, such as \n for newline and \" for a literal double-quote. \\ is a literal backslash. See "Syntax for Strings" in the elisp manual.

Characters

A character is an integer in current Emacs. (In the past character was a separate type.) ? is the read syntax.

<lang Lisp>?z => 122 ?\n => 10</lang>

See "Basic Char Syntax" in the elisp manual.

Erlang

Erlang strings are lists containing integer values within the range of the ASCII or (depending on version and settings) Unicode characters. <lang erlang> "This is a string". [$T,$h,$i,$s,$ ,$a,$ ,$s,$t,$r,$i,$n,$g,$,,$ ,$t,$o,$o]. </lang> Characters are represented either as literals (above) or integer values. <lang erlang> 97 == $a. % => true </lang> With the string syntax, characters can be escaped with \. <lang erlang> "\"The quick brown fox jumps over the lazy dog.\"". </lang>

Forth

In the interpreter: <lang forth>char c emit s" string" type</lang> In the compiler: <lang forth>: main

 [char] c   emit
 s" string"   type ;</lang>

Strings may contain any printable character except a double quote, and may not span multiple lines. Strings are done via the word S" which parses ahead for a terminal quote. The space directly after S" is thus not included in the string.

Works with: GNU Forth

GNU Forth has a prefix syntax for character literals, and another string literal word S\" which allows escaped characters, similar to C. <lang forth>'c emit s\" hello\nthere!"</lang>

friendly interactive shell

<lang fishshell>echo Quotes are optional in most cases. echo echo 'But they are when using either of these characters (or whitespace):' echo '# $ % ^ & * ( ) { } ; \' " \\ < > ?' echo echo Single quotes only interpolate \\ and \' sequences. echo '\In \other \cases, \backslashes \are \preserved \literally.' echo set something variable echo "Double quotes interpolates \\, \" and \$ sequences and $something accesses."</lang>

GAP

<lang gap>IsChar('a');

  1. true

IsString("abc");

  1. true

IsString('a');

  1. false

IsChar("a");

  1. false</lang>

gecho

<lang gecho>'a outascii</lang> Just one character. <lang gecho>'yo...dawg. print</lang> A string.

Go

In Go, character literals are contained in single quotes. <lang go>ch := 'z'</lang>

A character literal specifies a character constant, but like other kinds of constants in Go, it is not fully typed. The Go specification talks about "character literals" and "character constants" but not "character types." Instead there are simply types that are typically used to hold charcters. These byte and rune, which are actually numeric types, aliases for uint8 and int32 respectively. <lang go>ch := 'z' // by default, ch takes type int32 var r rune = 'z' // reflect.TypeOf(r) still returns int32 var b byte = 'z' // reflect.TypeOf(b) returns uint8 b2 := byte('z') // equivalent to b const c byte = 'z' // c is now a "typed constant" b3 := c // equivalent to b</lang>

Strings are contained in double quotes. <lang go>str := "z"</lang>

This means that 'z' and "z" are different. The former is a character while the latter is a string.

Unicode may be included in the string literals. They will be encoded in UTF-8. <lang go>str := "日本語"</lang>

Verbatim (a.k.a. "raw") strings are contained within backquotes, to indicate that backslash characters should NOT be treated as "escape sequences." <lang go>`\n` == "\\n"</lang>

Raw string literals, unlike regular string literals, may also span multiple lines. The newline is included in the string: <lang go>`abc def` == "abc\ndef"</lang>

Go raw string literals serve the purpose of here-strings in other languages. There is no variable expansion in either kind of string literal.

Groovy

In Groovy, unlike in Java, a String literal is delimited with single quotes (apostrophes(')). <lang groovy>def string = 'Able was I'</lang>

There is a double quote (quotation mark(")) delimited syntax in Groovy, but it represents an expression construct called a GString (I know, I know). Inside of a GString, sub-expression substitution of the form ${subexpression} may take place. Thus the following results: <lang groovy>def gString = "${string} ere I saw Elba"

println gString

//Outputs: //Able was I ere I saw Elba</lang>

UNIX Shell command line users should recognize these forms of syntax as strong ('-delimited) and weak ("-delimited) quoting. And like UNIX Shell weak quoting syntax, the evaluated subexpression part of the GString syntax loses its special meaning when preceded by a backslash (\):

<lang groovy>def gString2 = "1 + 1 = ${1 + 1}" assert gString2 == '1 + 1 = 2'

def gString3 = "1 + 1 = \${1 + 1}" assert gString3 == '1 + 1 = ${1 + 1}'</lang>

Groovy also supports multi-line String literals and multi-line GString expressions.

<lang groovy>def multiLineString = A man A plan A canal

def multiLineGString = """ ${multiLineString.trim()}: Panama! """

println multiLineGString

//Outputs: // //A man //A plan //A canal: //Panama! //</lang>

UNIX Shell programmers should recognize these forms of syntax as similar in function to the strong and weak forms of Here Document syntax.

Both String literals and GString expressions support a number of special characters (usually non-printable characters) which consist of a single character preceded by a backslash (hence \X), or a 4-hexadecimal-digit UNICODE encoding preceded by a backslash and a lowercase "u" (hence \uXXXX).

One of these special characters is the backslash itself, denoted with in a String or GString as \\. This actually interferes with regular expression syntax in which literal backslashes play various important regular-expression-specific roles. Thus it can become quite onerous to write regular expressions using String or GString quoting syntax, since every regex backslash would have to be written as \\.

However, Groovy has a special GString syntax that uses slash (/) as a GString delimiter rather that quote ("). In this special syntax, most backslash usages that would require a double backslash in a regular String or GString require only a single backslash (\). This does not create a "regular expression object" (there is not such a thing in Groovy); however, it does evaluate to form a "regular expression ready" String, as demonstrated in the following:

<lang groovy>def regexString = /(\[[Tt]itle\]|\[[Ss]ubject\])${10 * 5}/

assert regexString == '(\\[[Tt]itle\\]|\\[[Ss]ubject\\])50'</lang>

Javascript users (and others) will recognize the roots of this "regex-ready" syntax as a feature in their own language.

Since apostrophe is used to delimit String literals, that delimiter syntax is not available, as it is in Java, to denote single character literals (type char or Character). However, single character string literals can be converted to character literals by casting. Shown in the examples below are casting using the as operator, Java-style parenthetical casting, and forced coercion in the intialization of a variable of type char or Character.

<lang groovy>assert 'a' instanceof String assert ('a' as char) instanceof Character assert ((char)'a') instanceof Character

char x = 'a' assert x instanceof Character Character y = 'b' assert y instanceof Character && (x+1 == y)</lang>

As in Java, backslash is also used to mask a string delimiter. Thus the following two assignments represent strings containing a single quote and a single apostrophe respectively

<lang groovy>def quote = "\"" def apostrophe = '\</lang>

Of course, if you are not using GString subexpression evaluation, you can just use apostrophe delimiters to contain a quote, or quote delimiters to contain an apostrophe.

<lang groovy>def quote2 = '"' def apostrophe2 = "'" assert quote == quote2 assert apostrophe == apostrophe2</lang>

Haskell

language support

Characters use single quotes, strings use double quotes. Both allow Unicode. Escape sequences start with a backslash. There are no verbatim strings, no here-strings, and no expansion of variables in strings.

Strings may be split across lines, even indented, using the 'gap' syntax:

<lang haskell>"abcdef" == "abc\

           \def"

"abc\ndef" == "abc\n\

             \def"</lang>

The Haskell 98 Report section Character and String Literals has more information.

using raw-strings-qq package

using raw-strings-qq package:

<lang haskell> {-# LANGUAGE QuasiQuotes #-} import Text.RawString.QQ

"abc\ndef" == [r|abc def|] </lang>

HicEst

HicEst makes no distinction between single characters and strings. One can use single quotes, or double quotes, or most non-standard characters. <lang hicest>CHARACTER c1='A', c2="B", c3=&C& CHARACTER str1='single quotes', str2="double quotes", str3*100

str3 = % delimit "Nested 'strings' " if needed % </lang> A null character CHAR(0) is printed as " ", displayed as "." in dialogs, but ends the string in Windows controls such as StatusBar or ClipBoard <lang hicest>str3 = 'a string' // CHAR(0) // "may contain" // $CRLF // ~ any character ~ </lang> Named literal constants in HicEst: <lang hicest>$TAB == CHAR(9)  ! evaluates to 1 (true) $LF == CHAR(10) $CR == CHAR(13) $CRLF == CHAR(13) // CHAR(10) ! concatenation</lang>

Icon and Unicon

Below is a little program to demonstrate string literals. <lang Icon>procedure main()

  # strings are variable length are not NUL terminated
  # at this time there is no support for unicode or multi-byte charactersets
  c1 := 'aaab'                                          # not a string - cset
  s1 := "aaab"                                          # string
  s2 := "\"aaab\b\d\e\f\n\l\n\r\t\v\'\"\\\000\x00\^c"   # with escapes and imbedded zero
  # no native variable substitution, a printf library function is available in the IPL
  every x := c1|s1|s2 do                                # show them
     write(" size=",*x,", type=", type(x),", value=", image(x))

end</lang>

Output

  size=2, type=cset, value='ab'
  size=4, type=string, value="aaab"
  size=21, type=string, value="\"aaab\b\d\e\f\n\n\n\r\t\v'\"\\\x00\x00\x03"

IDL

The single and double quotes are fairly interchangeable allowing one to use whichever isn't to be quoted (though single-quotes seem more well-behaved around integers in strings). Thus the following are both valid character-constant assignments:

<lang idl>a = " that's a string " b = ' a "string" is this '</lang>

In a pinch, a character constant doesn't absolutely have to be terminated, rendering the following valid:

<lang idl>a = " that's a string</lang>

Duplicating either of them quotes them. Thus the following contains three single quotes and no double-quotes:

<lang idl>a = ' that''s a string print,a

==> that's a string</lang>

Things in quotes are not expanded. To get to the content of a variable, leave it unquoted:

<lang idl>b = 'hello' a = b+' world print,a

==> hello world</lang>

Single-quoted strings of valid hex or octal digits will be expanded if followed by "x" or "o":

<lang idl>print,'777'x

==> 1911

print,'777'o

==> 511

print,'777'

==> 777</lang>

so will be unterminated double-quoted strings if they represent valid octal numbers:

<lang idl>print,"777

==> 511

print,"877

==> 877</lang>

Note that this renders the following false (common trip-up for IDL newbies):

<lang idl>a = "0"

==> Syntax error.</lang>

...because the number zero indicates that an octal number follows, but the second double-quote is not a valid octal digit.

Byte-arrays that are converted into strings are converted to the ascii-characters represented by the bytes. E.g.

<lang idl>crlf = string([13b,10b])</lang>

Inform 7

String literals are enclosed in double quotes. These may include raw line breaks, or expressions to be substituted enclosed in square brackets.

<lang inform7>Home is a room. The description is "This is where you live...

...with your [number of animals in Home] pet[s]."</lang>

Single quotes in a string are translated to double quotes when they occur outside of a word: the string literal <lang inform7>"'That's nice,' said the captain."</lang> will print as

"That's nice," said the captain.

There are no character literals: phrases that manipulate characters pass them as single-character strings.

J

Like C, J treats strings as lists of characters. Character literals are enclosed in single quotes, and there is no interpolation. Therefore, the only "escape" character neccessary is the single-quote itself, and within a character literal is represented by a pair of adjacent single quotes (much like in C, where within a character literal, a slash is represented by a pair of adjacent slashes).

Examples:

<lang j>'x' NB. Scalar character 'string' NB. List of characters, i.e. a string 'cant get simpler' NB. Embedded single-quote</lang>

Like VB, J can include newlines and other special characters in literals with concatentation. Also like VB, J comes with certain constants predefined for some characters:

<lang j>'Here is line 1',LF,'and line two'

'On a mac, you need',CR,'a carriage return'

'And on windows, ',CRLF,'you need both'

TAB,TAB,TAB,'Everyone loves tabs!'</lang>

These constants are simply names assigned to selections from the ASCII alphabet. That is, the standard library executes lines like this:

<lang j>CR =: 13 { a. LF =: 10 { a. CRLF =: CR,LF NB. Or just 10 13 { a. TAB =: 9 { a.</lang>

Since these constants are nothing special, it can be seen that any variable can be similarly included in a literal:

<lang j>NAME =: 'John Q. Public' 'Hello, ',NAME,' you may have already won $1,000,000'</lang>

For multiline literals, you may define an explicit noun, which is terminated by a lone )

<lang j>template =: noun define Hello, NAME.

My name is SHYSTER, and I'm here to tell you that you my have already won $AMOUNT!!

To collect your winnings, please send $PAYMENT to ADDRESS. )</lang>

Simple substitution is most easily effected by using loading a standard script:

<lang j>load 'strings'

name =: 'John Q. Public' shyster =: 'Ed McMahon' amount =: 1e6 payment =: 2 * amount address =: 'Publishers Clearing House'

targets =:  ;: 'NAME SHYSTER AMOUNT PAYMENT ADDRESS' sources =: ":&.> name;shyster;amount;payment;address

message =: template rplc targets,.sources</lang>

While C-like interpolation can be effected with another:

<lang j> load 'printf'

  'This should look %d%% familiar \nto programmers of %s.' sprintf 99;'C'

This should look 99% familiar to programmers of C.</lang>

Java

<lang java> char a = 'a'; // prints as: a

 String b = "abc";  // prints as: abc
 char doubleQuote = '"';  // prints as: "
 char singleQuote = '\;  // prints as: '
 String singleQuotes = "";  // prints as: 
 String doubleQuotes = "\"\"";  // prints as: ""</lang>

Null characters ('\0') are printed as spaces in Java. They will not terminate a String as they would in C or C++. So, the String "this \0is \0a \0test" will print like this:

this  is  a  test

JavaScript

A JavaScript string is a sequence of zero or more characters enclosed in either 'single quotes' or "double quotes". Neither form prevents escape sequences: "\n" and '\n' are both strings of length 1. There is no variable interpolation.

Lasso

All strings in Lasso are Unicode strings. This means that a string can contain any of the characters available in Unicode. Lasso supports two kinds of string literals: quoted and ticked. Quoted strings can contain escape sequences, while ticked strings cannot. Both quoted and ticked string literals can contain line breaks and they both return same type of string object. [1]

Quoted Strings

<lang Lasso>'I\'m a 2" string\n' "I'm a 2\" string\n"</lang>

Ticked Strings

In the below example here \n would not be a line feed, it represents a backslash and n. <lang Lasso>`I'm also a 2" string\n`</lang>

LabVIEW

LabVIEW is a graphical language so it uses graphical string delimiters. No escaping is needed.
This image is a VI Snippet, an executable image of LabVIEW code. The LabVIEW version is shown on the top-right hand corner. You can download it, then drag-and-drop it onto the LabVIEW block diagram from a file browser, and it will appear as runnable, editable code.

LaTeX

Since LaTeX is a markup language rather than a programming language, quotes are displayed rather than interpreted. However, quotes do deserve special mention in LaTeX. Opening (left) quotes are denoted with backquotes and closing (right) quotes are denoted with quotes. Single quotes use a single symbol and double quotes use double symbols. For example, to typeset 'a' is for "apple" in LaTeX, one would type

<lang latex>`a' is for ``apple''</lang>

One common mistake is to use the same symbol for opening and closing quotes, which results in the one of the quotes being backward in the output. Another common mistake is to use a double quote symbol in the input file rather than two single quotes in order to produce a double quote in the output.

Liberty BASIC

<lang lb> 'Liberty BASIC does not support escape characters within literal strings. print "Quotation mark:" print chr$(34) print

'Print literal string print "Hello, World." 'Print literal string displaying quotation marks. print chr$(34);"Hello, World.";chr$(34) </lang>

Lisaac

Characters: <lang Lisaac>c1 := 'a'; c2 := '\n'; // newline c3 := '\; // quote c4 := '\101o'; // octal c5 := '\10\'; // decimal c6 := '\0Ah\'; // hexadecimal c7 := '\10010110b\'; // binary</lang> Strings: <lang Lisaac>s1 := "this is a\nsample"; // newline s2 := "\""; // double quote s3 := "abc\

     \xyz"; // "abcxyz", cut the gap</lang>

Logo does not have a string or character type that is separate from its symbol type ("word"). A literal word is specified by prefixing a double-quote character. Reserved and delimiting characters, ()[];~+-*/\=<>| and newline, may be used if preceded by a backslash. Alternatively, the string may be wrapped in vertical bars, in which case only backslash and vertical bar need be escaped. <lang logo>print "Hello\,\ world print "|Hello, world|</lang>

Lua

Strings can be enclosed using singlequotes or doublequotes. Having two different types of quotation symbols enables either of the symbols to be embedded within a string enclosed with the other symbol.

<lang lua>singlequotestring = 'can contain "double quotes"' doublequotestring = "can contain 'single quotes'" longstring = [[can contain

              newlines]]

longstring2 = [==[ can contain [[ other ]=] longstring " and ' string [===[ qualifiers]==]</lang>

Note that interpolation of variables names within a string does not take place. However, interpolation of literal characters escape sequences does occur, irrespective of whether singlequote or doublequote enclosures are being used.

M4

The quoting characters are ` and ', but can be changed by the changequote macro: <lang m4>`this is quoted string'</lang> <lang m4>changequote(`[',`]')dnl [this is a quoted string]</lang>

Maple

There is no separate character type in Maple; a character is just a string of length equal to 1. <lang Maple> > "foobar";

                               "foobar"

> "foo\nbar"; # string with a newline "foo

   bar"

> "c"; # a character

                                 "c"

</lang> Note that adjacent strings in the input (separated only by white-space) are concatenated automatically by the parser. <lang Maple> > "foo" "bar";

                               "foobar"

</lang> Since variable names are not distinguished lexically from other text (such as by using a "$" prefix, as in some shells), Maple does not do any kind of variable expansion inside strings.

Mathematica

<lang Mathematica>There is no character type in Mathematica, only string type. "c"; // String (result: "c") "\n"; // String (result: newline character)</lang>

MATLAB

Strings start and end with single quotes, the escape sequence for a single quote with in a string, is the use of two consequtive single quotes <lang Matlab>

   s1 = 'abcd'   % simple string
   s2 = 'abcd'   % string containing a single quote

</lang> Output:

>>     s1 = 'abcd'   % simple string
s1 = abcd
>>     s2 = 'ab''cd'   % string containing a single quote
s2 = ab'cd

Maxima

<lang maxima>/* A string */ "The quick brown fox jumps over the lazy dog";

/* A character - just a one character string */ "a"</lang>

ML/I

ML/I treats all input and programs as character streams. Strings do not have to be quoted; they are taken 'as is'. If one wishes to ensure that a string is taken literally (i.e. not evaluated), it is enclosed in literal brackets. There are no predefined literal brackets; the programmer can define anything suitable, usually by setting up a matched text skip, using the MCSKIP operation macro. By convention, the pair <> is used for literal brackets, unless this clashes in the case of a particular processing task.

Input

<lang ML/I>MCSKIP "WITH" NL "" Literals/String MCINS %. MCSKIP MT,<> "" Demonstration of literal string MCDEF Bob AS Alice "" The following two lines both mention Bob. The first line is "" evaluated, but the second is surrounded by literal brackets and is not "" evaluated. This is the first mention of Bob <and here we mention Bob again></lang>

Output

<lang ML/I>This is the first mention of Alice and here we mention Bob again</lang> Media:Example.ogg

Metafont

In Metafont there's no difference between a single character string and a single character. Moreover, the double quotes (which delimites a string) cannot be inserted directly into a string; for this reason, the basic Metafont macro set defines

<lang metafont>string ditto; ditto = char 34;</lang>

i.e. a string which is the single character having ASCII code 34 ("). Macro or variables expansion inside a string block is inhibited.

<lang metafont>message "You've said: " & ditto & "Good bye!" & ditto & ".";</lang>

Modula-3

Characters in Modula-3 use single quotes. <lang modula3>VAR char: CHAR := 'a';</lang> Strings in Modula-3 use double quotes. <lang modula3>VAR str: TEXT := "foo";</lang> TEXT is the string type in Modula-3. Characters can be stored in an array and then converted to type TEXT using the function Text.FromChars in the Text module.

Strings (of type TEXT) can be converted into an array of characters using the function Text.SetChars. <lang modula3>VAR str: TEXT := "Foo"; VAR chrarray: ARRAY [1..3] OF CHAR;

Text.SetChars(chrarray, str); (* chrarray now has the value ['F', 'o', 'o'] *)</lang>

MUMPS

All strings are delimited by the double quotes character. But you can escape the double quotes to add a double quotes character to a string.

USER>SET S1="ABC"
 
USER>SET S2="""DEF"""
 
USER>SET S3="""GHI"
 
USER>W S1
ABC
USER>W S2
"DEF"
USER>W S3
"GHI

Nemerle

Character literals are enclosed in single quotes. Regular strings are enclosed in double quotes, and use \ to delimit special characters, whitespace similar to C. A @ preceding the double quotes indicates a literal string. A $ preceding the double quote indicates string interpolation, identifiers prefixed with $ inside the string literal will be replaced with their value. Nemerle also has a recursive string literal, enclosed within <# #>, that is the same as a literal string, except that it allows nesting of strings.

<lang Nemerle>'a' // character literal '\n' // also a character literal "foo\nbar" // string literal @"x\n" // same as "x\\n" @"x

y"                                              // same as "x\n y"

@"""Hi!""" // "" replaces \" to escape a literal quote mark <#This string type can contain any symbols including " and new lines. It does not support escape codes like "\n".#> // same as "This string type can contain any symbols including \"\nand new lines. "

                                                //       + "It does not\nsupport escape codes\nlike \"\\n\"."

<#Test <# Inner #> end#> // same as "Test <# Inner #> end" (i.e. this string type support recursion.</lang>

Objective-C

The same as C, with the addition of the new string literal <lang objc>@"Hello, world!"</lang> which represents a pointer to a statically allocated string object, of type NSString *, similar to string literals in Java. You can use this literal like other object pointers, e.g. call methods on it [@"Hello, world!" uppercaseString].

Objeck

Objeck string support is similar to Java except that string elements are 1-byte in length. In addition, string literals may terminated using a NULL character or the string's length calculation.

OCaml

Characters are contained in single quotes: <lang ocaml># 'a';; - : char = 'a'</lang>

Strings are contained in double quotes: <lang ocaml># "Hello world";; - : string = "Hello world"</lang>

Strings may be split across lines and concatenated using the following syntax: (the newline and any blanks at the beginning of the second line is ignored) <lang ocaml># "abc\

   def";;

- : string = "abcdef"</lang>

If the above syntax is not used then any newlines and whitespace are included in the string: <lang ocaml># "abc

  def";;

- : string = "abc\n def"</lang>

Octave

Strings can be defined in Octave with single or double quotes. In order to maintain compatible with Matlab, it is recommended to use single quotes for defining strings. <lang Octave>

   s1 = 'abcd'   % simple string
   s2 = 'abcd'   % string containing a single quote using an escaped single quote
   s3 = 'ab"cd'   % simple string containing a double quote
   s4 = "ab'cd"   % string containing a single quote
   s5 = "ab""cd"   % string containing a double quote using an escaped double quote

</lang> Output:

octave:5>     s1 = 'abcd'   % simple string
s1 = abcd
octave:6>     s2 = 'ab''cd'   % string containing a single quote using an escaped single quote
s2 = ab'cd
octave:7>     s3 = 'ab"cd'   % simple string containing a double quote
s3 = ab"cd
octave:8>     s4 = "ab'cd"   % string containing a single quote
s4 = ab'cd
octave:9>     s5 = "ab""cd"   % string containing a double quote using an escaped double quote
s5 = ab"cd

Oz

<lang oz>declare Digit0 = &0  %% the character '0' NewLine = &\n  %% a character with special representation NewLine = &\012  %% characters can also be specified with octals

%% Strings are lists of characters, but can also be written in double quotes: [&H &e &l &l &o] = "Hello"

AnAtom = 'Hello'  %% single quotes are used for atoms Atom2 = hello = 'hello'  %% for atoms starting with a lower case letter, they are optional

%% To build strings out of other values, so-called virtual strings are used: MyName = "Peter" MyAge = 8 {System.showInfo MyName # " is " # MyAge # " years old."}</lang>

PARI/GP

There are just three escapes:

\e escape
\n newline
\t tab

Any other escaped character simply represents itself; \\ and \" are the most useful. There are no characters or character strings as such, but Vectorsmall("string") is very similar to a character array.

Version 2.4.3 added the functions printf and Strprintf which allow interpolation (typically with %Ps).

Pascal

See Delphi

Perl

Perl makes no distinction between single characters and strings. One can use single or double quotes, but they are different. Double-quotes allows you to interpolate variables and escape sequences, while single-quotes do not.

<lang perl>'c'; # character 'hello'; # these two strings are the same "hello"; 'Hi $name. How are you?'; # result: "Hi $name. How are you?" "Hi $name. How are you?"; # result: "Hi Bob. How are you?" '\n'; # 2-character string with a backslash and "n" "\n"; # newline character `ls`; # runs a command in the shell and returns the output as a string q/hello/; # same as 'hello', but allows custom delimiters, eg: q(hi) and q!hi! qq/hello/; # same as "hello", but allows custom delimiters, eg: qq{$hi} and qq#hi# qw/one two three/; # same as ('one', 'two', 'three'); constructs a list of the words qx/ls/; # quoted execution, same as `ls` qr/regex/; # creates a regular expression <<END; # Here-Document Hi, whatever goes here gets put into the string, including newlines and $variables, until the label we put above END <<'END'; # Here-Document like single-quoted Same as above, but no interpolation of $variables. END</lang>

Perl 6

Unlike most languages that hardwire their quoting mechanisms, the quote mechanism in Perl 6 is extensible, and all normal-looking quotes actually derive from a parent quoting language called Q via grammatical mixins, applied via standard Perl 6 adverbial syntax. The available quote mixins, straight from current spec S02, are:

Short       Long            Meaning
=====       ====            =======
:x          :exec           Execute as command and return results
:w          :words          Split result on words (no quote protection)
:ww         :quotewords     Split result on words (with quote protection)
:v          :val            Evaluate word or words for value literals
:q          :single         Interpolate \\, \q and \' (or whatever)
:qq         :double         Interpolate with :s, :a, :h, :f, :c, :b
:s          :scalar         Interpolate $ vars
:a          :array          Interpolate @ vars
:h          :hash           Interpolate % vars
:f          :function       Interpolate & calls
:c          :closure        Interpolate {...} expressions
:b          :backslash      Interpolate \n, \t, etc. (implies :q at least)
:to         :heredoc        Parse result as heredoc terminator
            :regex          Parse as regex
            :subst          Parse as substitution
            :trans          Parse as transliteration
            :code           Quasiquoting
:p          :path           Return a Path object (see S16 for more options

In any case, an initial Q, q, or qq may omit the initial colon to form traditional Perl quotes such as qw//. And Q can be used by itself to introduce a quote that has no escapes at all except for the closing delimiter: <lang perl6>my $raw = Q'$@\@#)&!#';</lang> Note that the single quotes there imply no single quoting semantics as they would in Perl 5. They're just the quotes the programmer happened to choose, since they were most like the raw quoting. Single quotes imply :q only when used as normal single quotes are, as discussed below. As in Perl 5, you can use any non-alphanumeric, non-whitespace characters for delimiters with the general forms of quoting, including matching bracket characters, including any Unicode brackets.

Using the definitions above, we can derive the various standard "sugar" quotes from Q, including:

Normal  Means
======  =====
q/.../  Q :q /.../
qq/.../ Q :qq /.../
'...'   Q :q /.../
"..."   Q :qq /.../
<...>   Q :q :w :v /.../
«...»   Q :qq :ww :v /.../
/.../   Q :regex /.../
quasi {...} Q :code {...}

The :qq-derived languages all give normal Perlish interpolation, but individual interpolations may be chosen or suppressed with extra adverbs.

Unlike in Perl 5, we don't use backticks as shorthand for what is now expressed as qqx// in Perl 6. (Backticks are now reserved for user-defined syntax.) Heredocs now have no special << syntax, but fall out of the :to adverb: <lang perl6>say qq:to/END/;

   Your ad here.
   END</lang>

Indentation equivalent to the ending tag is automatically removed.

Backslash sequences recognized by :b (and hence :qq) include: <lang perl6>"\a" # BELL "\b" # BACKSPACE "\t" # TAB "\n" # LINE FEED "\f" # FORM FEED "\r" # CARRIAGE RETURN "\e" # ESCAPE "\x263a" # ☺ "\o40" # SPACE "\0" # NULL

"\cC" # CTRL-C "\c8" # BACKSPACE "\c[13,10]" # CRLF "\c[LATIN CAPITAL LETTER A, COMBINING RING ABOVE]"</lang> Leading 0 specifically does not mean octal in Perl 6; you must use \o instead.

PHP

PHP makes no distinction between single characters and strings. One can use single or double quotes, but they are different. Double-quotes allows you to interpolate variables and escape sequences, while single-quotes do not.

<lang php>'c'; # character 'hello'; # these two strings are the same "hello"; 'Hi $name. How are you?'; # result: "Hi $name. How are you?" "Hi $name. How are you?"; # result: "Hi Bob. How are you?" '\n'; # 2-character string with a backslash and "n" "\n"; # newline character `ls`; # runs a command in the shell and returns the output as a string <<END # Here-Document Hi, whatever goes here gets put into the string, including newlines and $variables, until the label we put above END; <<'END' # Here-Document like single-quoted Same as above, but no interpolation of $variables. END;</lang>

PicoLisp

PicoLisp doesn't have a string data type. Instead, symbols are used. Certain uninterned symbols, called "transient symbols", however, look and behave like strings on other languages.

Syntactically, transient symbols (called "strings" in the following) are surrounded by double quotes. <lang PicoLisp>: "ab\"cd" -> "ab\"cd"</lang> Double quotes in strings are escaped with a backslash.

ASCII control characters can be written using the hat ('^') character: <lang PicoLisp>: "ab^Icd^Jef" # Tab, linefeed</lang> There is no special character type or representation. Individual characters are handled as single-character strings: <lang PicoLisp>: (chop "abc") -> ("a" "b" "c")

(pack (reverse @))

-> "cba"</lang> A limited handling of here-strings is available with the 'here' function.

Pike

<lang pike> 'c'; // Character code (ASCII) (result: 99) "c"; // String (result: "c") "\n"; // String (result: newline character) "hi " + world // String (result: "hi " and the contents of the variable world)

  1. "multiple line

string using the preprocessor" // single literal string with newlines in it </lang>

PL/I

<lang PL/I> 'H' /* a single character as a literal. */ 'this is a string' /* an empty string literal. */ 'Johns cat' /* a literal containing an embedded apostrophe. */

                    /* stored are <<John's cat>>                    */

'101100'b /* a bit string, stored as one bit per digit. */ </lang>

Plain TeX

The same as LaTeX case, even though one should say the opposite. The `` and '' in TeX (plainTeX, LaTeX and many more) are just examples of ligatures.


Pop11

In Pop11 charaters literals are written in inverted quotes (backticks)

`a`  ;;; charater a

String are written in quotes

<lang pop11>'a'  ;;; string consisting of single character</lang>

Backslash is used to insert special charaters into strings:

<lang pop11>'\'\n' ;;; string consisting of quote and newline</lang>

PowerShell

PowerShell makes no distinction between characters and strings. Single quoted strings do not interpolate variable contents but double quoted strings do. Also, escape sequences are quoted literally as separate characters within single quotes.

PowerShell here-strings begin with @' (or @") followed immediately by a line break and end with a line break followed by '@ (or "@). Escape sequences and variables are interpolated in @" quotes but not in @' quotes.

PureBasic

PureBasic supports char in ASCII and UNICODE as well as both dynamic and fixed length strings. <lang PureBasic>; Characters (*.c), can be ASCII or UNICODE depending on compiler setting Define.c AChar='A'

defines as *.a it will be ASCII and *.u is always UNICODE

Define.a A='b' Define.u U='水'

Strings is defined as **.s or ending with '$'

Define.s AStrion ="String #1" Define BStrion.s ="String #2" Define CString$ ="String #3"

Fixed length stings can be defined if needed

Define XString.s{100} ="I am 100 char long!"

'"' can be included via CHR() or its predefined constant

Define AStringQuotes$=Chr(34)+"Buu"+Chr(34)+" said the ghost!" Define BStringQuotes$=#DOUBLEQUOTE$+"Buu"+#DOUBLEQUOTE$+" said yet a ghost!"</lang> To dynamically detect the current sizes of a character, e.g. ASCI or UNICODE mode, StringByteLength() can be used. <lang PureBasic>Select StringByteLength("X")

 Case 1
   Print("ASCII-mode;  Soo, Hello world!")
 Case 2
   Print("UNICODE-mode; Soo, 您好世界!")

EndSelect</lang>

Python

Python makes no distinction between single characters and strings. One can use single or double quotes.

<lang python>'c' == "c" # character 'text' == "text" ' " ' " ' " '\x20' == ' ' u'unicode string' u'\u05d0' # unicode literal</lang>

As shown in the last examples Unicode strings are single or double quoted with a "u" or "U" prepended thereto.

Verbatim (a.k.a. "raw") strings are contained within either single or double quotes, but have an "r" or "R" prepended to indicate that backslash characters should NOT be treated as "escape sequences." This is useful when defining regular expressions as it avoids the need to use sequences like \\\\ (a sequence of four backslashes) in order to get one literal backslash into a regular expression string.

<lang python>r'\x20' == '\\x20'</lang>

The Unicode and raw string modifiers can be combined to prefix a raw Unicode string. This must be done as "ur" or "UR" (not with the letters reversed as it: "ru").

Here-strings are denoted with triple quotes.

<lang python> single triple quote """ double triple quote """</lang>

The "u" and "r" prefixes can also be used with triple quoted strings.

Triple quoted strings can contain any mixture of double and single quotes as well as embedded newlines, etc. They are terminated by unescaped triple quotes of the same type that initiated the expression. They are generally used for "doc strings" and other multi-line string expressions --- and are useful for "commenting out" blocks of code.

R

R makes no distinction between characters and strings, and uses single and double quotes interchangeably, though double quotes are considered to be preferred. Verbatim strings are not supported. See ?Quotes for more information.

<lang R>str1 <- "the quick brown fox, etc." str2 <- 'the quick brown fox, etc.' identical(str1, str2) #returns TRUE</lang>

R also supports testing string literals with ==, e.g.,

<lang R>modestring <- 'row,col' mode.vec <- unlist(strsplit(modestring, ',')) mode.vec[1] # "row" mode.vec[2] # "col" if (mode.vec[2] == 'col') { cat('Col!\n') } # Col! (with no quotes) if (mode.vec[1] == "row") { cat('Row!\n') } # Row!</lang>

R also uses backticks, for creating non-standard variable names (amongst other things).

<lang R>`a b` <- 4 `a b` # 4 a b # Error: unexpected symbol in "a b"</lang>

R will print different styles of single and double quote using sQuote and dQuote

<lang R>options(useFancyQuotes=FALSE) cat("plain quotes: ", dQuote("double"), "and", sQuote("single"), "\n")</lang>

returns

plain quotes:  "double" and 'single'

<lang R>options(useFancyQuotes=TRUE) cat("fancy quotes: ", dQuote("double"), "and", sQuote("single"), "\n")</lang>

returns

fancy quotes:  “double” and ‘single’

<lang R>options(useFancyQuotes="TeX") cat("fancy quotes: ", dQuote("double"), "and", sQuote("single"), "\n")</lang>

returns

 TeX quotes:  ``double'' and `single'

Racket

Characters are specified as hash-backslash-character, sometime using a name for the character.

<lang Racket>

 #\a
 #\space
 #\return

</lang>

Strings are double-quoted, and have most of the usual C-style escapes. To include a double-quote in strings, escape it with a backslash, and the same goes for doubly-escaped backslashes (leading to the usual regexp fun).

Racket source code is read as UTF-8 text so strings can include Unicode characters -- but the internal representation is UCS-4. This includes "\NNN" for octals and "\xHH" for hex and "\uHHHH" for higher characters. See the docs for a complete specification.

Racket also has here strings, and a more sophisticated facility for text that includes interpolation-like features, which is described in the Here Document entry

Retro

Strings are enclosed in double quotes, and can contain anything other than a double quote.

ASCII characters are prefixed by a single quote.

<lang Retro>'c "hello, world!"</lang>

REXX

There are two types of quotes used for REXX literals:

  •   "     (sometimes called a double quote or quote)
  •   '     (sometimes called a single quote or apostrophe)

There is no difference between them as far as specifying a REXX literal.
You can double them (code two of them adjacent) to specify a quote within the string. <lang rexx>char1 = "A" char2 = 'A' str = "this is a string" another = 'this is also a string' escape1 = "that's it!" escape2 = 'thats it!'</lang> Variable expansion is not possible within REXX literals.
Simply concatenate the string with the variable: <lang rexx>amount = 100 result = "You got" amount "points." say result</lang> output

You got 100 points.

It's also possible to express characters in hexadecimal notation in a string: <lang rexx>lf = '0A'x cr = '0D'x

mmm = '01 02 03 34 ee'x ppp = 'dead beaf 11112222 33334444 55556666 77778888 00009999 c0ffee'X

lang = '52455858'x /*which is "REXX" on ASCII-computers.*/</lang> Binary strings are also possible: <lang rexx>jjj = '01011011'B jjj = '01011011'b jjj = "0101 1011"b jjj = '0101 1011 1111'b longjjj = '11110000 10100001 10110010 11100011 11100100'B</lang>

Ruby

Quotes that do not interpolate: <lang ruby>'single quotes with \'embedded quote\' and \\backslash' %q(not interpolating with (nested) parentheses and newline)</lang> Quotes that interpolate: <lang ruby>a = 42 "double quotes with \"embedded quote\"\nnewline and variable interpolation: #{a} % 10 = #{a % 10}" %Q(same as above) %|same as above|</lang> Heredocs <lang ruby>print <<HERE With an unquoted delimiter, this interpolates: a = #{a} HERE print <<-INDENTED

  This delimiter can have whitespace before it
  INDENTED

print <<'NON_INTERPOLATING' This will not interpolate: #{a} NON_INTERPOLATING</lang>

Scala

Character literals use single quotes marks:

<lang scala>val c = 'c'</lang>

However, symbols are denoted with a single quote, so care must be taken not to confuse the two:

<lang scala>val sym = 'symbol</lang>

Strings can use either double quotes, or three successive double quotes. The first allows special characters, the second doesn't:

<lang scala>scala> "newline and slash: \n and \\" res5: java.lang.String = newline and slash:

and \

scala> """newline and slash: \n and \\""" res6: java.lang.String = newline and slash: \n and \\</lang>

However, Unicode characters are expanded wherever they happen, even inside comments. So, for instance:

<lang scala>scala> val uniquote = \u0022normal string" uniquote: java.lang.String = normal string

scala> val insidequote = """an inside \u0022 quote""" insidequote: java.lang.String = an inside " quote</lang>

Finally, on version 2.7, the triple-double-quoted string ends at the third consecutive quote, on version 2.8 it ends on the last quote of a series of at least three double-quotes.

Scala 2.7 <lang scala>scala> val error = """can't finish with a quote: """" <console>:1: error: unterminated string

      val error = """can't finish with a quote: """"
                                                   ^</lang>

Scala 2.8 <lang scala>scala> val success = """but it can on 2.8: """" success: java.lang.String = but it can on 2.8: "</lang>

Scheme

Characters are specified using the "#\" syntax: <lang scheme>#\a

  1. \A
  2. \?
  3. \space
  4. \newline</lang>

Strings are contained in double quotes: <lang scheme>"Hello world"</lang>

Literal symbols, lists, pairs, etc. can be quoted using the quote syntax: <lang scheme>'apple '(1 2 3) ; same as (list 1 2 3) '()  ; empty list '(a . b) ; same as (cons 'a 'b)</lang>

Seed7

The type char describes Unicode characters encoded with UTF-32. A character literal is written as UTF-8 encoded Unicode character enclosed in single quotes.

<lang seed7>var char: ch is 'z';</lang>

The type string describes sequences of Unicode characters. The characters in the string use the UTF-32 encoding. A string literal is a sequence of UTF-8 encoded Unicode characters surrounded by double quotes.

<lang seed7>var string: stri is "hello";</lang>

This means that 'z' and "z" are different. The former is a character while the latter is a string. Seed7 strings are not null terminated (they do not end with \0). They can contain any sequence of UNICODE (UTF-32) characters (including a \0). Empty strings are also allowed. In order to represent non-printable characters and certain printable characters the following escape sequences may be used.

audible alert BEL \a
backspace BS \b
escape ESC \e
formfeed FF \f
newline NL (LF) \n
carriage return CR \r
horizontal tab HT \t
vertical tab VT \v
backslash (\) \\
apostrophe (') \'
double quote (") \"
control-A \A
...
control-Z \Z

A backslash followed by an integer literal and a semicolon is interpreted as character with the specified ordinal number. Note that the integer literal is interpreted decimal unless it is written as based integer.

<lang seed7>"Euro sign: \8364;"</lang>

There is also a possibility to break a string into several lines.

<lang seed7>var string: example is "this is a string\

                      \ which continues in the next line\n\
                      \and contains a line break";</lang>

There is no built-in mechanism for expanding variables within strings.

Sidef

Quotes that do not interpolate: <lang ruby>'single quotes with \'embedded quote\' and \\backslash'; ‚unicode single quoted’; %q(not interpolating with (nested) parentheses and newline);</lang>

Quotes that interpolate: <lang ruby>var a = 42; "double \Uquotes\E with \"embedded quote\"\nnewline and variable interpolation: #{a} % 10 = #{a % 10}"; „same as above”; %Q(same as above);</lang>

Heredocs: <lang ruby>print <<EOT Implicit double-quoted (interpolates): a = #{a} EOT

print <<"EOD" Explicit double-quoted with interpolation: a = #{a} EOD

print <<'NON_INTERPOLATING' This will not interpolate: #{a} NON_INTERPOLATING</lang>

Slate

Characters are specified using the $ syntax:

<lang slate>$a $D $8 $, $\s $\n</lang>

Strings are contained in single quotes, with backslash for escaping: <lang slate>'Hello\'s the word.'</lang>

Standard ML

Characters are contained in the #"" syntax: <lang sml>- #"a"; val it = #"a" : char</lang>

Strings are contained in double quotes: <lang sml>- "Hello world"; val it = "Hello world" : string</lang>

Strings may be split across lines and concatenated by having two backslashes around the newline and whitespace: <lang sml>- "abc\

   \def";

val it = "abcdef" : string</lang>

Tcl

Tcl makes no distinction between single characters and strings.

Double quotes allow command and variable interpolation: <lang tcl>set str "This is Tcl $::tcl_version\tIt is [clock format [clock seconds]]" puts $str ;# ==> This is Tcl 8.5 It is Mon Apr 06 16:49:46 EDT 2009</lang>

Braces prevent interpolation <lang tcl>set str {This is Tcl $::tcl_version\tIt is [clock format [clock seconds]]} puts $str ;# ==> This is Tcl $::tcl_version\tIt is [clock format [clock seconds]]</lang>

TI-89 BASIC

Double quotes enclose strings, e.g. "Hello Rosetta Code". There are no escape characters. Quotes in strings are doubled: "This > "" < is one double-quote."

TUSCRIPT

<lang tuscript> $$ MODE TUSCRIPT,{} s1=* DATA "string" s2=* DATA + "double" quotes s3=* DATA + 'single' quotes s4=* DATA + "double" + 'single' quotes show=JOIN(s1," ",s2,s3,s4) show=JOIN(show) PRINT show </lang> Output:

"string" + "double" quotes + 'single' quotes + "double" + 'single' quotes

UNIX Shell

Works with: Bourne Shell
Works with: bash

The Unix shell supports several types of quotation marks:

  • singlequotes - for literal string quotation
  • doublequotes - for interpolated string quotation
  • backticks - Used to capture the output from an external program

Quotation marks within a literal String

It is possible to place singlequote characters within a string enclosed with doublequotes and to put doublequote characters in a string enclosed within singlequotes: <lang bash>echo "The boy said 'hello'." echo 'The girl said "hello" too.'</lang>

We can also use an escapesequence to put doublequote characters in an interpolated string:

<lang bash>print "The man said \"hello\".";</lang>

Here documents

The shell supports the use of here documents for the passing of quoted text as input into a command. Here documents cannot be used to represent literal strings as an expression for variable assignment.

<lang bash>cat << END 1, High Street, SMALLTOWN, West Midlands. WM4 5HD. END</lang>

Ursala

Single characters are denoted with a back quote. <lang Ursala>a = `x</lang> Unprintable character constants can be expressed like this. <lang Ursala>cr = 13%cOi&</lang> Strings are enclosed in single forward quotes. <lang Ursala>b = 'a string'</lang> A single quote in a string is escaped by another single quote. <lang Ursala>c = 'Hobsons choice'</lang> Multi-line strings are enclosed in dash-brackets. <lang Ursala>d =

-[this is a list of strings]-</lang> Dash-bracket enclosed text can have arbitrary nested unquoted expressions, provided they evaluate to lists of character strings. <lang Ursala>e = -[the front matter -[ d ]- the rest of it]-

f = -[text -[ d ]- more -[ e ]- text ]-</lang> This notation can also be used for defining functions. <lang Ursala>g "x" = -[ Dear -[ "x" ]- bla bla ]-</lang> The double quotes aren't for character strings but dummy variables.

V

A simple quoted string is of the form 'string' e.g <lang v>'hello world' puts</lang>

Vim Script

A string constant delimited by double quotes " may contain escape sequences like \n, \t, \123 (byte value in octal), \xab (byte value in hexadecimal), \\ (backslash), \" or \u12ff (character code in hexadecimal according to the current encoding).

If a string constant is delimited by single quotes ' all characters are taken as they are. In order to use a single quote inside a literal string it must be escaped with another single quote, i.e. two single quotes stand for one.

Strings must always end at the current line and characters are just strings of length one.

Visual Basic .NET

Visual Basic only supports single-line strings. The only escape sequence supported is the double double-quote (""), which is translated into a single double-quote.

<lang vbnet>Dim s = "Tom said, ""The fox ran away.""" Result: Tom said, "The fox ran away."</lang>

XPL0

The literal value of a character is specified by preceding it with a
caret. For example, this assigns the ASCII value of A (hex 41) to Ch:

Ch:= ^A;

Strings are one-dimensional byte arrays. For example, this assigns the
address of the string enclosed in quote marks to Str:

Str:= "Hello, world!";

The ASCII code for each character is stored in sequential bytes. By
default strings are terminated with the most significant bit set on the
last character. The exclamation point would be stored as hex A1. Strings
can also be terminated with a zero byte (like in the C language). If the
command:

string 0;

occurs anywhere before a string is set up then it will have an extra zero
byte at the end.

A quote mark can be included in a string by preceding it with a caret.
Carets are also included this way. For example:

"^"^^^" is a ^"caret^""

results in:

"^" is a "caret"

Carets can also be used to specify control characters. For example, this
is escape E (hex 1B C5):

"^[E"

Strings can any length and can span lines, for example:

"Hello,
world!"

A carriage return (hex 0D) and line feed (hex 0A) are in the middle.

Strings are output to various devices (such as the console screen or
printer) with the Text intrinsic.

XSLT

XSLT is based on XML, and so can use either " or ' to delimit strings. Since XML attribute values are defined using double-quotes, one must use single-quotes for string literals within attributes. <lang xml><xsl:if test="starts-with(@name, 'Mr.')">Mister</xsl:if></lang>

Double and single quote characters may also be escaped with XML entities: &quot; and &apos; respectively.

zkl

Interpreted string: "hoho". Raw string: 0'|hoho| where | is user choosen. \b, \f, \n, \r, \t, \e escapes are supported. Two adjacent strings are treated as one: "foo" 0'~bar~ --> "foobar". No variable expansion.

here-strings: <lang zkl>text:= 0'|foo| "bar\n";</lang>

Output:
"foobar\n"

<lang zkl>n:=7; text:=String(

  "foo = ",3,"\n"
  "bar=",n,"\n"

);</lang>

Output:
text = "foo = 3\nbar=7"