apitest_unittest/common/bif_functions/bif_str.py

61 lines
1.4 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# coding: utf-8
# -------------------------------------------------------------------------------
# Name: bif_str.py
# Description:
# Author: kira
# EMAIL: 262667641@qq.com
# Date: 2021/1/12 15:12
# -------------------------------------------------------------------------------
from common.bif_functions import logger
__all__ = ['substr', 'str_join']
def substr(raw_str, start=None, end=None):
"""
截取字符串
Args:
raw_str: 原始字符串
start: 字符串开始位置
end: 字符串结束位置
Returns: 截取的字符串
"""
try:
start = int(start) if (isinstance(start, str) and start.isdigit()) else start
end = int(end) if (isinstance(end, str) and end.isdigit()) else end
return raw_str[start:end]
except TypeError as e:
return ''
@logger.catch
def str_join(obj, connector=","):
"""
连接任意数量的字符
Args:
obj: 被连接对象类型list、tuple
connector: 连接符
Returns:
"""
if not isinstance(connector, str):
connector = str(connector)
if isinstance(obj, str):
return obj
elif isinstance(obj, (list, tuple)):
temp_obj = []
for item in obj:
if not isinstance(item, str):
item = str(item)
temp_obj.append(item)
return connector.join(temp_obj)
if __name__ == '__main__':
substr()
str_join()