Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
May 03, 2004 string init | ||||
---|---|---|---|---|
| ||||
Let's say I want to define a mutable string: char[] a_tmp = "abcd"; // immutable ref char[] a; a[] = a_tmp; // mutable copy of immutable data. OR..? char[] a = "abc" ~ "d"; Is there a simpler or more direct syntax? Kevin |
May 03, 2004 Re: string init | ||||
---|---|---|---|---|
| ||||
Posted in reply to kevinbealer | kevinbealer@yahoo.com wrote:
> Let's say I want to define a mutable string:
>
> char[] a_tmp = "abcd"; // immutable ref
> char[] a;
> a[] = a_tmp; // mutable copy of immutable data.
>
> OR..?
>
> char[] a = "abc" ~ "d";
>
> Is there a simpler or more direct syntax?
>
> Kevin
>
>
char[] a = ("abcd").dup; may work for you.
-- andy
|
May 04, 2004 Re: string init | ||||
---|---|---|---|---|
| ||||
Posted in reply to kevinbealer | kevinbealer@yahoo.com wrote:
> Let's say I want to define a mutable string:
>
> char[] a_tmp = "abcd"; // immutable ref
> char[] a;
> a[] = a_tmp; // mutable copy of immutable data.
>
> OR..?
>
> char[] a = "abc" ~ "d";
>
> Is there a simpler or more direct syntax?
>
> Kevin
The ".dup" property will duplicate an array:
char[] a = a_tmp.dup;
|
May 04, 2004 Re: string init | ||||
---|---|---|---|---|
| ||||
Posted in reply to Andy Friesen | In article <c76g4e$iau$1@digitaldaemon.com>, Andy Friesen says... > >kevinbealer@yahoo.com wrote: >> Let's say I want to define a mutable string: >> >> char[] a_tmp = "abcd"; // immutable ref >> char[] a; >> a[] = a_tmp; // mutable copy of immutable data. >> >> OR..? >> >> char[] a = "abc" ~ "d"; >> >> Is there a simpler or more direct syntax? >> >> Kevin >> >> > >char[] a = ("abcd").dup; may work for you. > > -- andy Yep, that looks like the most logical. Kevin |
May 06, 2004 Re: string init | ||||
---|---|---|---|---|
| ||||
Posted in reply to kevinbealer | On Mon, 3 May 2004 22:05:57 +0000 (UTC), kevinbealer@yahoo.com wrote:
>Let's say I want to define a mutable string:
>
>char[] a_tmp = "abcd"; // immutable ref
>char[] a;
>a[] = a_tmp; // mutable copy of immutable data.
>
>OR..?
>
>char[] a = "abc" ~ "d";
>
>Is there a simpler or more direct syntax?
>
>Kevin
>
try const .... as in
---------------------
import std.c.stdio;
void nctest() {
char[] a = "abcd";
char[]b = a;
printf( "char[] a = 'acbd'\n" );
printf( "char[] b = a;\n=>\n" );
printf( "a=%.*s\n", a );
printf( "b=%.*s\n", b );
printf( "b[2]='2'...\n" );
b[2] = '2';
printf( "a=%.*s\n", a );
printf( "b=%.*s\n", b );
}
void ctest() {
const char[] a = "abcd";
char[]b = a;
printf( "const char[] a = 'acbd'\n" );
printf( "char[] b = a;\n=>\n" );
printf( "b[2]='2'...\n" );
printf( "a=%.*s\n", a );
printf( "b=%.*s\n", b );
printf( "b[2]='2'...\n" );
b[2] = '2';
printf( "a=%.*s\n", a );
printf( "b=%.*s\n", b );
}
int main( char[][] args ) {
nctest();
ctest();
return 0;
}
------------------------
output (as one would hope is)
char[] a = 'acbd'
char[] b = a;
=>
a=abcd
b=abcd
b[2]='2'...
a=ab2d
b=ab2d
const char[] a = 'acbd'
char[] b = a;
=>
b[2]='2'...
a=abcd
b=abcd
b[2]='2'...
a=abcd
b=ab2d
|
Copyright © 1999-2021 by the D Language Foundation