15

次のpythonに相当するものは何ですか:

if (strpos($elem,"text") !== false) {
    // do_something;  
}
4

3 に答える 3

38

見つからない場合は -1 を返します。

pos = haystack.find(needle)
pos = haystack.find(needle, offset)

見つからない場合は ValueError を発生させます:

pos = haystack.index(needle)
pos = haystack.index(needle, offset)

部分文字列が文字列内にあるかどうかを単純にテストするには、次を使用します。

needle in haystack

これは、次の PHP と同等です。

strpos(haystack, needle) !== FALSE

http://www.php2python.com/wiki/function.strpos/から

于 2013-06-17T08:30:56.297 に答える
2
if elem.find("text") != -1:
    do_something
于 2013-06-17T08:39:27.543 に答える