我正在尝试将缓冲区转换为字符串,但显然有这个空间我无法删除
我应该如何剥离此空格并将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
}
答案 0 :(得分:3)
您需要使用正确的长度切片缓冲区。 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