Go to spaces and special symbols
S.strip (). Lstrip (). Rstrip (', ')
Copy string
#strcpy (SSTR1,SSTR2)
SSTR1 = ' strcpy '
SSTR2 = SStr1
SSTR1 = ' Strcpy2 '
Print SSTR2
Connection string
#strcat (SSTR1,SSTR2)
SSTR1 = ' strcat '
SSTR2 = ' Append '
SSTR1 + = SStr2
Print SSTR1
Find characters
#strchr (SSTR1,SSTR2)
# < 0 for not found
SSTR1 = ' STRCHR '
SSTR2 = ' s '
NPos = Sstr1.index (SSTR2)
Print NPos
Comparing strings
#strcmp (SSTR1,SSTR2)
SSTR1 = ' STRCHR '
SSTR2 = ' Strch '
Print CMP (SSTR1,SSTR2)
Scans whether a string contains the specified character
#strspn (SSTR1,SSTR2)
SSTR1 = ' 12345678 '
SSTR2 = ' 456 '
#sStr1 and chars both in SSTR1 and SSTR2
Print Len (SSTR1 and SSTR2)
String length
#strlen (SSTR1)
SSTR1 = ' strlen '
Print Len (SSTR1)
Converts the case in a string
#strlwr (SSTR1)
SSTR1 = ' jcstrlwr '
SSTR1 = Sstr1.upper ()
#sStr1 = Sstr1.lower ()
Print SSTR1
Appends a string of the specified length
#strncat (Sstr1,sstr2,n)
SSTR1 = ' 12345 '
SSTR2 = ' abcdef '
n = 3
SSTR1 + = Sstr2[0:n]
Print SSTR1
String specifying length Comparison
#strncmp (Sstr1,sstr2,n)
SSTR1 = ' 12345 '
SSTR2 = ' 123BC '
n = 3
Print CMP (Sstr1[0:n],sstr2[0:n])
Copy characters of specified length
#strncpy (Sstr1,sstr2,n)
SSTR1 = ' '
SSTR2 = ' 12345 '
n = 3
SSTR1 = Sstr2[0:n]
Print SSTR1
Replaces the first n characters of a string with the specified character
#strnset (Sstr1,ch,n)
SSTR1 = ' 12345 '
ch = ' R '
n = 3
SSTR1 = n * ch + sstr1[3:]
Print SSTR1
Scan string
#strpbrk (SSTR1,SSTR2)
SSTR1 = ' Cekjgdklab '
SSTR2 = ' Gka '
NPos =-1
For C in SSTR1:
If C in SSTR2:
NPos = Sstr1.index (c)
Break
Print NPos
Flip string
#strrev (SSTR1)
SSTR1 = ' ABCDEFG '
SSTR1 = Sstr1[::-1]
Print SSTR1
Find string
#strstr (SSTR1,SSTR2)
SSTR1 = ' ABCDEFG '
SSTR2 = ' CDE '
Print Sstr1.find (SSTR2)
Split string
#strtok (SSTR1,SSTR2)
SSTR1 = ' Ab,cde,fgh,ijk '
SSTR2 = ', '
SSTR1 = Sstr1[sstr1.find (SSTR2) + 1:]
Print SSTR1
#或者
s = ' Ab,cde,fgh,ijk '
Print (S.split (', '))
Connection string
delimiter = ', '
MyList = [' Brazil ', ' Russia ', ' India ', ' China ']
Print Delimiter.join (mylist)
Implementation of Addslashes in PHP
def addslashes (s):
D = {' ': ' \ \ ', ' ' ': ' \ \ ', '/': ' \\\0 ', ' \ \ ': ' \\\\ '}
Return '. Join (D.get (c, c) for C in s)
s = "John ' Johny ' Doe (a.k.a. \" Super joe\ ") \\\0"
Print S
Print Addslashes (s)
Show only letters and numbers
def onlycharnum (s,oth= "):
S2 = S.lower ();
Fomart = ' abcdefghijklmnopqrstuvwxyz0123456789 '
For C in S2:
If not C in Fomart:
s = S.replace (c, ");
return s;
Print (Onlystr ("a000 aa-b"))
Intercept string
str = ' 0123456789 '
Print Str[0:3] #截取第一位到第三位的字符
Print str[:] #截取字符串的全部字符
Print Str[6:] #截取第七个字符到结尾
Print Str[:-3] #截取从头开始到倒数第三个字符之前
Print Str[2] #截取第三个字符
Print Str[-1] #截取倒数第一个字符
Print Str[::-1] #创造一个与原字符串顺序相反的字符串
Print Str[-3:-1] #截取倒数第三位与倒数第一位之前的字符
Print str[-3:] #截取倒数第三位到结尾
Print (str[:-5:-3]) #逆序截取, output 96
Print (Str[:-4:-2]) #逆序截取, output 97
Print (Str[:-6:-2]) #逆序截取, output 975
Python string manipulation