View Subcategory

Define a string containing special characters

Define the literal string "\#{'}${"}/"
ruby
special = '\#{\'}${"}/'
java
String special = "\\#{'}${\"}/";
perl
$special = '\#{\'}${"}/';
$special = q(\#{'}${"}/);
groovy
special = "\\#{'}\${\"}/"
special = '\\#{\'}${"}/'
special = /\#{'}${'$'}{"}\//
assert '42'.isNumber()
scala
val special = "\\#{'}${\"}/"
val special2 = """\#{'}${"}/"""
python
# yes, Python has way too many forms of string literals :)
print "\\#{'}${\"}/"
print "\\#{'}${"'"'"}/"
print r"""\#{'}${"}/"""
print '\\#{\'}${"}/'
print '\\#{'"'"'}${"}/'
print r'''\#{'}${"}/'''
cpp
std::string special = "\\#{'}${\"}/";
String^ special = L"\\#{'}${\"}/";
fsharp
let special = "\#{'}${\"}/"
erlang
Special = "\\#{'}\${\"}/",
ocaml
"\\#{'}${\"}/"
csharp
string verbatim = @"\#{'}${""""}/";
string cStyle = "\\#{'}${\"\"}/";
php
$special = "\\#{'}\${\"}/";
$special = '\#{\'}${"}/';
haskell
putStrLn "\"\\#{'}${\"}/\""
let special = "\\#{'}${\"}/"
clojure
(def special "\\#{'}${\"}/")
fantom
special := Str<|\#{'}${"}/|>
go
s := "\\#{'}${\"}/"
s := `"\#{'}${"}/"`

Define a multiline string

Define the string:
"This
Is
A
Multiline
String"
ruby
text = <<"HERE"
This
Is
A
Multiline
String
HERE
text = "This\nIs\nA\nMultiline\nString"
java
String text = "This\nIs\nA\nMultiline\nString";
String text =
"This\n" +
"Is\n" +
"A\n" +
"Multiline\n" +
"String"
perl
$text = 'This
Is
A
Multiline
String';
$text = <<EOF;
This
Is
A
Multiline
String
EOF
groovy
def text =
"""This
Is
A
Multiline
String"""
def text = "This\nIs\nA\nMultiline\nString"
scala
val text = """This
Is
A
Multiline
String"""
val text = "This\nIs\nA\nMultiline\nString"
python
text = """This
Is
A
Multiline
String"""
# with proper indentation
text = (
"This\n"
"Is\n"
"A\n"
"Multiline\n"
"String"
)
cpp
std::string text =
"This\n"
"Is\n"
"A\n"
"Multiline\n"
"String";
String^ text = L"This\nIs\nA\nMultiline\nString";
std::string text = "This\nIs\nA\nMultiline\nString";
fsharp
let multiline = "This\nIs\nA\nMultiline\nString"
let multiline = "This
Is
A
Multiline
String"
erlang
Text = "This\nIs\nA\nMultiline\nString",
ocaml
"This\nIs\nA\nMultiline\nString"
"This
Is
A
Multiline
String"
csharp
string output = "This\nIs\nA\nMultiline\nString";
string output = @"This
Is
A
Multiline
String";
php
$multiline = <<<ML
This
Is
A
Multiline
String
ML;
$multiline = "This
Is
A
Multiline
String";
$multiline = "This\nIs\nA\nMultiline\nString";
haskell
s = "This \
\Is \
\A \
\Multiline \
\String"
clojure
(def multiline "This\nIs\nA\nMultiline\nString")
fantom
s := "This
Is
A
Multiline
String"
go
text := "This\nIs\nA\nMultiline\nString\n"
text := "This\n" + "Is\n" + "A\n" + "Multiline\n" + "String\n"
var s = `This
Is
A
Multiline
String`

Define a string containing variables and expressions

Given variables a=3 and b=4 output "3+4=7"
ruby
puts "#{a}+#{b}=#{a+b}"
puts "#{a}+#{b}=%s" % (a + b)
java
System.out.println(a + "+" + b + "=" + (a+b));
System.out.printf("%d+%d=%d\n", a, b, a + b);
perl
print "$a+$b=${\($a+$b)}\n";
sprintf("%d+%d=%d", $a, $b, $a + $b);
print $a, '+', $b, '=', $a + $b;
groovy
println "$a+$b=${a+b}"
printf "%d+%d=%d\n", a, b, a + b
scala
printf("%d+%d=%d\n", a, b, a + b)
"%d+%d=%d".format(a, b, a + b)
s"$a + $b = ${a+b}"
python
class EvalDict(dict):
def __getitem__(s, k):
return eval(k, s)

a=3; b=4
"%(a)d+%(b)d=%(a+b)d" % EvalDict(locals())
a=3; b=4
"%d+%d=%d" % (a, b, a+b)
cpp
Console::WriteLine(L"{0}+{1}={2}", a, b, a+b);
std::printf("%d+%d=%d\n", a, b, a+b);
std::cout << boost::format("%|1|+%|1|=%|1|") % a % b % (a+b) << std::endl;
fsharp
let a, b = 3, 4
let mystr = sprintf "%d+%d=%d" a b (a+b)
printfn "%s" mystr
erlang
A = 3, B = 4,
io:format("~B+~B=~B~n", [A, B, (A+B)]).
ocaml
Printf.printf "%d+%d=%d" a b (a+b);;
Printf.printf "%d+%d=%d" a b (a+b);;
csharp
int a = 3;
int b = 4;
Console.WriteLine("{0}+{1}={2}", a,b,a+b);
php
echo "$a+$b=".($a+$b);
printf("%d+%d=%d\n", $a, $b, $a + $b);
haskell
import Text.Printf

main = do
let a = 3
let b = 4
printf "%d+%d=%d" a b (a + b)
a = 3
b = 4
s = show a ++ "+" ++ show b ++ "=" ++ show (a + b)
main = putStrLn s
clojure
(format "%d + %d = %d" a b (+ a b))
fantom
echo("$a+$b=${a+b}")
go
a, b := 3, 4
fmt.Printf("%d+%d=%d\n", a, b, a + b)
a, b := 3, 4
fmt.Println(a, "+", b, "=", a+b)