我想将输入字符串的长度减少到最多20个字符,但我不想在单词中间打断字符串。
// show me 20 char: 12345678901234567890
string inputString = "This is an example user input which has to be shorten at a white space";
if (inputString.length > 20)
{
shortenString = inputString.SubString(0, 21); // <-- "This is an example us"
// I need a regex to match everything until the last white space
// final output: "This is an example"
}
(.{0,20})(\s|$)
此正则表达式将捕获最多20个字符的组,以空格(或字符串结尾)结尾
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句