【STM32】利用 C 语言 strchar() 函数查找字符串中指定字符的位置
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                【STM32】利用 C 语言 strchar() 函数查找字符串中指定字符的位置
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                文章目錄
- 字符串中查找字符 strchr()
- 描述
- 聲明
- 參數
- 返回值
 
- 字符串分割 strtok()
- 描述
- 聲明
- 參數
- 返回值
 
- 自己的函數
字符串中查找字符 strchr()
描述
C 庫函數 char *strchr(const char *str, int c) 在參數 str 所指向的字符串中搜索第一次出現字符 c(一個無符號字符)的位置。
聲明
下面是 strchr() 函數的聲明。
char *strchr(const char *str, int c)參數
str – 要被檢索的 C 字符串。
 c – 在 str 中要搜索的字符。
返回值
該函數返回在字符串 str 中第一次出現字符 c 的位置,如果未找到該字符則返回 NULL。
#include <stdio.h> #include <string.h>int main () {const char str[] = "http://www.runoob.com";const char ch = '.';char *ret;ret = strchr(str, ch);printf("|%c| 之后的字符串是 - |%s|\n", ch, ret);return(0); }結果為:
|.| 之后的字符串是 - |.runoob.com|字符串分割 strtok()
描述
C 庫函數 char *strtok(char *str, const char *delim) 分解字符串 str 為一組字符串,delim 為分隔符。
聲明
下面是 strtok() 函數的聲明。
char *strtok(char *str, const char *delim)參數
str – 要被分解成一組小字符串的字符串。
 delim – 包含分隔符的 C 字符串。
返回值
該函數返回被分解的第一個子字符串,如果沒有可檢索的字符串,則返回一個空指針。
#include <string.h> #include <stdio.h>int main () {char str[80] = "This is - www.runoob.com - website";const char s[2] = "-";char *token;/* 獲取第一個子字符串 */token = strtok(str, s);/* 繼續獲取其他的子字符串 */while( token != NULL ) {printf( "%s\n", token );token = strtok(NULL, s);}return(0); }結果為:
This is www.runoob.com website自己的函數
#include <stdio.h> #include <string.h> #include <stdlib.h>int main() {char str[] = "標簽坐標: X = 28889 cm , Y = 36 cm, Z = 8 cm";char *token;int XPos=0, YPos=0, ZPos=0;/* 獲取第一個子字符串 */token = strtok(str, " ");//printf("%s\n", token);/* 繼續獲取其他的子字符串 */while (token != NULL){ if (*token == 'X'){ token = strtok(NULL, " "); //printf("%s\n", token);token = strtok(NULL, " "); //printf("XPos=%s\n", token);XPos = atoi(token);}if (*token == 'Y'){ token = strtok(NULL, " "); //printf("%s\n", token);token = strtok(NULL, " "); //printf("YPos=%s\n", token);YPos = atoi(token);}if (*token == 'Z'){ token = strtok(NULL, " "); //printf("%s\n", token);token = strtok(NULL, " "); //printf("ZPos=%s\n", token);ZPos = atoi(token);}token = strtok(NULL, " "); //printf("%s\n", token);}printf("%d\n", XPos);printf("%d\n", YPos);printf("%d\n", ZPos);return (0); }結果為:
28889 36 8Ref:
總結
以上是生活随笔為你收集整理的【STM32】利用 C 语言 strchar() 函数查找字符串中指定字符的位置的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 【STM32】F1 系列驱动全彩显示屏
- 下一篇: 【Matlab】绘制热力图和三维热力图
