D语言新手问题 D语言中去除开始或者结束字符串,类似与PHP中rtrim ,ltrim

· Created · Last modified by yunfeng~ replied at · 1173 times read

参考:https://dlang.org/library/std/string.html

去掉结束字符串 https://dlang.org/library/std/string/chomp.html


string sr1 = chomp("hellow "); // 默认去除 \r\n  , \f  ,\v   \n,\r 等
string sr2 = chomp("hellow "," ");
string sr3 = chomp("hellow;",";");
writeln(sr1~"end"); // hellow end
writeln(sr2~"end"); // hellowend
writeln(sr3~"end"); // hellowend

去掉开始字符串 https://dlang.org/library/std/string/chomp_prefix.html]

writeln(chompPrefix("hello world", "he")); // "llo world"
writeln(chompPrefix("hello world", "hello w")); // "orld"
writeln(chompPrefix("hello world", " world")); // "hello world"
writeln(chompPrefix("", "hello")); // ""

去掉最后一个字符 https://dlang.org/library/std/string/chop.html

writeln(chop("hello world")); // "hello worl"
writeln(chop("hello world\n")); // "hello world"
writeln(chop("hello world\r")); // "hello world"
writeln(chop("hello world\n\r")); // "hello world\n"
writeln(chop("hello world\r\n")); // "hello world"
writeln(chop("Walter Bright")); // "Walter Brigh"
writeln(chop("")); // ""
writeln(chop("")); // ""
writeln(chop("测试下")); // 测试

Login to reply