文章目錄
- 前言
- 字串是誰
- 經常聽說字串字面量,究竟是什么鬼?
- 字串字面量如何存盤的
- C語言字符陣列與字符指標
- C語言中的字串庫
- 1、strlen函式
- 2、strcat 和 strncat函式
- 3、strcmp 和 strncmp函式
- 4、strcpy 和 strncpy函式
前言
本文是針對對字串有疑惑的初學者,例如:對C語言中的字串并不了解,不太會使用,學過其他編程語言,現在轉入了C語言,但是在C語言中使用字串時不能像Java一樣如愿以償,自由自在的使用,那么就可以看本篇文章,本篇文章不會涉及太深的東西,太深的東西對于初學者會受不了的,
字串是誰
說到字串是誰,那么需要提一下字符是什么,沒有字符就沒有字串,
像我們學的“每一個”英文字符(a,b,c…)都是屬于字符,并且漢字、數字、標點符號都是屬于一個字符;
像“我是誰,我在哪”這7個字符合起來就是一個字串,那么串的話其實就是多個字符合在一起的結果,
經常聽說字串字面量,究竟是什么鬼?
經常聽別人說字串字面量,字面量的這是啥呢?
其實你一點也磨陌生,可能只是對這個名字陌生一些,你在學習Hello World的時候,其實就在用了,沒錯用雙引號包含的“Hello World”就是字串字面量,
printf("Hello World!");
執行結果:
Hello World!
我們想換行時,可以加個\n:
printf("Hello \n World!");
結果:
Hello
World!
字串字面量如何存盤的
例如字串:"Hello World!"
你看著是一個字串,實際上在存盤的時候,是像陣列一樣,一個字符一個字符分開存的,
[“H”,“e”,“l”,“l”,“o”," “,“W”,“o”,“r”,“l”,“d”,”!","\0"];
在存盤的時候,每個字串的結尾是包含"\0",所以一般我們如果不知道一個字串的長度如何遍歷每個字符呢?那就可以使用"\0"條件來判斷,
C語言字符陣列與字符指標
我們可以利用char型別的陣列,來定義和初始化字串,
char str[12] = "I Love You!";
char *pStr = "I Love You!";
我們在列印的時候,需要使用%s來格式化數值:
printf("str=%s\n",str);
printf("pStr=%s\n",pStr);
列印結果:
str=I Love You!
pStr=I Love You!
也支持多種單個訪問方式:
printf("*(str+2)=%c\n",*(str+2));
printf("*(pStr+2)=%c\n",*(pStr+2));
結果:
*(str+2)=L
*(pStr+2)=L
C語言中的字串庫
在Java中有String型別的jar包,在C語言中也有相應的字串庫,無論是Java中的jar包,還是C語言中的庫,其實都是一些封裝好的工具,以便給他人使用,
在實際開發中,我們掌握這些庫的基本用法是必須的,可以大大的提高我們的作業效率,
在Linux中我們可以使用man命令來查看幫助手冊,
例如:
man string
會出現下面這樣的結果:

可以看到:

這些都是我現在這個Ubuntu系統中的string庫中所提供的一些功能的函式,
在此我帶著熟悉幾個常用的函式,
我挑選了幾個常用的,其實也就四種:統計字串中的字符個數、字串拼接、字串比較、字串拷貝/賦值,
SYNOPSIS
#include <string.h>
size_t strlen(const char *s);
Return the length of the string s.
char *strcat(char *dest, const char *src);
Append the string src to the string dest, returning a pointer dest.
char *strncat(char *dest, const char *src, size_t n);
Append at most n bytes from the string src to the string dest, returning a pointer to dest.
int strcmp(const char *s1, const char *s2);
Compare the strings s1 with s2.
int strncmp(const char *s1, const char *s2, size_t n);
Compare at most n bytes of the strings s1 and s2.
char *strcpy(char *dest, const char *src);
Copy the string src to dest, returning a pointer to the start of dest.
char *strncpy(char *dest, const char *src, size_t n);
Copy at most n bytes from string src to dest, returning a pointer to the start of dest.
我們如何想使用的話,需要先include一下頭檔案:
本次使用的代碼框架如下:
#include <stdio.h>
#include <string.h>
int main(void)
{
return 0;
}
1、strlen函式
從函式的名字上也不難看出,這個是跟字串和長度有關系的函式,
如果第一次使用的話,可以使用:man strlen來查看這個函式的使用說明,

解釋的非常詳細,
可以看到strlen()函式在計算傳入的字串s的時候是不計算結束符"\0"的,
回傳值是傳入字串s的位元組數/字符格式,
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[12] = "I Love You!";
char *pStr = "I Love You!";
printf("str=%s\n",str);
printf("pStr=%s\n",pStr);
int strLen = strlen(str);
int pStrLen = strlen(pStr);
printf("str len:%d \n",strLen);
printf("pStr len:%d \n",pStrLen);
return 0;
}
結果:
可以看到無論是字符陣列還是字符指標,都是可以統計出來有多少個字符的
zhenghui@zhlinux:~/桌面/code/string$
zhenghui@zhlinux:~/桌面/code/string$ make strtest && ./strtest
cc strtest.c -o strtest
str=I Love You!
pStr=I Love You!
str len:11
pStr len:11
zhenghui@zhlinux:~/桌面/code/string$
計算時到底有沒有跟陣列的大小有關系?
char str[12] = "I Love You!";
char str2[100] = "I Love You!";
char str3[100] = "I Love You! ";
int strLen = strlen(str);
int strLen2 = strlen(str2);
int strLen3 = strlen(str3);
printf("str len:%d \n",strLen);
printf("str2 len:%d \n",strLen2);
printf("str3 len:%d \n",strLen3);
列印結果:
str len:11
str2 len:11
str3 len:26
可以看到str2和str統計的數量是一樣的,
從上面結果來看,陣列的大小并不決定這個字串的長度,
strlen函式只統計“\0”結尾之前的字數,而且不算“\0”這個字符,
2、strcat 和 strncat函式
不了解strcat函式可以直接在linux的命令列中輸入:man strcat
就可以看到有兩個相關的函式:
SYNOPSIS
#include <string.h>
char *strcat(char *dest, const char *src);
char *strncat(char *dest, const char *src, size_t n);
還有很詳細的說文,
The strcat() function appends the src string to the dest string, overwriting the terminating null byte
('\0') at the end of dest, and then adds a terminating null byte. The strings may not overlap, and the
dest string must have enough space for the result. If dest is not large enough, program behavior is un‐
predictable; buffer overruns are a favorite avenue for attacking secure programs.
這個strcat函式是用來拼接字串的,分別傳入dest和src字串,最終把src拼接到dest中進行回傳,
實驗代碼:
zhenghui@zhlinux:~/桌面/code/string$
zhenghui@zhlinux:~/桌面/code/string$ cat strcatTest.c
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[12] = " I ";
char str2[100] = " Love ";
char *pStr = " You!iiiiii ";
int sl = strlen(str1);
printf("str1 len :%d \n",sl);
printf("str1=%s\n",str1);
printf("str2=%s\n",str2);
printf("pStr=%s\n",pStr);
strcat(str1,str2);
strcat(str1,pStr);
printf("#####################\n");
printf("str1 :%s \n",str1);
sl = strlen(str1);
printf("str1 len :%d \n",sl);
return 0;
}
zhenghui@zhlinux:~/桌面/code/string$
執行結果:
用法很簡單,就是傳入兩個需要拼接的字串即可
zhenghui@zhlinux:~/桌面/code/string$ make strcatTest && ./strcatTest
cc strcatTest.c -o strcatTest
str1 len :3
str1= I
str2= Love
pStr= You!iiiiii
#####################
str1 : I Love You!iiiiii
str1 len :21
zhenghui@zhlinux:~/桌面/code/string$
相比較來說,strncat比strcat安全一些,
例如:
如果a的空間是有限的,b的長度又很大,那么就會超出了a的大小,
那么為了安全,就可以使用strncat來指定拼接的大小,
strcat(a,b);
為了安全,我們可以使用strncat:
zhenghui@zhlinux:~/桌面/code/string$
zhenghui@zhlinux:~/桌面/code/string$
zhenghui@zhlinux:~/桌面/code/string$ cat strcatTest.c
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[12] = " I ";
char str2[100] = " Love ";
char *pStr = " You!iiiiii ";
int sl = strlen(str1);
printf("str1 len :%d \n",sl);
printf("str1=%s\n",str1);
printf("str2=%s\n",str2);
printf("pStr=%s\n",pStr);
//strcat(str1,str2);
//strcat(str1,pStr);
strncat(str1,str2,sizeof(str1) - strlen(str1) - 1);
strncat(str1,pStr,sizeof(str1) - strlen(str1) - 1);
printf("#####################\n");
printf("str1 :%s \n",str1);
sl = strlen(str1);
printf("str1 len :%d \n",sl);
return 0;
}
zhenghui@zhlinux:~/桌面/code/string$
可以看到結果也是正常的:
zhenghui@zhlinux:~/桌面/code/string$ make strcatTest && ./strcatTest
cc strcatTest.c -o strcatTest
str1 len :3
str1= I
str2= Love
pStr= You!iiiiii
#####################
str1 : I Love Y
str1 len :11
zhenghui@zhlinux:~/桌面/code/string$
我們使用sizeof(str1) - strlen(str1)就可以計算出str1所剩余的空間,-1是為了給“\0”留出空間,
strncat(str1,str2,sizeof(str1) - strlen(str1) - 1);
3、strcmp 和 strncmp函式
在C語言日常開發中,strcmp是非常常用的,我們做字串比較,兩個字串是否相等,我們直接呼叫這個函式就可以很好的解決這個難題,
我們仍然可以使用:man strcmp來查看幫助手冊:
SYNOPSIS
#include <string.h>
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
DESCRIPTION
The strcmp() function compares the two strings s1 and s2. The locale is not taken into account (for a lo‐
cale-aware comparison, see strcoll(3)). 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.
The strncmp() function is similar, except it compares only the first (at most) n bytes of s1 and s2.
RETURN VALUE
The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or
the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2.
這是用來比較兩個字串的,
原型如下:
SYNOPSIS
#include <string.h>
int strcmp(const char *s1, const char *s2);
int strncmp(const char *s1, const char *s2, size_t n);
我們需要傳遞兩個字串s1和s2;
回傳值是:
回傳值是一個大于、小于或等于0的值,
如果strcmp(s1,s2)==0:說明s1和s2相等;
如果strcmp(s1,s2) > 0:說明s1 > s2;
如果strcmp(s1,s2) < 0:說明s1 < s2;
當然了<=和>=運算子也是可以使用的,
RETURN VALUE
The strcmp() and strncmp() functions return an integer less than, equal to, or greater than zero if s1 (or
the first n bytes thereof) is found, respectively, to be less than, to match, or be greater than s2.
實驗代碼:
zhenghui@zhlinux:~/桌面/code/string$ cat strcmpTest.c
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[12] = "abc";
char *str2 = "ABC";
printf("str1=%s\n",str1);
printf("str2=%s\n",str2);
printf("#####################\n");
int res1 = strcmp(str1,str2);
printf("strcmp(str1,str2) :%d \n",res1);
int res2 = strcmp(str2,str1);
printf("strcmp(str2,str1) :%d \n",res2);
if(res1 > 0)
{
printf("res1:%s > %s \n",str1,str2);
}
if(res2 < 0)
{
printf("res2:%s < %s \n",str2,str1);
}
return 0;
}
zhenghui@zhlinux:~/桌面/code/string$
實驗結果:
zhenghui@zhlinux:~/桌面/code/string$ make strcmpTest && ./strcmpTest
make: “strcmpTest”已是最新,
str1=abc
str2=ABC
#####################
strcmp(str1,str2) :32
strcmp(str2,str1) :-32
res1:abc > ABC
res2:ABC < abc
zhenghui@zhlinux:~/桌面/code/string$
4、strcpy 和 strncpy函式
從字面上看,看著就很想copy拷貝,
沒錯,這個就是拷貝的功能,
同樣,我們使用:man strcpy來查看幫助手冊,
也有很詳細的說明:
SYNOPSIS
#include <string.h>
char *strcpy(char *dest, const char *src);
char *strncpy(char *dest, const char *src, size_t n);
DESCRIPTION
The strcpy() function copies the string pointed to by src, including the terminating null byte ('\0'), to
the buffer pointed to by dest. The strings may not overlap, and the destination string dest must be large
enough to receive the copy. Beware of buffer overruns! (See BUGS.)
The strncpy() function is similar, except that at most n bytes of src are copied. Warning: If there is no
null byte among the first n bytes of src, the string placed in dest will not be null-terminated.
If the length of src is less than n, strncpy() writes additional null bytes to dest to ensure that a total
of n bytes are written.
廢話不多說,直接上代碼:
zhenghui@zhlinux:~/桌面/code/string$
zhenghui@zhlinux:~/桌面/code/string$ cat strcpyTest.c
#include <stdio.h>
#include <string.h>
int main(void)
{
char str1[12] = " abc aowmdi9";
char *str2 = " ABC 9999";
printf("str1=%s\n",str1);
printf("str2=%s\n",str2);
printf("#####################\n");
strcpy(str1,str2);
printf("strcpy(str1,str2):%s \n",str1);
return 0;
}
zhenghui@zhlinux:~/桌面/code/string$
實驗結果:
zhenghui@zhlinux:~/桌面/code/string$ make strcpyTest && ./strcpyTest
make: “strcpyTest”已是最新,
str1= abc aowmdi9
str2= ABC 9999
#####################
strcpy(str1,str2): ABC 9999
zhenghui@zhlinux:~/桌面/code/string$
通過下面代碼可以看出,直接把str2的內容拷貝到了str1中,直接覆寫了,
strcpy(str1,str2);
重點:其實strcpy是用來解決我們不能用賦值運算子來賦值的操作的問題,
那么什么時候,不能用“=”號來賦值呢?
例如:
我們定義一個陣列,一開始不賦值,然后等我們業務邏輯到達的時候,再賦值:
char str3[10];
假設到了該賦值的地方:
str3 = "zhenghui";
這個地方會執行錯誤的,
可以看下執行結果:
zhenghui@zhlinux:~/桌面/code/string$ make strcpyTest && ./strcpyTest
cc strcpyTest.c -o strcpyTest
strcpyTest.c: In function ‘main’:
strcpyTest.c:11:7: error: assignment to expression with array type
11 | str3 = "zhenghui";//錯誤
| ^
make: *** [<內置>:strcpyTest] 錯誤 1
zhenghui@zhlinux:~/桌面/code/string$
像這種特殊的賦值,我們可以利用strcpy來解決:
strcpy(str3,"zhenghui");
執行就沒問題了,
如果帶引數n的話,就是用來限制copy的資料值的多少,也是為了安全才有的,
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/291907.html
標籤:其他
