Python 字串切割函式設計

2021-07-01 23:06:11 字數 646 閱讀 3100

s="   fs.fes..23...43....tghf    "

print "要切割的字串為:",s ,"\n"

s = s.strip() #去掉字串左右兩邊空格

print "輸出去掉空格的字串:",s,"\n"

# sep 為切割字串的符號

sep = '.'

def my_split(src, sep): #自定義my_split()函式

a = s.find(sep) #find返回查詢字元開始的索引

w1 = s[:a]

print(w1)

b = a

n = len(sep)

while b <= len(s) and b != -1:

while s[a:a + n] == sep:

a = a + n

b = s.find(sep, a) #從a開始查詢sep

if b != -1:

print(s[a:b])

else:

print(s[a:])

a = b

print '呼叫 my_split函式,提取切割出的字串:\n'

my_split(s, sep)

python基礎 字串切割函式split

字串切割函式split,以及需要留意的深坑 字串切割 str 我的暱稱是奔奔,我的年齡是18,我的愛好是python res str.split print res 指定切割次數 res str.split 1 print res todo 如果切割符在左右兩端.那麼 定會出現空字串.深坑請留意 s...

C 字串切割函式

strtok的典型用法是 p strtok s4,split while p null 需要注意的是 如果s4是字串常量,執行這一段 會報出段錯誤。因為strtok為了處理方便,會更改字串s4中的一些字元 將分隔符split更改為 0 而字串常量是不能更改的,所以會報段錯誤。第二個需要注意的點是 s...

SQL Server字串切割函式

複製 如下 create function fgewww.cppcns.comtstrbysplit source varchar max index int,splitchar varchar 1 程式設計客棧 returns varchar max as begin declare len in...