1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
def findchr(string, char) : i = 0; while i < len(string) : if string[i] == char : return i i += 1 return -1
def rfindchr(string, char) : i = len(string) - 1 while i >= 0 : if string[i] == char : return i i -= 1 return -1
def subchr(myString, origchar, newchar) : '将string中的origchar字符替换成newchar字符' import string string.replace(myString,origchar,newchar)
myString = 'abcdefcg' print findchr(myString, 'c') print findchr(myString, 'c') subchr(myString, 'c', 'X') print myString
|