pydu/docs/string.rst

86 lines
1.9 KiB
ReStructuredText
Raw Normal View History

2017-10-31 15:54:39 +00:00
String
------
2017-11-12 18:25:30 +00:00
.. py:function:: pydu.string.safeunicode(obj, encoding='utf-8')
2017-10-31 15:54:39 +00:00
2017-12-13 14:01:14 +00:00
Converts any given object to unicode string.
2017-10-31 15:54:39 +00:00
2017-12-14 12:38:22 +00:00
>>> from pydu.string import safeunicode
>>> safeunicode('hello')
u'hello'
>>> safeunicode(2)
u'2'
>>> safeunicode('\xe4\xb8\xad\xe6\x96\x87')
u'中文'
2017-10-31 15:54:39 +00:00
.. py:function:: pydu.string.safeencode(obj, encoding='utf-8')
2017-12-13 14:01:14 +00:00
Converts any given object to encoded string (default: utf-8).
2017-12-14 12:38:22 +00:00
>>> from pydu.string import safeencode
>>> safeencode('hello')
'hello'
>>> safeencode(2)
'2'
>>> safeencode(u'中文')
'\xe4\xb8\xad\xe6\x96\x87'
2017-11-12 18:25:30 +00:00
.. py:function:: pydu.string.lstrips(text, remove)
2017-10-31 15:54:39 +00:00
2017-12-13 14:01:14 +00:00
Removes the string ``remove`` from the left of ``text``.
2017-10-31 15:54:39 +00:00
2017-12-14 12:38:22 +00:00
>>> from pydu.string import lstrips
>>> lstrips('foobar', 'foo')
'bar'
>>> lstrips('FOOBARBAZ', ['FOO', 'BAR'])
'BAZ'
>>> lstrips('FOOBARBAZ', ['BAR', 'FOO'])
'BARBAZ'
2017-10-31 15:54:39 +00:00
2017-11-12 18:25:30 +00:00
.. py:function:: pydu.string.rstrips(text, remove)
2017-10-31 15:54:39 +00:00
2017-12-13 14:01:14 +00:00
Removes the string ``remove`` from the right of ``text``.
2017-10-31 15:54:39 +00:00
2017-12-14 12:38:22 +00:00
>>> from pydu.string import rstrips
>>> rstrips('foobar', 'bar')
'foo'
2017-10-31 15:54:39 +00:00
2017-11-12 18:25:30 +00:00
.. py:function:: pydu.string.strips(text, remove)
2017-10-31 15:54:39 +00:00
2017-12-13 14:01:14 +00:00
Removes the string ``remove`` from the both sides of ``text``.
2017-10-31 15:54:39 +00:00
2017-12-14 12:38:22 +00:00
>>> from pydu.string import strips
>>> strips('foobarfoo', 'foo')
'bar'
2017-12-11 14:26:02 +00:00
.. py:function:: pydu.string.common_prefix(l)
2017-12-13 14:01:14 +00:00
Return common prefix of the stings
2017-12-11 14:26:02 +00:00
2017-12-14 12:38:22 +00:00
>>> from pydu.string import common_prefix
>>> common_prefix(['abcd', 'abc1'])
'abc'
2017-12-11 14:26:02 +00:00
.. py:function:: pydu.string.common_suffix(l)
2017-12-13 14:01:14 +00:00
Return common suffix of the stings
2017-12-11 14:26:02 +00:00
2017-12-14 12:38:22 +00:00
>>> from pydu.string import common_suffix
>>> common_suffix(['dabc', '1abc'])
'abc'
2017-12-14 12:37:27 +00:00
.. py:function:: pydu.string.sort(s, reversed=False)
Sort given string by ascending order.
2017-12-14 12:38:22 +00:00
If ``reverse`` is `True`, sorting given string by descending order.
2017-12-14 12:37:27 +00:00
>>> from pydu.string import sort
>>> sort('dabc')
'abcd'