網(wǎng)站首頁 編程語言 正文
字符串處理函數(shù):
存放和使用字符串:
字符串常量:“fishc”,“小甲魚”,“魚C工作室”字符數(shù)組:
獲取字符串長度:strlen
用法:strlen(str)
注意:
- 這個函數(shù)獲得的是長度(不帶’\0’),不是尺寸。
- 頭文件<string.h>
- 最好用%u(無符號十進(jìn)制數(shù))輸出,因為輸出的是長度,當(dāng)然%d也行。
示例:
#include<stdio.h> #include<string.h> int main() { char str[]="i love fishc.com!"; printf("sozeof(str)= %d\n",sizeof(str)); printf("strlen(str)= %u\n",strlen(str)); return 0; }
拷貝字符串:strcpy和strncpy
用法:strncpy(目標(biāo)字符串,待拷貝字符串);
注意:
- 不可以用變量來拷貝字符串
- 將長的字符串拷貝進(jìn)入短的字符串中會溢出
- C語言不會對數(shù)組的邊界做檢查,如果內(nèi)存溢出也不會報錯
示例:
#include<stdio.h> #include<string.h> int main() { char str1[]="original string"; char str2[]="new string"; char str3[100]; strcpy(str1,str2); 10 strcpy(str3,"copy sucessful"); printf("str1:%s\n",str1); printf("str2:%s\n",str2); printf("str3:%s\n",str3); return 0; }
strncpy:
用法:
strncpy(目標(biāo)字符串,待拷貝字符串,拷貝字符串尺寸);
注意:
- 可定義拷貝進(jìn)去的字符長度
- 如果源字符串長度小于目標(biāo)數(shù)組,會用’\0’來填充額外的空間
- 如果大于,則會截取目標(biāo)數(shù)組的字符串長度來替換,(并且不含’\0’,則需要在結(jié)尾加上’\0’)。
示例:
#include<stdio.h> #include<string.h> int main() { char str1[]="to be or not to be"; char str2[40]; strncpy(str2,str1,5); str2[5]='\0'; //在結(jié)尾加上'\0' printf("str2:%s\n",str2); return 0; }
連接字符串:strcat和strncat
用法:strcat(一號字符串,二號字符串);//把二號加到一號后面
注意:
- strncat和strncpy一樣,是為了限定加上的字符個數(shù)
- 不同的是,在連接之后自動追加結(jié)束符’\0’。
示例:
例一:
#include<stdio.h> #include<string.h> int main() { char str1[]="to be or not to be"; char str2[]="fishc.com!"; strcat(str1," ");//在str1后面加上個空格,沒什么作用,僅僅為了好看點 strcat(str1,str2); printf("str1:%s\n",str1); return 0; }
例二:
#include<stdio.h> #include<string.h> int main() { char str1[]="to be or not to be"; char str2[]="fishc.com!"; strncat(str1," ",2);//2用來限定所加的字符個數(shù) strncat(str1,str2,5); //不需要加'\0'來表示結(jié)束,這個會自己加 printf("str1:%s\n",str1); return 0; }
比較字符串:strcmp和ctrncmp
用法:strcmp(str1,str2)
注意:字符串相等的話會返回0
從返回值來剖析:
該函數(shù)的返回值如下:
- 如果返回值小于 0,則表示 str1 小于 str2。
- 如果返回值大于 0,則表示 str1 大于 str2。
- 如果返回值等于 0,則表示 str1 等于 str2。
示例:
#include<stdio.h> #include<string.h> int main() { char str1[]="fishc.com!"; char str2[]="fishc.com!"; if(!strcmp(str1,str2))//strcmp前面要加'非'(!),因為strcmp判斷相等的話返回值為0 { printf("兩個字符串完全一致!\n"); } else { printf("兩個字符串存在差異!\n"); } return 0; }
strncmp和strcmp和區(qū)別:
限制比較的長度,可在后面加上限制參數(shù)
示例:
#include<stdio.h> #include<string.h> int main() { char str1[]="fishc.cfm!"; char str2[]="fishc.com!"; if(!strncmp(str1,str2,5))//strcmp前面要加'非'(!),因為syrcmp判斷一致的話返回值 為0 { printf("兩個字符串完全一致!\n"); } else { printf("兩個字符串存在差異!\n"); } return 0; }
總結(jié)
原文鏈接:https://blog.csdn.net/HICKER_BOY/article/details/122641233
相關(guān)推薦
- 2023-12-08 Can‘t open the append-only file: Permission denied
- 2022-10-23 redis如何實現(xiàn)清空緩存_Redis
- 2022-09-29 淺談音視頻?pts?dts基本概念及理解_其它綜合
- 2022-06-11 shell編程中for循環(huán)語句的實現(xiàn)過程及案例_linux shell
- 2023-01-30 基于redis樂觀鎖實現(xiàn)并發(fā)排隊_Redis
- 2024-02-16 SpringBoot 事務(wù)管理Transactional 數(shù)據(jù)回滾 數(shù)據(jù)一致性
- 2022-07-21 解決win10系統(tǒng)網(wǎng)絡(luò)連接正常,但是網(wǎng)頁打不開的問題
- 2022-07-16 (ES6以上以及TS) Map對象轉(zhuǎn)數(shù)組
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支