Discuss / Python / q

    if not isinstance(s, str):

        raise Exception("非字符串,参数错误")

    #如果是空字符串,直接返回s

    if len(s)==0:

        return s

    length = len(s)

    start=0

    end = -1

    if s.startswith(' '):

        start = start + 1

        while True:

            #index越界,字符串内全是空格,直接返回空字符串

            if start==length:

                return '';

            #遇到空格

            elif s[start] == ' ':

                start = start + 1    

            #非空格字符,跳出循环

            else:

                s = s[start:]

                break;

    if s.endswith(' '):

        end = end-1

        while True:

            #遇到空格

            if s[end] == ' ':

                end = end - 1

            #非空格字符,跳出循环

            else:

                s = s[:end+1]

                break;

    print("s=" + s)

    return s


  • 1

Reply