Copy a string

Revision as of 00:45, 13 September 2007 by rosettacode>Nirs (Objective C, various string copying options)

This task is about copying a string.

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

Ada

Ada provides three diffferent 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.

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

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.

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;

BASIC

Interpeter: QuickBasic 4.5, PB 7.1

 src$ = "Hello"
 dst$ = src$

C

 // Using strdup
 const char* src = "Hello";
 char* dst = strdup(src);
 // Using malloc/strcpy
 const char* src = "Hello";
 int len = strlen(src);
 char* dst = (char*)malloc(len+1);
 strcpy(dst, src);
 // Using malloc/strncpy
 const char* src = "Hello";
 int len = strlen(src);
 char* dst = (char*)malloc(len+1);
 strncpy(dst, src, len+1); dst[len] = 0;
 // Using static buffer
 const char* src= "Hello";
 static char dst[80];
 strncpy(dst, src, 80);
 dst[79] = 0;

Objective-C

Immutable strings - since they are immutable, you may get the same instance with its references count increased.

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

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

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

Copying a CString:

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.

C++

Libraries: C++ Standard Library

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

Libraries: Qt

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

Libraries: Microsoft Foundation Classes

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

C#

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

D

char[] src = "string";
char[] dest = src.dup;

Erlang

Src = "Hello".
Dst = Src.

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

Fortran

Java

 String src = "Hello";
 String dst = new String(s);

JavaScript

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

Perl

Interpeter: Perl

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

PHP

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

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);

Python

Interpeter: Python 2.3, 2.4, 2.5

 src = "Hello"
 dst = src

Ruby

 src="Hello"
 dst=src.dup

Seed7

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

Tcl

set src "Rosetta Code"
set dst $src

Toka

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