词条 | strncmp |
释义 | 函数名: strncmp 功 能: 串比较 用 法: int strncmp(char *str1, char *str2, int maxlen); 说明:此函数功能即比较字符串str1和str2的前maxlen个字符。如果前maxlen字节完全相等,返回值就=0;在前maxlen字节比较过程中,如果出现str1[n]与str2[n]不等,则返回(str1[n]-str2[n])。 程序例: #include <string.h> #include <stdio.h> int main(void) { char *buf1 = "aaabbb", *buf2 = "bbbccc", *buf3 = "ccc"; int ptr; ptr = strncmp(buf2,buf1,3); if (ptr > 0) printf("buffer 2 is greater than buffer 1\"); else if(ptr<0) printf("buffer 2 is less than buffer 1\"); ptr = strncmp(buf2,buf3,3); if (ptr > 0) printf("buffer 2 is greater than buffer 3\"); else if(ptr<0) printf("buffer 2 is less than buffer 3\"); return(0); } PHP中: 函数名: strncmp () 功 能:比较字符串的前N个字符 用 法: int strncmp(string str1, char str2, int len); 说明:比较字符串str1和str2的大小,如果str1小于str2,返回值就<0,反之如果str1大于str2,返回值就>0,如果str1等于str2,返回值就=0,len指的是str1与str2的比较的字符数。此函数功能即比较字符串str1和str2的前len个字符。 提示:该函数区分大小写。 示例:<?php $str1="I like php !"; $str2="i an fine !"; echo strncmp($str1,str2,2); ?> 结果为:-1 |
随便看 |
百科全书收录4421916条中文百科知识,基本涵盖了大多数领域的百科知识,是一部内容开放、自由的电子版百科全书。