import std.algorithm;
import std.array;
import std.ascii;
import std.string;

int wordCount(string str) {
    auto words = str
        .filter!(c => !isPunct(c) && !isSpace(c))
        .split(isSpace);

    return words.length;
}

void main() {
    string str = "Hello, world!";
    int count = wordCount(str);
    writeln(count); // 2
}
dlang语言实现统计一个字符串含有的单词数目有点类似php的以下常用代码use IlluminateSupportStr;StrwordCountHello world!; 2注意不止包含空格要去除一些特殊字符标点符号也要去除然后统计单词数目给出完整可以运行的d语言实现代码

原文地址: https://www.cveoy.top/t/topic/fmXT 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录