Remove space characters from char [] array in D

What is the recommended way to remove a space from char [] in D. for example using dmd 2.057 I have

import std.stdio; import std.string; import std.algorithm; char[] line; int main(){ line = r"this is a line with spaces "; line = removechars(line," "); writeln(line); return 0; } 

When compiling, this will result in this error:

 Error: cannot implicitly convert expression ("this is a line with spaces ") of type string to char[] Error: template std.string.removechars(S) if (isSomeString!(S)) does not match any function template declaration Error: template std.string.removechars(S) if (isSomeString!(S)) cannot deduce template function from argument types !()(char[],string) 

When I performed any search on Google, I found that a similar error was registered as an error and was sent in June 2011, but I'm not sure if this was a link to the same or to another problem.

In general, what approach is recommended to remove from the string and indicate the order of characters from the previous character array?

In this case, return

 assert(line == "thisisalinewithspaces") 

after removing whitespace

+4
source share
3 answers

removechars accepts all types of strings (char [], wchar [], dchar [], string, wstring and dstring), but the second argument must be of the same type as the first. Therefore, if you pass char [] as the first arg, the second arg should also be char []. You, however, pass the string: ""

A simple solution would be to duplicate the string on char []: "" .dup

removechars (string, "" .dup)

This also works:

removechars (string, ['\ x20'])

+5
source

Give removechars a immutable(char)[] (this is what string is an alias for). You will also need a .dup result to get a mutable char array.

 import std.stdio; import std.string; import std.algorithm; char[] line; void main() { auto str = r"this is a line with spaces "; line = removechars(str," ").dup; writeln(line); } 
+3
source

I try, but I can’t. Now I can.

 #include <iostream> #include <string> using namespace std; void main(){ char pswd[10]="XOXO ";//this actually after i fetch from oracle string pass=""; char passwd[10]=""; pass=pswd; int x = pass.size(), y=0; while(y<x) { if(pass[y]!=' ') {passwd[y]=pass[y];} y++; } strcpy(pswd,passwd); cout<<pswd; } 
+1
source

Source: https://habr.com/ru/post/1395087/


All Articles