Jump to content

Copy a string: Difference between revisions

m
Changed over to headers.
(→‎[[Java]]: Use Java header instead)
m (Changed over to headers.)
Line 2:
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.
 
==[[{{header|Ada]]}}==
[[Category: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.
 
Line 26 ⟶ 25:
Dest : Unbounded_String := Src;
 
==[[{{header|BASIC]]}}==
[[Category:BASIC]]
'''Interpeter:''' [[QuickBasic]] 4.5, PB 7.1
 
Line 56 ⟶ 54:
dst[79] = 0;
 
==[[{{header|Objective-C]]}}==
[[Category:Objective-C]]
 
Immutable strings - since they are immutable, you may get the same instance with its references count increased.
Line 98 ⟶ 95:
CString dst = src;
 
==[[{{header|C sharp|C #]]}}==
[[Category:C sharp]]
string src = "Hello";
string dst = src;
 
==[[{{header|D]]}}==
[[Category:D]]
 
char[] src = "string";
char[] dest = src.dup;
 
==[[{{header|Erlang]]}}==
[[Category:Erlang]]
 
Src = "Hello".
Dst = Src.
 
==[[{{header|Forth]]}}==
[[Category: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:
Line 139 ⟶ 133:
//"strCopy.equals(src)" is true
 
==[[{{header|JavaScript]]}}==
[[Category:JavaScript]]
var src = "Hello";
var dst = src;
 
==[[{{header|Lisp]]}}==
[[Category:Lisp]]
(setf orig "Hello")
(setf copied (copy-seq orig))
Line 154 ⟶ 146:
(eq orig copied)
nil
==[[{{header|Perl]]}}==
[[Category:Perl]]
'''Interpeter:''' [[Perl]]
 
Line 161 ⟶ 152:
my $dst = $src;
 
==[[{{header|PHP]]}}==
[[Category:PHP]]
 
$src = "Hello";
$dst = $src;
 
==[[{{header|Pop11]]}}==
[[Category:Pop11]]
 
In Pop11 normal data are represented by references, so plain assignment
Line 182 ⟶ 171:
vars dst=copy(src);
==[[{{header|Python]]}}==
[[Category:Python]]
'''Interpeter:''' Python 2.3, 2.4, 2.5
 
Line 214 ⟶ 202:
Be careful with 'is' - use it only when you want to compare the identity of the object. To compare string values, use '=='.
 
==[[{{header|Raven]]}}==
[[Category:Raven]]
 
Copy a string by reference:
Line 227 ⟶ 214:
a copy as b
 
==[[{{header|Ruby]]}}==
[[Category:Ruby]]
 
src="Hello"
dst=src.dup
 
==[[{{header|Seed7]]}}==
[[Category:Seed7]]
 
var string: dest is "";
Line 240 ⟶ 225:
dest := "Hello";
 
==[[{{header|Tcl]]}}==
[[Category:Tcl]]
 
set src "Rosetta Code"
set dst $src
 
==[[{{header|Toka]]}}==
[[Category:Toka]]
" hello" is-data a
a string.clone is-data b
 
==[[{{header|Visual Basic .NET]]}}==
[[Category:Visual Basic .NET]]
 
'''Platform:''' [[.NET]]
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.