1.(字符串)-获取最后一个字符串及长度

如:' hello word is good  ',返回:good,4

python:

class Solution:
    def strc(self, s):
        end = len(s)
        #取到最后一个字母所在位置的后一位
        while end > 0 and s[end - 1] == ' ':
            end -= 1
        
        start = end
        #取到最后一个单词的首字母位置
        while start > 0 and s[start - 1] != ' ':
             start -= 1
        L=end - start
    
        return L,s[start:end]

s = Solution()
print(s.strc(' hello word is good '))
#(4, 'good')
原文地址:https://www.cnblogs.com/onenoteone/p/12441783.html