为了计算字符串
s
开头和结尾的空格数量,我这样做:s.index(/[^ ]/) # Number of spaces at the beginning of s
s.reverse.index(/[^ ]/) # Number of spaces at the end of s
这种方法需要单独处理只包含空格的 s
的边缘情况。
有更好(更简洁/高效)的方法吗?
*
而不是+
。 - steenslag