需求:
需要過濾掉輸入字串的前導,後續空格或其它字元.這在處理使用者輸入的時候比較有用.
討論:和其它語言類似,python也提供了lstrip,rstrip和strip方法,類似delphi和c#的trim方法.
用法:
>>> x = ' test '
>>> print '|', x.lstrip( ), '|', x.rstrip( ), '|', x.strip( ), '|'
| test | test | test |
另外,這三個方法還可以接受乙個引數,用於過濾指定的字元組成的串 ,如:
>>> x = 'xyxxyy testyx yyx'
>>> print '|'+x.strip('xy')+'|'
| testyx |
需要注意的是:testyx前面有乙個空格,因為空格前的所有的x,y都被過濾掉了,而test後面的yx沒有被過濾掉,是因為執行到testyx後面那個空格的時候就完成了.如果我們只需要留下'test',輸入下面的語句即可:
>>> print x.strip('xy ')
test
js去掉字串兩邊的空格
正規表示式的方式 去左空格 function ltrim s 去右空格 function rtrim s 去左右空格 function trim s 去除前面空格 while mystr.lastindexof mystr.length 1 mystr.length 1 去除後面空格 if myst...
去掉字串兩邊空格,全形轉半形
trimtodbcmodelbinder.cs 1 一定要使用using system.web.mvc下的defaultmodelbinder 2public class trimtodbcmodelbinder defaultmodelbinder314 else 1518 19 20 全形轉半形...
iOS和Android去除字串兩邊的空格
今天測試組測bug,說是使用者名稱密碼都對,就是登入不上去,後來才發現是因為寫了個空格,最後還是提了個bug,說是前後有空格也得讓進。下面分別是ios和安卓去除字串左右兩邊空格的辦法。ios nsstring str d da fa nsstring res str stringbytrimming...