Skip to content

Commit 0018f23

Browse files
增加了超多的新功能!
1 parent 4a2e8b4 commit 0018f23

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

type.py

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
__author__ = "Jerry"
2-
__version__ = "1.2.3"
2+
__version__ = "1.2.4"
33

44
class NotComposedOfNumbersError(Exception):
55
"不是由数字组成的字符串使用String对象的toInt()函数或toInteger()函数时抛出"
@@ -89,6 +89,37 @@ def isNumber(self):
8989
else:
9090
return True
9191

92+
def isEven(self):
93+
"判断数字是否是偶数,返回布尔值(前提是这个字符串是由数字组成的,否则抛出NotComposedOfNumbersError异常)"
94+
if self.toInt() % 2 == 0:
95+
return True
96+
else:
97+
return False
98+
99+
def isOdd(self):
100+
"判断数字是否是奇数,返回布尔值(前提是这个字符串是由数字组成的,否则会出NotComposedOfNumbersError异常)"
101+
if self.toInt() % 2 == 0:
102+
return False
103+
else:
104+
return True
105+
106+
def hasSpace(self):
107+
"判断字符串中是否存在空格,返回布尔值"
108+
for i in self.__str:
109+
if i == " ":
110+
return True
111+
return False
112+
113+
def replace(self, /, past, now):
114+
"在前一个括号内填写字符串内要匹配的元素,后一个括号内填入要被替换成的元素,这样所有匹配的元素都会被替换为要被替换成的元素,最后返回String对象"
115+
return String(self.__str.replace(past, now))
116+
117+
def subString(self, /, range1, range2=None):
118+
"字符串切片。在前一个括号中填入起始索引值,后一个括号中填入结束索引值。若索引值超出了字符串范围,将会返回空字符串"
119+
if range2 is None:
120+
range2 = range1 + 1
121+
return String(self.__str[range1:range2])
122+
92123
def toInt(self):
93124
"如果能,将字符串转成整数,否则抛出NotComposedOfNumbersError异常"
94125
try:
@@ -177,6 +208,24 @@ def __ge__(self, other) -> bool:
177208
else:
178209
return self.__int >= other
179210

211+
def isEven(self):
212+
"判断数字是否是偶数,返回布尔值"
213+
if self.__int % 2 == 0:
214+
return True
215+
else:
216+
return False
217+
218+
def isOdd(self):
219+
"判断数字是否是奇数,返回布尔值"
220+
if self.__int % 2 == 0:
221+
return False
222+
else:
223+
return True
224+
225+
def replace(self, /, past, now):
226+
"在前一个括号内填写字符串内要匹配的元素,后一个括号内填入要被替换成的元素,这样所有匹配的元素都会被替换为要被替换成的元素,最后返回Integer对象"
227+
return Integer(int(self.toStr().replace(past, now)))
228+
180229
def toStr(self):
181230
"将整数转成字符串"
182231
return str(self.__int)

0 commit comments

Comments
 (0)