String append
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
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
Most languages provide a way to concatenate two string values, but some languages also provide a convenient way to append in-place to an existing string variable without referring to the variable twice. For this task, create a string variable equal to any text value. Append the string variable with another string literal in the most idiomatic way, without double reference if your language supports it.
Show the contents of the variable after the append operation.
ALGOL 68
File: String_append.a68<lang algol68>#!/usr/bin/a68g --script #
- -*- coding: utf-8 -*- #
STRING str := "12345678"; str +:= "9!"; print(str)</lang>Output:
123456789!
BASIC
Applesoft BASIC
<lang BASIC>S$ = "Hello" S$ = S$ + " World!" PRINT S$</lang>
BBC BASIC
<lang BBC BASIC> S$="Hello"
S$+=" World!" PRINT S$ END</lang>
- Output:
Hello World!
C#
<lang csharp>class Program {
static void Main(string[] args) { string x = "foo"; x += "bar"; System.Console.WriteLine(x); }
}</lang>
D
<lang d>import std.stdio;
void main() {
string s = "Hello"; s ~= " world!"; writeln(s);
}</lang>
- Output:
Hello world!
F#
Strings are immutable in .NET. To append (to the same variable) the variable has to be declared mutable. <lang fsharp>let mutable x = "foo" x <- x + "bar" printfn "%s" x</lang>
NetRexx
<lang NetRexx>s_ = 'Hello' s_ = s_', world!' say s_</lang>
- Output:
Hello, world!
Perl 6
<lang perl6>my $str = "foo"; $str ~= "bar"; say $str;</lang>
- Output:
foobar
Python
File: String_append.py<lang python>#!/usr/bin/env python
- -*- coding: utf-8 -*- #
str = "12345678"; str += "9!"; print(str)</lang>Output:
123456789!
Racket
<lang racket>;there is no built-in way to set! append in racket (define mystr "foo") (set! mystr (string-append mystr " bar")) (displayln mystr)
- but you can create a quick macro to solve that problem
(define-syntax-rule (set-append! str value)
(set! str (string-append str value)))
(define mymacrostr "foo") (set-append! mymacrostr " bar") (displayln mystr)</lang>
REXX
<lang rexx>s='he' s=s'llo world!' Say s</lang> Output:
hello world!
Ruby
<lang ruby>s = "Hello wo" s += "rld" # new string object s << "!" # mutates in place, same object puts s</lang>
- Output:
Hello world!
Tcl
String concatenation is a fundamental feature of the Tcl language, and there is also an append
that makes concatenation even simpler:
<lang tcl>set s "he"
set s "${s}llo wo"; # The braces distinguish varname from text to concatenate
append s "rld"
puts $s</lang>
- Output:
hello world
Wart
<lang python>s <- "12345678" s <- (s + "9!")</lang>