python中的strncmp

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14948900/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 12:56:04  来源:igfitidea点击:

strncmp in python

python

提问by R11

I am parsing through a file with a list of paths. I am trying to see if one path is under a specific directory or not. So I have two strings: S1 = '/tmp/'and S2 = '/tmp/file.txt'

我正在解析一个包含路径列表的文件。我正在尝试查看一个路径是否在特定目录下。所以我有两个字符串:S1 = '/tmp/'S2 = '/tmp/file.txt'

If I want to check if S2contains S1plus some extra bytes, in C, I would do a strncmpof S1and S2upto strlen(S1)bytes. Is there a way to do that in Python? I am new to Python and do not know all the modules available to me yet. I could implement this trivially by just iterating over the characters in the strings and comparing, but want to find out if there is anything that gives me these kind of helper functions by default

如果我想检查是否S2包含S1加上一些额外的字节,在 C 中,我会做一个strncmpofS1S2uptostrlen(S1)个字节。有没有办法在 Python 中做到这一点?我是 Python 新手,还不知道所有可用的模块。我可以通过迭代字符串中的字符并进行比较来轻松实现这一点,但想知道是否有任何东西在默认情况下为我提供了这些帮助函数

采纳答案by mgilson

Yes. You can do: if a in b:That will check if ais a substring anywhere in b.

是的。你可以这样做: if a in b:这将检查是否a是一个子随时随地b

e.g.

例如

if 'foo' in 'foobar':
    print True

if 'foo' in 'barfoo':
    print True

From your post, it appears you want to only look at the start of the strings. In that case, you can use the .startswithmethod:

从您的帖子中,您似乎只想查看字符串的开头。在这种情况下,您可以使用以下.startswith方法:

if 'foobar'.startswith('foo'):
    print "it does!"

Similarly, you can do the same thing with endswith:

同样,您可以使用以下命令执行相同的操作endswith

if 'foobar'.endswith('bar'):
    print "Yes sir :)"

finally, maybe the most literal translation of strncmpwould be to use slicing and ==:

最后,也许最直白的翻译strncmp是使用切片和==

if a[:n] == b[:n]:
    print 'strncmp success!'

Python also has many facilities for dealing with path names in the os.pathmodule. It's worth investigating what is in there. There are some pretty neat functions.

Python 也有许多处理os.path模块中路径名的工具。里面有什么值得研究。有一些非常简洁的功能。

回答by rgrinberg

You're probably looking for os.path.commonprefix.

您可能正在寻找os.path.commonprefix.

for example: os.path.commonprefix(['/tmp/','/tmp/file.txt'])will return '/tmp/

例如:os.path.commonprefix(['/tmp/','/tmp/file.txt'])会回来'/tmp/

so you should check for len(os.path.commonprefix([s1,s2])) > 0

所以你应该检查 len(os.path.commonprefix([s1,s2])) > 0

Check out docs here: http://docs.python.org/2/library/os.path.html

在此处查看文档:http: //docs.python.org/2/library/os.path.html

回答by Ali-Akber Saifee

you can use the findmethod of a string

你可以使用find字符串的方法

>>> s1 = "/tmp"
>>> s2 = "/tmp/file.txt"
>>> s2.find(s1)
0

回答by Jiangzhou

You can test that by

你可以通过

S2[:len(S1)] == S1

or even simpler:

甚至更简单:

S2.startswith(S1)