如何檢查值是否與字符串匹配

[英]How do I check if a value matches a string


Major brainfreeze while learning C here.

在這里學習C語言的時候大腦會凍結。

I have a struct here with something like:

這里有一個結構,比如

char *sname;
........
players[i].sname

equalling "James".

等於“詹姆斯”。

I need to check for equality between values like so:

我需要檢查這樣的值之間的相等性:

if (players[i].sname == 'Lee')

but am not having much luck. Is there a str function I should be using or is there anyway to fix up my if statement. It's not working atm.

但我運氣不太好。是否有一個str函數我應該使用或者是否有它來修正if語句。這不是atm工作。

3 个解决方案

#1


8  

The short answer: strcmp().

簡短的回答:strcmp()。

The long answer: So you've got this:

長篇大論的回答是:

if(players[i].sname == 'Lee')

This is wrong in several respects. First, single-quotes mean "character literal" not "string literal" in C.

這在幾個方面是錯誤的。首先,單引號的意思是“字符文字”而不是“字符串文字”。

Secondly, and more importantly, "string1" == "string2" doesn't compare strings, it compares char *s, or pointers to characters. It will tell you if two strings are stored in the same memory location. That would mean they're equal, but a false result wouldn't mean they're inequal.

其次,更重要的是,“string1”=“string2”並不比較字符串,而是比較char *s,或指向字符的指針。它將告訴您兩個字符串是否存儲在相同的內存位置。這意味着它們是相等的,但一個錯誤的結果並不意味着它們是不相等的。

strcmp() will basically go through and compare each character in the strings, stopping at the first character that isn't equal, and returning the difference between the two characters (which is why you have to say strcmp() == 0 or !strcmp() for equality).

strcmp()將基本遍歷並比較字符串中的每個字符,在第一個不相等的字符處停止,並返回兩個字符之間的差異(這就是為什么要為相等而寫入strcmp() = 0或!strcmp())。

Note also the functions strncmp() and memcmp(), which are similar to strcmp() but are safer.

還要注意strncmp()和memcmp()函數,它們與strcmp()類似,但更安全。

#2


8  

You should be using strcmp():

您應該使用strcmp():

if (strcmp(players[i].sname, "Lee") == 0) { ...

Also note that strings in C are surrounded by double quotes: "". Single characters are surrounded by single quotes: ''. I'm not sure exactly what your compiler might be doing with 'Lee', but it's almost certainly not what you want.

還要注意,C中的字符串被雙引號包圍:“”。單字符被單引號包圍:"。我不確定你的編譯器可能在用“Lee”做什么,但它肯定不是你想要的。

#3


3  

You'd be looking for strcmp() from the header <string.h>.

您將從header 中查找strcmp()。

Note that you need a string — 'Lee' is not a string but a multi-character constant, which is allowed but seldom useful, not least because the representation is defined by the compiler, not the C standard.

注意,您需要一個字符串——'Lee'不是一個字符串,而是一個多字符常量,這是允許的,但很少有用,尤其是因為表示是由編譯器定義的,而不是C標准。

If you are looking to compare two strings — call the pointers to them first and second, then you write:

如果你想比較兩個字符串——先調用指針,然后再調用指針,你可以這樣寫:

if (strcmp(first, second) == 0)    // first equal to second
if (strcmp(first, second) <= 0)    // first less than or equal to second
if (strcmp(first, second) <  0)    // first less than second
if (strcmp(first, second) >= 0)    // first greater than or equal to second
if (strcmp(first, second) >  0)    // first greater than second
if (strcmp(first, second) != 0)    // first unequal to second

This, in my view, makes it clear what the comparison is and so the notation should be used. Note that strcmp() may return any negative value to indicate 'less than' or any positive value to indicate 'greater than'.

在我看來,這清楚地說明了什么是比較,所以應該使用符號。注意,strcmp()可以返回任何負值來表示“小於”,也可以返回任何正值來表示“大於”。

Be cautious about using strncmp() instead of strcmp(), which was suggested in one answer. If you have:

要小心使用strncmp()而不是strcmp(),這是在一個答案中建議的。如果你有:

if (strncmp(first, "command", 7) == 0)

then if first contains "commander", the match will be valid. If that's not what you want but you want to use strncmp() anyway, you would write:

如果第一個包含“指揮官”,則該匹配將有效。如果這不是您想要的,但是您想使用strncmp(),您可以這樣寫:

if (strncmp(first, "command", sizeof("command")) == 0)

This will correctly reject "commander".

這將正確地拒絕“指揮官”。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/10/21/72f7b82250c077a4afacf206542172d9.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com