Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simple string match

I want to do a simple string match, searching from the -front- of the string. Are there easier ways to do this with maybe a builtin? (re seems like overkill)

def matchStr(ipadr = '10.20.30.40', matchIP = '10.20.'):
    if ipadr[0:len(matchIP)] == matchIP: return True
    return False
like image 589
tMC Avatar asked Jan 30 '26 23:01

tMC


2 Answers

def matchStr(ipadr = '10.20.30.40', matchIP = '10.20.'):
    return ipadr.startswith(matchIP)
like image 134
Sven Marnach Avatar answered Feb 01 '26 12:02

Sven Marnach


>>> 'abcde'.startswith('abc')
True
like image 30
Michael Lorton Avatar answered Feb 01 '26 13:02

Michael Lorton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!