def delBString(a,b): if not isinstance(a,str): raise TypeError("a is not str") if not isinstance(b,str): raise TypeError("b is not str") if len(a) < len(b): raise Exception('a length must large to b length') result = [] flag = False i=0 la = len(a) lb = len(b) while i <la: j = 0 while j < lb: if i+j < la and a[i+j] == b[j]: j += 1 else : j += 1 flag = False break flag = True if flag: i += lb else: result.append(a[i]) i += 1 return "".join(result)
测试用例:class TestdelInnerStringFunctions(): def setUp(self): pass def tearDown(self): pass def test_nomorl1(self): assert delBString('asdqwe','we') == 'asdq' def test_nomorl2(self): assert delBString('asdqwe','0') == 'asdqwe' def test_nomorl3(self): assert delBString('测试asdqwe','we') == '测试asdq' def test_nomorl4(self): assert delBString('测试asdqwe','测试') == 'asdqwe' def test_nomorl5(self): assert delBString('asdqwe','') == 'asdqwe' def test_nomorl6(self): with pytest.raises(TypeError): delBString('', 0) def test_nomorl7(self): with pytest.raises(TypeError): delBString(0, 'as') def test_nomorl8(self): with pytest.raises(TypeError): delBString(True) def test_nomorl9(self): with pytest.raises(Exception) as excinfo:delBString('acd','acde') assert "a length must large to b length" in str(excinfo.value) assert excinfo.type == Exception