Mis*_*hko 8 ruby string space count
要计算字符串开头和结尾的空格数,s我会这样做:
s.index(/[^ ]/) # Number of spaces at the beginning of s
s.reverse.index(/[^ ]/) # Number of spaces at the end of s
Run Code Online (Sandbox Code Playgroud)
当s包含空格时,这种方法需要边缘情况才能单独处理.
是否有更好(更优雅/更有效)的方法呢?
| 归档时间: |
|
| 查看次数: |
4548 次 |
| 最近记录: |