日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

【STM32】利用 C 语言 strchar() 函数查找字符串中指定字符的位置

發(fā)布時間:2025/4/5 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【STM32】利用 C 语言 strchar() 函数查找字符串中指定字符的位置 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

  • 字符串中查找字符 strchr()
    • 描述
    • 聲明
    • 參數(shù)
    • 返回值
  • 字符串分割 strtok()
    • 描述
    • 聲明
    • 參數(shù)
    • 返回值
  • 自己的函數(shù)

字符串中查找字符 strchr()

描述

C 庫函數(shù) char *strchr(const char *str, int c) 在參數(shù) str 所指向的字符串中搜索第一次出現(xiàn)字符 c(一個無符號字符)的位置。

聲明

下面是 strchr() 函數(shù)的聲明。

char *strchr(const char *str, int c)

參數(shù)

str – 要被檢索的 C 字符串。
c – 在 str 中要搜索的字符。

返回值

該函數(shù)返回在字符串 str 中第一次出現(xiàn)字符 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 庫函數(shù) char *strtok(char *str, const char *delim) 分解字符串 str 為一組字符串,delim 為分隔符。

聲明

下面是 strtok() 函數(shù)的聲明。

char *strtok(char *str, const char *delim)

參數(shù)

str – 要被分解成一組小字符串的字符串。
delim – 包含分隔符的 C 字符串。

返回值

該函數(shù)返回被分解的第一個子字符串,如果沒有可檢索的字符串,則返回一個空指針。

#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);/* 繼續(xù)獲取其他的子字符串 */while( token != NULL ) {printf( "%s\n", token );token = strtok(NULL, s);}return(0); }

結果為:

This is www.runoob.com website

自己的函數(shù)

#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);/* 繼續(xù)獲取其他的子字符串 */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 8

Ref:

  • C 庫函數(shù) - strchr()
  • C語言中常見的字符串處理函數(shù)
  • C 庫函數(shù) - strtok()
  • 總結

    以上是生活随笔為你收集整理的【STM32】利用 C 语言 strchar() 函数查找字符串中指定字符的位置的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。