啊,好难啊,展现一下我的蠢代码
Topic source第一个回复错的,第二个好一点,但是还是遇到如果最后一个字符不是空的情况有问题,我再修改修改。。。。好烦啊。
def trim(s):
j=len(s)
i=0
n=s[0:1]
while n==' ':
n=s[i+1:i+2]
i=i+1
m=s[j-2:j-1]
while m==' ':
m=s[j-3:j-2]
j=j-1
s=s[i:j-1]
return s
print(trim(' 测 试 文 本 '))
第三个版!这个肯定对!。。。。我测了。。。。。
def trim(s):
j=len(s)
i=0
n=s[0:1]
while n==' ':
n=s[i+1:i+2]
i=i+1
m=s[j-1:j] #
#if m!=' ':
while m==' ':
m=s[j-2:j-1]
j=j-1
s=s[i:j]
return s
print(trim(' j fsfs '))
def trim(s):
j=len(s)
i=0
n=s[0:1]
while n==' ':
n=s[i+1:i+2]
i=i+1
m=s[j-1:j]
while m==' ':
m=s[j-2:j-1]
j=j-1
s=s[i:j]
return s
trim(' j fsfs ')
- 1
爷左_Cola
def trim(s):
s=input('请输入你的字符串')
j=len(s)
i=0
n=s[0:1]
while n==' ':
n=s[i+1:i+2]
i=i+1
m=s[j-2:j-1]
while m==' ':
m=s[j-3:j-2]
j=j-1
return s