String prepend: Difference between revisions
(Added Erlang) |
Catskill549 (talk | contribs) |
||
Line 17: | Line 17: | ||
012345678 |
012345678 |
||
</pre> |
</pre> |
||
=={{header|AWK}}== |
|||
<lang AWK> |
|||
# syntax: GAWK -f STRING_PREPEND.AWK |
|||
BEGIN { |
|||
s = "bar" |
|||
s = "foo" s |
|||
print(s) |
|||
exit(0) |
|||
} |
|||
</lang> |
|||
<p>output:</p> |
|||
<pre> |
|||
foobar |
|||
</pre> |
|||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
<lang BBC BASIC>S$ = " World!" |
<lang BBC BASIC>S$ = " World!" |
Revision as of 00:41, 23 October 2013
You are encouraged to solve this task according to the task description, using any language you may know.
Basic Data Operation
This is a basic data operation. It represents a fundamental action on a basic data type.
You may see other such operations in the Basic Data Operations category, or:
Integer Operations
Arithmetic |
Comparison
Boolean Operations
Bitwise |
Logical
String Operations
Concatenation |
Interpolation |
Comparison |
Matching
Memory Operations
Pointers & references |
Addresses
Create a string variable equal to any text value. Prepend the string variable with another string literal. If your language supports any idiomatic ways to do this without referring to the variable twice in one expression, include such solutions.
To illustrate the operation, show the content of the variable.
ALGOL 68
File: String_prepend.a68<lang algol68>#!/usr/bin/a68g --script #
- -*- coding: utf-8 -*- #
STRING str := "12345678"; "0" +=: str; print(str)</lang>Output:
012345678
AWK
<lang AWK>
- syntax: GAWK -f STRING_PREPEND.AWK
BEGIN {
s = "bar" s = "foo" s print(s) exit(0)
} </lang>
output:
foobar
BASIC
<lang BBC BASIC>S$ = " World!" S$ = "Hello" + S$ PRINT S$ </lang>
- Output:
Hello World!
Applesoft BASIC
BBC BASIC
D
<lang d>import std.stdio;
void main() {
string s = "world!"; s = "Hello " ~ s; writeln(s);
}</lang>
- Output:
Hello world!
Erlang
- Output:
1> S = "world". "world" 2> "Hello " ++ S. "Hello world"
J
<lang j> s=: 'value'
s
value
s=: 'new ',s s
new value</lang>
Java
<lang java>String s = "value"; System.out.println(s); s = "new " + s; System.out.println(s);
StringBuilder sb = new StringBuilder("value"); System.out.println(sb); sb.insert(0, "new "); System.out.println(sb);</lang> Output:
value new value value new value
NetRexx
<lang NetRexx>s_ = 'world!' s_ = 'Hello, 's_ say s_</lang>
- Output:
Hello, world!
Perl 6
<lang perl6># explicit concatentation $_ = 'byte'; $_ = 'kilo' ~ $_; .say;
- interpolation as concatenation
$_ = 'buck'; $_ = "mega$_"; .say;
- lvalue substr
$_ = 'bit'; substr-rw($_,0,0) = 'nano'; .say;
- regex substitution
$_ = 'fortnight'; s[^] = 'micro'; .say;
- reversed append assignment
$_ = 'cooper'; $_ [R~]= 'mini'; .say;</lang>
- Output:
kilobyte megabuck nanobit microfortnight minicooper
Python
File: String_prepend.py<lang python>#!/usr/bin/env python
- -*- coding: utf-8 -*- #
str = "12345678"; str = "0" + str; # by concatination # print(str)</lang>Output:
012345678
Racket
<lang racket>;there is no built-in way to set! prepend in racket (define str "foo") (set! str (string-append "bar " str)) (displayln str)
- but you can create a quick macro to solve that problem
(define-syntax-rule (set-prepend! str value)
(set! str (string-append value str)))
(define macrostr " bar") (set-prepend! macrostr "foo") (displayln macrostr) </lang>
- Output:
bar foo foo bar
REXX
<lang rexx>s='llo world!' s='he's Say s </lang> Output:
hello world!
Ruby
There is a method for prepending a string, aptly named "prepend". <lang ruby>str = "llo world" str.prepend("He") p str #=> "Hello world"</lang>
Tcl
Concatenation is a fundamental feature of Tcl's basic language syntax. <lang tcl>set s "llo world" set s "he$s" puts $s</lang>
- Output:
hello world
Wart
<lang python>s <- "12345678" s <- ("0" + s)</lang>