Copy a string: Difference between revisions
Content deleted Content added
added common lisp |
No edit summary |
||
Line 6: | Line 6: | ||
===Fixed Length String Copying.=== |
===Fixed Length String Copying.=== |
||
<ada> |
<lang ada> |
||
Src : String := "Hello"; |
Src : String := "Hello"; |
||
Dest : String := Src; |
Dest : String := Src; |
||
</ |
</lang> |
||
Ada provides the ability to manipulate slices of strings. |
Ada provides the ability to manipulate slices of strings. |
||
<ada> |
<lang ada> |
||
Src : String "Rosetta Stone"; |
Src : String "Rosetta Stone"; |
||
Dest : String := Src(1..7); -- Assigns "Rosetta" to Dest |
Dest : String := Src(1..7); -- Assigns "Rosetta" to Dest |
||
Dest2 : String := Src(9..13); -- Assigns "Stone" to Dest2 |
Dest2 : String := Src(9..13); -- Assigns "Stone" to Dest2 |
||
</ |
</lang> |
||
===Bounded Length String Copying=== |
===Bounded Length String Copying=== |
||
<ada> |
<lang ada> |
||
-- Instantiate the generic package Ada.Strings.Bounded.Generic_Bounded_Length with a maximum length of 80 characters |
-- 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); |
package Flexible_String is new Ada.Strings.Bounded.Generic_Bounded_Length(80); |
||
Line 24: | Line 24: | ||
Src : Bounded_String := To_Bounded_String("Hello"); |
Src : Bounded_String := To_Bounded_String("Hello"); |
||
Dest : Bounded_String := Src; |
Dest : Bounded_String := Src; |
||
</ |
</lang> |
||
Ada Bounded_String type provides a number of functions for dealing with slices. |
Ada Bounded_String type provides a number of functions for dealing with slices. |
||
=== Unbounded Length String Copying=== |
=== Unbounded Length String Copying=== |
||
<ada> |
<lang ada> |
||
-- The package Ada.Strings.Unbounded contains the definition of the Unbounded_String type and all its methods |
-- The package Ada.Strings.Unbounded contains the definition of the Unbounded_String type and all its methods |
||
Src : Unbounded_String := To_Unbounded_String("Hello"); |
Src : Unbounded_String := To_Unbounded_String("Hello"); |
||
Dest : Unbounded_String := Src; |
Dest : Unbounded_String := Src; |
||
</ |
</lang> |
||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Line 191: | Line 191: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
<lisp>(setq dst (copy-seq src))</ |
<lang lisp>(setq dst (copy-seq src))</lang> |
||
=={{header|D}}== |
=={{header|D}}== |
||
Line 295: | Line 295: | ||
To copy a string, just use ordinary assignment: |
To copy a string, just use ordinary assignment: |
||
<perl>my $original = 'Hello.'; |
<lang perl>my $original = 'Hello.'; |
||
my $new = $original; |
my $new = $original; |
||
$new = 'Goodbye.'; |
$new = 'Goodbye.'; |
||
print $original, "\n"; # prints "Hello."</ |
print $original, "\n"; # prints "Hello."</lang> |
||
To create a reference to an existing string, so that modifying the referent changes the original string, use a backslash: |
To create a reference to an existing string, so that modifying the referent changes the original string, use a backslash: |
||
<perl>my $ref = \$original; |
<lang perl>my $ref = \$original; |
||
$$ref = 'Goodbye.'; |
$$ref = 'Goodbye.'; |
||
print $original, "\n"; # prints "Goodbye."</ |
print $original, "\n"; # prints "Goodbye."</lang> |
||
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: |
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: |
||
<perl>our $alias; |
<lang perl>our $alias; |
||
local *alias = \$original; |
local *alias = \$original; |
||
$alias = 'Good evening.'; |
$alias = 'Good evening.'; |
||
print $original, "\n"; # prints "Good evening."</ |
print $original, "\n"; # prints "Good evening."</lang> |
||
Note that < |
Note that <tt>our $alias</tt>, though in most cases a no-op, is necessary under stricture. Beware that <tt>local</tt> binds dynamically, so any subroutines called in this scope will see (and possibly modify!) the value of <tt>$alias</tt> assigned here. |
||
=={{header|PHP}}== |
=={{header|PHP}}== |
||
Line 342: | Line 342: | ||
Since strings are immutable, all copy operations return the same string. Probably the reference is increased. |
Since strings are immutable, all copy operations return the same string. Probably the reference is increased. |
||
<python> |
<lang python> |
||
>>> src = "hello" |
>>> src = "hello" |
||
>>> a = src |
>>> a = src |
||
Line 351: | Line 351: | ||
>>> src is a is b is c is d |
>>> src is a is b is c is d |
||
True |
True |
||
</ |
</lang> |
||
To actually copy a string: |
To actually copy a string: |
||
<python> |
<lang python> |
||
>>> a = 'hello' |
>>> a = 'hello' |
||
>>> b = ''.join(a) |
>>> b = ''.join(a) |
||
Line 362: | Line 362: | ||
>>> b is a ### Might be True ... depends on "interning" implementation details! |
>>> b is a ### Might be True ... depends on "interning" implementation details! |
||
False |
False |
||
</ |
</lang> |
||
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. |
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. |
||
Line 386: | Line 386: | ||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
<scheme>(define dst (string-copy src))</ |
<lang scheme>(define dst (string-copy src))</lang> |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |