Если вы используете угловые, тогда посмотрите эту директиву ng-file-upload
. Это довольно круто.
Just one space, or all consecutive spaces? If the second, then strings already have a .strip()
method:
>>> ' Hello '.strip()
'Hello'
>>> ' Hello'.strip()
'Hello'
>>> 'Bob has a cat'.strip()
'Bob has a cat'
>>> ' Hello '.strip() # ALL consecutive spaces at both ends removed
'Hello'
If you need only to remove one space however, you could do it with:
def strip_one_space(s):
if s.endswith(" "): s = s[:-1]
if s.startswith(" "): s = s[1:]
return s
>>> strip_one_space(" Hello ")
' Hello'
Also, note that str.strip()
removes other whitespace characters as well (e.g. tabs and newlines). To remove only spaces, you can specify the character to remove as an argument to strip
, i.e.:
>>> " Hello\n".strip(" ")
'Hello\n'
Это удалит все начальные и конечные пробелы в myString
:
myString.strip()
Вы хотите strip ():
myphrases = [ " Hello ", " Hello", "Hello ", "Bob has a cat" ]
for phrase in myphrases:
print phrase.strip()
Если Вы хотите к конкретное количество для обрезки пробелов от левого и правого , Вы могли бы сделать это:
def remove_outer_spaces(text, num_of_leading, num_of_trailing):
text = list(text)
for i in range(num_of_leading):
if text[i] == " ":
text[i] = ""
else:
break
for i in range(1, num_of_trailing+1):
if text[-i] == " ":
text[-i] = ""
else:
break
return ''.join(text)
txt1 = " MY name is "
print(remove_outer_spaces(txt1, 1, 1)) # result is: " MY name is "
print(remove_outer_spaces(txt1, 2, 3)) # result is: " MY name is "
print(remove_outer_spaces(txt1, 6, 8)) # result is: "MY name is"