利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法:

首先判断字符串的长度是否为0,如果是,直接返回字符串

第二,循环判断字符串的首部是否有空格,如果有,去掉空格,再判断字符串的长度是否为0,如果是,直接返回字符串

第三,循环判断字符串的尾部是否有空格,如果有,去掉空格,再判断字符串的长度是否为0,如果是,直接返回字符串

最后,返回字符串

 1 # -*- coding: utf-8 -*-
 2 def trim(s):
 3     if 0==len(s):
 4         return s
 5
 6     while ‘ ‘==s[0]:
 7         s=s[1:]
 8         if 0==len(s):
 9             return s
10
11     while ‘ ‘==s[-1]:
12         s=s[:-1]
13         if 0==len(s):
14             return s
15
16     return s

测试代码:

 1 # 测试:
 2 from trim import trim
 3 if trim(‘hello  ‘) != ‘hello‘:
 4     print(‘测试失败!‘)
 5 elif trim(‘  hello‘) != ‘hello‘:
 6     print(‘测试失败!‘)
 7 elif trim(‘  hello  ‘) != ‘hello‘:
 8     print(‘测试失败!‘)
 9 elif trim(‘  hello  world  ‘) != ‘hello  world‘:
10     print(‘测试失败!‘)
11 elif trim(‘‘) != ‘‘:
12     print(‘测试失败!‘)
13 elif trim(‘    ‘) != ‘‘:
14     print(‘测试失败!‘)
15 else:
16     print(‘测试成功!‘)

原文地址:https://www.cnblogs.com/denggelin/p/8953629.html

时间: 2024-07-29 13:03:56

利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法:的相关文章

python练习题:利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法

方法一: # -*- coding: utf-8 -*- # 利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法: def trim(s): while s[:1] == ' ': s = s[1:] while s[-1:] == ' ': s = s[0:-1] return s # 测试: if trim('hello ') != 'hello': print('测试失败!') elif trim(' hello') != 'hello':

【Python实践-4】切片操作去除字符串首尾的空格

1 #利用切片操作,实现一个trim()函数,去除字符串首尾的空格,注意不要调用str的strip()方法 2 def trim(s): 3 while s[0:1]==' ': 4 s=s[1:] 5 while s[(len(s)-1):len(s)]==' ': 6 s=s[:-1] 7 return s 8 9 s=input('请输入一个字符串:') 10 print('去除首尾空格后',trim(s)) 知识点: 取一个list或tuple的部分元素,比如取list的前3个元素,对这

去除字符串首尾的空格

使用正则去除字符串首尾的空格. 分享三个去除字符串首尾空格的方法. 第一种,只调用一次replace方法 function trim(str){ return (str || "").replace(/^\s+|\s+$/g,""); } assert(trim(" #id div.class ") == "#id div.class", "Extra whitespace trimmed from a select

strip()函数---去除字符串首尾字符

#去除空 >>> s = ' 0000a0bc0000' >>> s.strip() '0000a0bc0000' #去除首尾字符'0' >>> s = '0000a0bc0000' >>> s.strip('0') 'a0bc' 不传入参数默认去除首尾空格 strip()函数只能去除首尾字符,中间的字符并不能去除 原文地址:https://www.cnblogs.com/thebear/p/9260676.html

去除字符串首尾空格和特殊字符

去除字符串首尾空格和特殊字符 用户在输入数据时,经常会在无意中输入多余的空格,在有些情况下,字符串中不允许出现空格和特殊字符,此时就需要去除字符串中的空格和特殊字符.在PHP中提供了trim()函数去除字符串左右两边的空格和特殊字符.ltrim()函数去除字符串左边的空格和特殊字符.rtrim()函数去除字符串中右边的空格和特殊字符. 1.trim()函数 trim()函数用于去除字符串开始位置以及结束位置的空格,并返回去掉空格后的字符串. 语法格式如下: string trim(string

shell实现trim函数-去除字符串两侧的空格(包括tab,space键)

shell实现trim函数效果去除字符串两侧的空格,以下三个命令等价,都能实现 sed 's/^\s*//' totrim.txt |sed 's/\s*$//'>trimed.txtsed 's/^\s*//;s/\s*$//' totrim.txt>trimed.txtsed -e 's/^\s*//' -e 's/\s*$//' totrim.txt>trimed.txt 主要就是利用正则表达式,^\s*表示字符串前面的零个或多个空格,\s*$表示字符串后面的零个或多个空格.

[py]str list切片-去除字符串首尾空格-递归思想

取出arr的前几项 #方法1 print([arr[0], arr[1]]) #方法2 arr2 = [] for i in range(2): arr2.append(arr[i]) print(arr2) #方法3 -切片 print(arr[:2]) 切片操作 参考 可以操作序列,如str list 取出最后一项 取出第一项 取出前10项 s[:10] 取出后10项 s[-10:] 删除首项 s[1:] 删除最后一项 s[:-1] ## 第一波 arr = [0, 1, 2, 3, 4]

每天一个JavaScript实例-去除字符串末尾的空白

<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>每天一个JavaScript实例-去除字符串末尾的空白</title> <script> function white(){ var input = document.ge

JavaSE8基础 String trim 去除字符串两端的空格

os :windows7 x64    jdk:jdk-8u131-windows-x64    ide:Eclipse Oxygen Release (4.7.0)        code: package jizuiku.t02; public class Demo3 { public static void main(String[] args) { String s1 = " h ell wor l d "; //去除字符串两头的空格 System.out.println(s1