String prepend: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
Line 35: Line 35:
{{out}}
{{out}}
<pre>Hello world!</pre>
<pre>Hello world!</pre>

=={{header|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>
{{out}}
<pre>kilobyte
megabuck
nanobit
microfortnight
minicooper</pre>


=={{header|Python}}==
=={{header|Python}}==

Revision as of 00:54, 4 October 2013

Task
String prepend
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.

To illustrate the operation, show the content of the variable.

ALGOL 68

Works with: ALGOL 68 version Revision 1.
Works with: ALGOL 68G version Any - tested with release algol68g-2.7.
Works with: ELLA ALGOL 68 version Any (with appropriate job cards).

File: String_prepend.a68<lang algol68>#!/usr/bin/a68g --script #

  1. -*- coding: utf-8 -*- #

STRING str := "12345678"; "0" +=: str; print(str)</lang>Output:

012345678

BBC BASIC

<lang BBC BASIC> S$=" World!"

     S$="Hello"+S$
     PRINT S$
     END</lang>
Output:
Hello World!

D

<lang d>import std.stdio;

void main() {

   string s = "world!";
   s = "Hello " ~ s; 
   writeln(s);

}</lang>

Output:
Hello world!

Perl 6

<lang perl6># explicit concatentation $_ = 'byte'; $_ = 'kilo' ~ $_; .say;

  1. interpolation as concatenation

$_ = 'buck'; $_ = "mega$_"; .say;

  1. lvalue substr

$_ = 'bit'; substr-rw($_,0,0) = 'nano'; .say;

  1. regex substitution

$_ = 'fortnight'; s[^] = 'micro'; .say;

  1. 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

  1. -*- coding: utf-8 -*- #

str = "12345678"; str = "0" + str; # by concatination # print(str)</lang>Output:

012345678

REXX

<lang rexx>s='llo world!' s='he's Say s </lang> Output:

hello world!

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 wart>s <- "12345678" s <- ("0" + s)</lang>