简体   繁体   English

dlang如何将char []转换并修剪为字符串?

[英]dlang how to convert and trim a char[] into a string?

I am trying to convert a buffer to string but apparently there is this space that I cannot remove 我试图将缓冲区转换为字符串,但显然有一个我无法删除的空间
How should I strip this space and convert a char[] into an string container 我应该如何剥离此空间并将char []转换为字符串容器

string getClass(HWND hwnd=NULL){
    char[100] str;
    GetClassNameA(hwnd, str.ptr, str.length);
    writeln(str.dup.strip,'"'); //nothing is stripped, str is printed as 100 characters
    writeln(str[99]=='\0'); //false
    writeln(str[99]==' '); //false
    writeln(str.dup[99]=='\0'); //false
    writeln(str.dup[99]==' '); //false
    writeln(str.dup.strip[99]=='\0'); //false
    writeln(str.dup.strip[99]==' '); //false
    return to!string(str).strip; //same nothing is stripped
}

You need to slice the buffer with the correct length. 您需要使用正确的长度切片缓冲区。 GetClassName returns the length of the string so do something like GetClassName返回字符串的长度,所以做类似的事情

char[100] buffer;
auto recv = GetClassNameA(hwnd, buffer.ptr, buffer.length);
if(recv == 0) throw new Exception("failed");
char[] str = buffer[0 .. recv];
// now you can work with str

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM