Copy a string: Difference between revisions
Content deleted Content added
→[[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.
==
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;
==
'''Interpeter:''' [[QuickBasic]] 4.5, PB 7.1
Line 56 ⟶ 54:
dst[79] = 0;
==
Immutable strings - since they are immutable, you may get the same instance with its references count increased.
Line 98 ⟶ 95:
CString dst = src;
==
[[Category:C sharp]]
string src = "Hello";
string dst = src;
==
char[] src = "string";
char[] dest = src.dup;
==
Src = "Hello".
Dst = Src.
==
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
==
var src = "Hello";
var dst = src;
==
(setf orig "Hello")
(setf copied (copy-seq orig))
Line 154 ⟶ 146:
(eq orig copied)
nil
==
'''Interpeter:''' [[Perl]]
Line 161 ⟶ 152:
my $dst = $src;
==
$src = "Hello";
$dst = $src;
==
In Pop11 normal data are represented by references, so plain assignment
Line 182 ⟶ 171:
vars dst=copy(src);
==
'''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 '=='.
==
Copy a string by reference:
Line 227 ⟶ 214:
a copy as b
==
src="Hello"
dst=src.dup
==
var string: dest is "";
Line 240 ⟶ 225:
dest := "Hello";
==
set src "Rosetta Code"
set dst $src
==
" hello" is-data a
a string.clone is-data b
==
'''Platform:''' [[.NET]]
|