www.digitalmars.com         C & C++   DMDScript  

D - string split method silly

reply Paul Stanton <Paul_member pathlink.com> writes:
is it just me or does this look a bit funny...

{
char[][] result = split("garry,barry,harry", ",");

for(int i = 0; i < result.length; i++)
printf("%s\n", (char*)result[i]);
}

prints:
garry,barry,harry
barry,harry
harry

personally, i was expecting
garry
barry
harry
Jan 23 2003
parent "Walter" <walter digitalmars.com> writes:
"Paul Stanton" <Paul_member pathlink.com> wrote in message
news:b0por5$5dc$1 digitaldaemon.com...
 is it just me or does this look a bit funny...

 {
 char[][] result = split("garry,barry,harry", ",");

 for(int i = 0; i < result.length; i++)
 printf("%s\n", (char*)result[i]);
 }

 prints:
 garry,barry,harry
 barry,harry
 harry

 personally, i was expecting
 garry
 barry
 harry

This is happening because %s is expecting a null terminated string. char[] in D are not null terminated. Try using %.*s instead, and you should get the answer you expect.
Mar 06 2003