Last active
March 13, 2020 08:50
-
-
Save stilllisisi/5b82e672470a5ddb8fea7f7c5dfd6270 to your computer and use it in GitHub Desktop.
【C-字符串】截取从某位置开始指定长度子字符串方法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** c语言标准库没有截取部分字符串的函数,为啥?因为用现有函数strncpy,很容易做到! **/ | |
| char dest[4] = {""}; | |
| char src[] = {"123456789"}; | |
| strncpy(dest, src, 3); | |
| puts(dest); | |
| //输出结果为 123 | |
| //strncpy函数中的参数是字符串数组的名字,而数组名本质上是指针,那么,src+3 就可以实现将 src中从第4个字符开始复制n个字符给 dest 了 | |
| char dest[4] = {""}; | |
| char src[] = {"123456789"}; | |
| strncpy(dest, src+3, 3); | |
| dest[4] = '\0'; //n<sizeof(src)时,必须有这一句,不然输出出错 | |
| puts(dest); | |
| //输出结果为 456 | |
| //注意:比较两个char*字符串是否相等,if(strcmp(recData,name) == 0)来判断。 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
1