Copy a string

From Rosetta Code

Jump to: navigation, search
Task
Copy a string
You are encouraged to solve this task according to the task description, using any language you may know.

This task is about copying a string. Where it is relevant, distinguish between copying the contents of a string versus making an additional reference to an existing string.

Contents

[edit] ActionScript

Strings are immutable in ActionScript, and can safely be assigned with the assignment operator, much as they can in Java.[1]

var str1:String = "Hello";
var str2:String = str1;

[edit] Ada

Ada provides three different kinds of strings. The String type is a fixed length string. The Bounded_String type is a string with variable length up to a specified maximum size. The Unbounded_String type is a variable length string with no specified maximum size. The Bounded_String type behaves a lot like C strings, while the Unbounded_String type behaves a lot like the C++ String class.

[edit] Fixed Length String Copying.

Src : String := "Hello";
Dest : String := Src;

Ada provides the ability to manipulate slices of strings.

Src : String "Rosetta Stone";
Dest : String := Src(1..7); -- Assigns "Rosetta" to Dest
Dest2 : String := Src(9..13); -- Assigns "Stone" to Dest2

[edit] Bounded Length String Copying

-- Instantiate the generic package Ada.Strings.Bounded.Generic_Bounded_Length with a maximum length of 80 characters
package Flexible_String is new Ada.Strings.Bounded.Generic_Bounded_Length(80);
use Flexible_String;
 
Src : Bounded_String := To_Bounded_String("Hello");
Dest : Bounded_String := Src;

Ada Bounded_String type provides a number of functions for dealing with slices.

[edit] Unbounded Length String Copying

-- The package Ada.Strings.Unbounded contains the definition of the Unbounded_String type and all its methods
Src : Unbounded_String := To_Unbounded_String("Hello");
Dest : Unbounded_String := Src;

[edit] ALGOL 68

In ALGOL 68 strings are simply flexible length arrays of CHAR;

(
STRING src:="Hello", dest;
dest:=src
)

[edit] AutoHotkey

src := "Hello"
dst := src

[edit] AWK

BEGIN {
a = "a string"
b = a
sub(/a/, "X", a) # modify a
print b # b is a copy, not a reference to...
}

[edit] BASIC

Works with: QuickBasic version 4.5 Works with: PB version 7.1

 src$ = "Hello"
 dst$ = src$

[edit] Batch File

Since the only variables are environment variables, creating a string copy is fairly straightforward:

set src=Hello
set dst=%src%

[edit] C

[edit] Using strdup

const char* src = "Hello";
char* dst = strdup(src);

[edit] Using malloc/strcpy

const char* src = "Hello";
int len = strlen(src);
char* dst = malloc(len+1);
strcpy(dst, src);

[edit] Using malloc/strncpy

const char* src = "Hello";
int len = strlen(src);
char* dst = malloc(len+1);
strncpy(dst, src, len+1); dst[len] = 0;

[edit] Using static buffer

const char* src= "Hello";
static char dst[80];
strncpy(dst, src, 80);
dst[79] = 0;

[edit] Creating another reference to an existing string

const char* src = "Hello";
const char* dst;
dst = src; /* now dst refers top the same string as src */

The same works for mutable strings, e.g.

char[] src = "Hello";
char* dst;
dst = src;
dst[1] = 'a'; // Now src contains "Hallo"

[edit] C++

[edit] Using only standard facilities

Library: STL

#include <string>
std::string src = "Hello";
std::string dst = src;

Standard C++ strings are not guaranteed to be stored contiguous, as needed for C functions, and it's only possible to get pointers to constant C strings. Therefore if the C function modifies the string, it has to be copied into a C string. There are several ways, depending on the needs:

Using a std::vector allows C++ to manage the memory of the copy. Since C++2003 (TR1), std::vector is guaranteed to be contiguous; but even before, there was no implementation which wasn't.

#include <string>
#include <vector>
 
std::string s = "Hello";
{
// copy into a vector
std::vector<char> string_data(s.begin(), s.end());
// append '\0' because C functions expect it
string_data.push_back('\0');
// call C function
modify(&string_data[0]);
// copy resulting C string back to s
s = (&string_data[0]);
} // string_data going out of scope -> the copy is automatically released

Another way is to manage your memory yourself and use C functions for the copy:

#include <string>
#include <cstring> // For C string functions (strcpy)
 
std::string s = "Hello";
// allocate memory for C string
char* cs = new char[s.length() + 1];
// copy string into modifyable C string (s.c_str() gives constant C string)
std::strcpy(cs, s.c_str());
// call C function
modify(cs);
// copy result back into s
s = cs;
// get rid of temporary buffer
delete[] cs;

If the C function calls free or realloc on its argument, new[] cannot be used, but instead malloc must be called:

#include <string>
#include <cstring>
#include <cstdlib> // for malloc
 
std::string s = "Hello";
// allocate memory for C string
char* cs = (char*)malloc(s.length()+1);
// copy the string
std::strcpy(cs, s.c_str());
// call C function which may call realloc
modify(&cs);
// copy result back to s
s = cs;
// get rid of temporary buffer
free(cs); // delete or delete[] may not be used here

[edit] Using string types supplied by specific compiler-provided or external libraries

Library: Qt

// Qt
QString src = "Hello";
QString dst = src;

Library: Microsoft Foundation Classes

// MFC
CString src = "Hello";
CString dst = src;

[edit] C#

string src = "Hello";
string dst = src;

[edit] ColdFusion

In ColdFusion, only complex data types (structs, objects, etc.) are passed by reference. Hence, any string copy operations are by value.

<cfset stringOrig = "I am a string." />
<cfset stringCopy = stringOrig />

[edit] Clojure

 
(let [s "hello"
s1 s]
(println s s1))
 

[edit] Common Lisp

(let* ((s1     "Hello")        ; s1 is a variable containing a string
(s1-ref s1) ; another variable with the same value
(s2 (copy-seq s1))) ; s2 has a distinct string object with the same contents
(assert (eq s1 s1-ref)) ; same object
(assert (not (eq s1 s2))) ; different object
(assert (equal s1 s2)) ; same contents
 
(fill s2 #\!) ; overwrite s2
(princ s1)
(princ s2)) ; will print "Hello!!!!!"

[edit] D

string src = "string";
 
// copy contents:
auto dest = src.dup;
 
// copy reference with copy-on-write:
auto dest2 = src;

[edit] E

E is a pass-references-by-value object-oriented language, and strings are immutable, so there is never a need for or benefit from copying a string. Various operations, such as taking the substring (run) from the beginning to the end (someString.run(0)) might create a copy, but this is not guaranteed.

[edit] Erlang

Src = "Hello".
Dst = Src.

[edit] Factor

Factor strings are mutable but not growable. Strings will be immutable in a future release.

"This is a mutable string." dup ! reference
"Let's make a deal!" dup clone  ! copy
"New" " string" append .  ! new string
"New string"

Factor string buffers (sbufs) are mutable and growable.

SBUF" Grow me!" dup "  OK." append
SBUF" Grow me! OK."

Convert a string buffer to a string.

SBUF" I'll be a string someday." >string .
"I'll be a string someday."

[edit] Forth

Forth strings are generally stored in memory as prefix counted string, where the first byte contains the string length. However, on the stack they are most often represented as <addr cnt> pairs. Thus the way you copy a string depends on where the source string comes from:

\ Allocate two string buffers
create stringa 256 allot
create stringb 256 allot
 
\ Copy a constant string into a string buffer
s" Hello" stringa place
 
\ Copy the contents of one string buffer into another
stringa count stringb place

[edit] Fortran

str2 = str1

Because Fortran uses fixed length character strings if str1 is shorter than str2 then str2 is padded out with trailing spaces. If str1 is longer than str2 it is truncated to fit.

[edit] F#

let f =
let mutable s0 = "Hello"
let s1 = s0
s0 <- "Goodbye"
printfn "%s %s" s0 s1

prints Goodbye Hello when run.

[edit] Go

Just use assignment:

src := "Hello"
dst := src

Note that strings in Go are immutable.

[edit] Groovy

The dynamics of references and object creation are very much the same as in Java. However, the meaning of the equality (==) operator is different in Groovy, so we show those differences here, even though they are not relevant to the actual copying.

Example and counter-example:

def string = 'Scooby-doo-bee-doo'    // assigns string object to a variable reference
def stringRef = string // assigns another variable reference to the same object
def stringCopy = new String(string) // copies string value into a new object, and assigns to a third variable reference

Test Program:

assert string == stringRef           // they have equal values (like Java equals(), not like Java ==)
assert string.is(stringRef) // they are references to the same objext (like Java ==)
assert string == stringCopy // they have equal values
assert ! string.is(stringCopy) // they are references to different objects (like Java !=)

Caveat Lector: Strings are immutable objects in Groovy, so it is wasteful and utterly unnecessary to ever make copies of them within a Groovy program.

[edit] Haskell

In Haskell, every value is immutable, including Strings. So one never needs to copy them; references are shared.

[edit] HicEst

src = "Hello World"
dst = src

[edit] Icon and Unicon

[edit] Icon

Strings in Icon are immutable.

procedure main()
a := "qwerty"
b := a
b[2+:4] := "uarterl"
write(a," -> ",b)
end

Under the covers 'b' is created as a reference to the same string as 'a'; the sub-string assignment creates a new copy of the string. However, there is no way to tell this in the language. While most of the time this is transparent, programs that create very long strings through repeated concatenation need to avoid generating intermediate strings. Instead using a list and concatenating at the last minute can perform much better.

Note that strings are indicated using double quotes. However, single quotes are another type called character sets or csets.

[edit] Unicon

Strings in Unicon behave the same as in Icon.

[edit] J

src  =: 'hello'
dest =: src

J has copy-on-write semantics. So both src and dest are references to the same memory, until src changes, at which time dest retains a copy of the original value of src.

[edit] Java

In Java, Strings are immutable, so it doesn't make that much difference to copy it.

String src = "Hello";
String newAlias = src;
String strCopy = new String(src);
 
//"newAlias == src" is true
//"strCopy == src" is false
//"strCopy.equals(src)" is true

Instead, maybe you want to create a StringBuffer (mutable string) from an existing String or StringBuffer:

StringBuffer srcCopy = new StringBuffer("Hello");

[edit] JavaScript

var src = "Hello";
var dst = '' + src;

Note that '' + src concatenation was used to make sure that the string is actually copied instead of being passed by reference, but since strings in JavaScript are immutable and always compared by value, there is no way to test whether any two given variables are both references to the same string or two distinct strings which just happen to have the same value. So in practice this code is exactly equivalent:

var src = "Hello";
var dst = src;

even though the string might or might not have been actually copied.

[edit] Joy

"hello" dup

Strings are immutable.

[edit] Lisaac

+ scon : STRING_CONSTANT;
+ svar : STRING;
 
scon := "sample";
svar := STRING.create 20;
svar.copy scon;
svar.append "!\n";
 
svar.print;

STRING_CONSTANT is immutable, STRING is not.

[edit] Logo

As a functional language, words are normally treated as symbols and cannot be modified. The EQUAL? predicate compares contents instead of identity. In UCB Logo the .EQ predicate tests for "thing" identity.

make "a "foo
make "b "foo
print .eq :a :b  ; true, identical symbols are reused
 
make "c :a
print .eq :a :c  ; true, copy a reference
 
make "c word :b "||  ; force a copy of the contents of a word by appending the empty word
print equal? :b :c  ; true
print .eq :b :c  ; false

[edit] Lua

Lua strings are immutable, so only one reference to each string exists.

 
a = "string"
b = a
print(a == b) -->true
print(b) -->string

[edit] Mathematica

a="Hello World"
b=a

[edit] MATLAB

string1 = 'Hello';
string2 = string1;

[edit] MAXScript

str1 = "Hello"
str2 = copy str1

[edit] Metafont

Metafont will always copy a string (does not make references).

string s, a;
s := "hello";
a := s;
s := s & " world";
message s;  % writes "hello world"
message a;  % writes "hello"
end

[edit] Modula-3

Strings in Modula-3 have the type TEXT.

VAR src: TEXT := "Foo";
VAR dst: TEXT := src;

[edit] MUMPS

SET S1="Greetings, Planet"
SET S2=S1

[edit] Objective-C

Immutable strings - since they are immutable, you may get the same instance with its references count increased. Or, you can get a copy which is mutable.

NSString *original = @"Literal String";
NSString *new = [original copy];
NSString *newMutable = [original mutableCopy];

Mutable strings - you can get either new mutable or immutable string:

NSMutableString *original = [NSMutableString stringWithString:@"Literal String"];
NSString *immutable = [original copy];
NSMutableString *mutable = [original mutableCopy];

Copying a CString:

const char *cstring = "I'm a plain C string";
NSString *string = [NSString stringWithUTF8String:cstring];

Copying from data, possibly not null terminated:

char bytes[] = "some data";
NSString *string = [[NSString alloc] initWithBytes:bytes length:9 encoding:NSASCIIStringEncoding];

And of course, if a C string is needed, you can use standard functions like strcpy.

[edit] Objeck

 
a := "GoodBye!";
b := a;
 

[edit] OCaml

let dst = String.copy src

[edit] Octave

str2 = str1

[edit] Pascal

 
program in,out;
 
type
 
pString = ^string;
 
var
 
s1,s2 : string ;
pStr : pString ;
 
begin
 
/* direct copy */
s1 := 'Now is the time for all good men to come to the aid of their party."
s2 := s1 ;
 
writeln(s1);
writeln(s2);
 
/* By Reference */
pStr := @s1 ;
writeln(pStr^);
 
pStr := @s2 ;
writeln(pStr^);
 
end;


[edit] Perl

To copy a string, just use ordinary assignment:

my $original = 'Hello.';
my $new = $original;
$new = 'Goodbye.';
print "$original\n"; # prints "Hello."

To create a reference to an existing string, so that modifying the referent changes the original string, use a backslash:

my $original = 'Hello.';
my $ref = \$original;
$$ref = 'Goodbye.';
print "$original\n"; # prints "Goodbye."

If you want a new name for the same string, so that you can modify it without dereferencing a reference, assign a reference to a typeglob:

my $original = 'Hello.';
our $alias;
local *alias = \$original;
$alias = 'Good evening.';
print "$original\n"; # prints "Good evening."

Note that our $alias, though in most cases a no-op, is necessary under stricture. Beware that local binds dynamically, so any subroutines called in this scope will see (and possibly modify!) the value of $alias assigned here.

To make a lexical variable that is an alias of some other variable, the Lexical::Alias module can be used:

use Lexical::Alias;
my $original = 'Hello.';
my $alias;
alias $alias, $original;
$alias = 'Good evening.';
print "$original\n"; # prints "Good evening."

[edit] PHP

$src = "Hello";
$dst = $src;

[edit] PicoLisp

(setq Str1 "abcdef")
(setq Str2 Str1) # Create a reference to that symbol
(setq Str3 (name Str1)) # Create new symbol with name "abcdef"

[edit] Pike

int main(){
string hi = "Hello World.";
string ih = hi;
}

[edit] PostScript

In PostScript,

(hello) dup length string copy

[edit] Pop11

In Pop11 normal data are represented by references, so plain assignment will copy references. To copy data one has to use copy procedure:

vars src, dst;
'Hello' -> src;
copy(src) -> dst;

One can also combine assignment (initialization) with variable declarations:

vars src='Hello';
vars dst=copy(src);

[edit] PowerShell

Since PowerShell uses .NET behind the scenes and .NET strings are immutable you can simply assign the same string to another variable without breaking anything:

$str = "foo"
$dup = $str

To actually create a copy the Clone() method can be used:

$dup = $str.Clone()

[edit] PureBasic

src$ = "Hello"
dst$ = src$

[edit] Python

Works with: Python version 2.3, 2.4, and 2.5 Since strings are immutable, all copy operations return the same string. Probably the reference is increased.

>>> src = "hello"
>>> a = src
>>> b = src[:]
>>> import copy
>>> c = copy.copy(src)
>>> d = copy.deepcopy(src)
>>> src is a is b is c is d
True

To actually copy a string:

>>> a = 'hello'
>>> b = ''.join(a)
>>> a == b
True
>>> b is a ### Might be True ... depends on "interning" implementation details!
False

As a result of object "interning" some strings such as the empty string and single character strings like 'a' may be references to the same object regardless of copying. This can potentially happen with any Python immutable object and should be of no consequence to any proper code.

Be careful with is - use it only when you want to compare the identity of the object. To compare string values, use the == operator. For numbers and strings any given Python interpreter's implementation of "interning" may cause the object identities to coincide. Thus any number of names to identical numbers or strings might become references to the same objects regardless of how those objects were derived (even if the contents were properly "copied" around). The fact that these are immutable objects makes this a reasonable behavior.

[edit] R

Copy a string by value:

str1 <- "abc"
str2 <- str1

[edit] REBOL

rebol [
Title: "String Copy"
Date: 2009-12-16
Author: oofoe
URL: http://rosettacode.org/wiki/Copy_a_string
]

 
x: y: "Testing."
y/2: #"X"
print ["Both variables reference same string:" mold x "," mold y]
 
x: "Slackeriffic!"
print ["Now reference different strings:" mold x "," mold y]
 
y: copy x ; String copy here!
y/3: #"X" ; Modify string.
print ["x copied to y, then modified:" mold x "," mold y]
 
y: copy/part x 7 ; Copy only the first part of y to x.
print ["Partial copy:" mold x "," mold y]
 
y: copy/part skip x 2 3
print ["Partial copy from offset:" mold x "," mold y]

Output:

Script: "String Copy" (16-Dec-2009)
Both variables reference same string: "TXsting." , "TXsting."
Now reference different strings: "Slackeriffic!" , "TXsting."
x copied to y, then modified: "Slackeriffic!" , "SlXckeriffic!"
Partial copy: "Slackeriffic!" , "Slacker"
Partial copy from offset: "Slackeriffic!" , "ack"

[edit] Raven

Copy a string by reference:

'abc' as a
a as b

Copy a string by value:

'abc' as a
a copy as b

[edit] REXX

src = "this is a string"
dst = src
 


[edit] Ruby

original = "hello"
new_reference = original
copy = original.clone # or original.dup


[edit] Sather

class MAIN is
main is
s  ::= "a string";
s1 ::= s;
-- s1 is a copy
end;
end;

[edit] Scheme

(define dst (string-copy src))

[edit] Seed7

var string: dest is "";
 
dest := "Hello";

[edit] Slate

[ | :s | s == s copy] applyTo: {'hello'}. "returns False"


[edit] Smalltalk

|s1 s2|
"bind the var s1 to the object string on the right"
s1 := 'i am a string'.
"bind the var s2 to the same object..."
s2 := s1.
"bind s2 to a copy of the object bound to s1"
s2 := (s1 copy).

[edit] SNOBOL4

 
* copy a to b
b = a = "test"
output = a
output = b
* change the copy
b "t" = "T"
output = b
end

Output

 test
 test
 Test

[edit] Standard ML

In Standard ML, strings are immutable, so you don't copy it.

Instead, maybe you want to create a CharArray.array (mutable string) from an existing string:

val src = "Hello";
val srcCopy = CharArray.array (size src, #"x"); (* 'x' is just dummy character *)
CharArray.copyVec {src = src, dst = srcCopy, di = 0};
src = CharArray.vector srcCopy; (* evaluates to true *)

or from another CharArray.array:

val srcCopy2 = CharArray.array (CharArray.length srcCopy, #"x"); (* 'x' is just dummy character *)
CharArray.copy {src = srcCopy, dst = srcCopy2, di = 0};

[edit] Tcl

set src "Rosetta Code"
set dst $src

Tcl copies strings internally when needed. To be exact, it uses a basic value model based on simple objects that are immutable when shared (i.e., when they have more than one effective reference to them); when unshared, they can be changed because the only holder of a reference has to be the code requesting the change. At the script level, this looks like Tcl is making a copy when the variable is assigned as above, but is more efficient in the common case where a value is not actually modified.

[edit] TI-83 BASIC

 
:"Rosetta Code"→Str1
:Str1→Str2
 

[edit] TI-89 BASIC

 
:"Rosetta Code"→str1
:str1→str2
 

[edit] Toka

" hello" is-data a
a string.clone is-data b

[edit] Trith

Strings are immutable character sequences, so copying a string just means duplicating the reference at the top of the stack:

"Hello" dup

[edit] V

dup really makes a reference, but the language is functional so the string is immutable.

"hello" dup

[edit] Visual Basic .NET

Platform: .NET

Works with: Visual Basic .NET version 9.0+

'Immutable Strings
Dim a = "Test string"
Dim b = a 'reference to same string
Dim c = New String(a.ToCharArray) 'new string, normally not used
 
'Mutable Strings
Dim x As New Text.StringBuilder("Test string")
Dim y = x 'reference
Dim z = New Text.StringBuilder(x.ToString) 'new string
Personal tools
Support