www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to use strip or stripRight on char[len] ? Thanks.

reply FrankLike <1150015857 qq.com> writes:
Hi,everyone,
How to use strip or stripRight on char[len]?

For example:

string abcs ="aabc    ";
auto abcsaa = abcs.stripRight;
writeln(abcsaa);
writeln("---abcsaa--stripRight ok---- ");


  char[100] abc ="aabc    ";
  auto abcaa = ((abc).dup).stripRight;
  writeln(abcaa);
writeln("----stripRight error----- ");

Where is error? Thanks.
Feb 22 2018
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 22 February 2018 at 16:55:14 UTC, FrankLike wrote:
  char[100] abc ="aabc    ";
  auto abcaa = ((abc).dup).stripRight;
try: auto abcaa = stripRight(abc[]) just make sure you do NOT return that abcaa from the function or store it in an object. It still refers to the static array that does not outlive that function. It is simply that these functions require a slice so it can resize it and you can't resize a static array.
Feb 22 2018
next sibling parent reply FrankLike <1150015857 qq.com> writes:
On Thursday, 22 February 2018 at 16:59:40 UTC, Adam D. Ruppe 
wrote:
 On Thursday, 22 February 2018 at 16:55:14 UTC, FrankLike wrote:
 It is simply that these functions require a slice so it can 
 resize it and you can't resize a static array.
char[100] abc ="aabc "; string aa = to!string(abc); string aaa = aa.stripRight; It get the same result.
Feb 22 2018
parent FrankLike <1150015857 qq.com> writes:
On Thursday, 22 February 2018 at 17:08:14 UTC, FrankLike wrote:
 On Thursday, 22 February 2018 at 16:59:40 UTC, Adam D. Ruppe 
 wrote:
 On Thursday, 22 February 2018 at 16:55:14 UTC, FrankLike wrote:
 It is simply that these functions require a slice so it can 
 resize it and you can't resize a static array.
char[100] abc ="aabc "; char[] p = abc; string aa = to!string(p); string aaa = aa.stripRight; It get the same result too.
Feb 22 2018
prev sibling parent reply FrankLike <1150015857 qq.com> writes:
On Thursday, 22 February 2018 at 16:59:40 UTC, Adam D. Ruppe 
wrote:
 On Thursday, 22 February 2018 at 16:55:14 UTC, FrankLike wrote:
  char[100] abc ="aabc    ";
  auto abcaa = ((abc).dup).stripRight;
try: auto abcaa = stripRight(abc[])
Now,I want to get the result: char[100] Path; writeln("will get path "); SHGetSpecialFolderPath(NULL,cast(char*)Path.ptr,CSIDL_DESKTOP,0); if use char[] Path; It not get the result. How to strip the Path? Thanks.
Feb 22 2018
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 22 February 2018 at 17:44:53 UTC, FrankLike wrote:
 SHGetSpecialFolderPath(NULL,cast(char*)Path.ptr,CSIDL_DESKTOP,0);
You don't strip that at all, the function writes a zero-terminated string to the buffer. So either: use the pointer as-is to other C functions, or call the `fromStringz` function in Phobos to convert it and copy it if you want to store it. http://dpldocs.info/experimental-docs/std.string.fromStringz.html import std.string; string s = fromStringz(abc.ptr).idup; // the idup copies it out
Feb 22 2018
parent FrankLike <1150015857 qq.com> writes:
On Thursday, 22 February 2018 at 18:02:11 UTC, Adam D. Ruppe 
wrote:

 You don't strip that at all, the function writes a 
 zero-terminated string to the buffer.
Thank you very much ! I forgot it.
Feb 22 2018