比较运营商C字符串 [英] Comparison operators for C strings

查看:122
本文介绍了比较运营商C字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有比较C字符串的一些困难定位信息。据我所知,不像C ++,C不支持运算符重载,所以我不知道是否有什么方法可以检查一个字符串是否大于/小于另一个(例如STR1> STR2)?

I am having some difficulty locating information of comparing C strings. I understand that unlike C++, C does not support operator overloading, so I'm wondering if there is any way to check if one string is greater/less than another (e.g. str1 > str2)?

由于时间提前您的答复。这是老老实实的第一次我竟然不得不问一个问题,因为我无法找到相关的职位之一。

Thanks ahead of time for your responses. This is honestly one of the first times I have actually had to ask a question because I could not find a related post.

推荐答案

有几个,每次服不同的目的(现在省略宽字符变种)。

There are several, each serving different purposes (omitting wide character variants for now).

STRCMP - 比较两个字符串中的字符(与C概念什么字符串相等或不 - 这并不需要与人是怎么想的不谋而合 - 见的strcoll )。有一个变种只比较第一最多的 N 的字符, STRNCMP

strcmp – compares two strings, character by character (with the C notion of what strings are equal or not – that doesn't need to coincide with how humans think – see strcoll). There's a variant for comparing only the first at most n characters, strncmp.

strcasecmp - 比较两个字符串,忽略大小写。有一个变种只比较第一最多的 N 的字符, strncasecmp

的strcoll - 比较两个字符串,观察当前设置区域(这就是为什么它被称为整理,在这种情况下,不比较)。如果你想 SS SS 来比较平等的德国观众,那么这就是你应该使用什么。

strcoll – compares two strings, observing the currently set locale (which is why it's called collation, not comparing in this case). If you want ss and ß to compare equal for a German audience, then this is what you should use.

在这里您可以写

if (string1 > string2) ...

在语言,你必须写

if (strmp(string1, string2) > 0) ...

在C.基本上你两个操作数搬入函数调用,保留了比较运营商与 0 比较吧。

in C. Essentially you move both operands into the function call, retain the comparison operator and compare with 0 instead.

这篇关于比较运营商C字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆