linux c语言字符串比较问题

linux c语言字符串比较问题,第1张

linux中,c语言字符串比较采用strcmp()函数

在linux命令行下,man strcmp,可以看到函数说明:

$ man strcmp

...

NAME

       strcmp, strncmp - compare two strings

SYNOPSIS

       #include <string.h>

       int strcmp(const char *s1, const char *s2)

DESCRIPTION

       The  strcmp()  function  compares  the  two  strings s1 and s2.  It returns an integer less than, equal to, or greater than zero if s1 is found, respectively, to be less than, to match, or be greater than s2.

       strcmp函数比较两个字符串s1和s2. 函数返回小于0,等于0,或大于0的整数,分别对应s1<s2, s1=s2, s1<s2 .

比如 s1 = "good"   s2="...this is good", 从s2取后四位进行两个字符串比较,代码如下:

#include <stdio.h>

#include <string.h>

int main()

{

char *s1 = "good" ,  *s2="...this is good" 

int len=strlen(s2)

if ( len >= 4 ) //如果串长超过4位,则移动指针到最后四位的位置

s2 =s2+len-4

printf("s2=%s\n", s2 ) //输出移位后的字符串内容

printf("compare s1,s2=%d\n", strcmp(s1,s2) ) //输出0,表示相同

return 0

}

只需要一个等号吧。

#!/bin/sh

var1="xxx"

var2="yyy"

if [ "$var1" = "$var2" ]then

echo "The same!"

else

echo "Different!"

fi

我这里一直是输出a的,现象都跟你描述的不一致啊。

如果改为:if [[ "$a" = "a*" ]]then ,才输出b。

你这里用的是通配符,其实可以用正则匹配:

if [[ "$a" =~ "a.*" ]]then

这样也是输出a


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/9013744.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-23
下一篇 2023-04-23

发表评论

登录后才能评论

评论列表(0条)

保存