C - 字串處理

比較字串常用的有:strcmp、strncmp
搜尋字串常用的有:strchr(舊版使用index)、strrchr(舊版使用rindex)、strstr
複製字串常用的有:strcat、strcpy
查詢字串常用的有:strlen
分解字串常用的有:strtok_r(舊版使用strtok)、strsep
記憶體相關常用的有:memset、memcpy、malloc、calloc、free


比較

strcmp


    int strcmp(const char *s1, const char *s2);

兩字串相同回傳0
兩字串不同回傳正號或負號數值


strncmp


    int strncmp(const har *s1, const char *s2, size_t n);

兩字串相同回傳0
兩字串不同回傳正號或負號數值


搜尋

strchr (以前用 index)

從字串左邊開始找字串中的字元

    char *strchr(const char *s, int c);

 
    char *p, *buf = "abcdefabcdef";
    p = strchr(buf, 'd');
    printf("%s\n", p);
    p = strchr(buf, 'z');
    printf("%s\n", p);

=== 執行結果 ===
defabcdef
(null)

找到,回傳找到的指標
找不到,回傳 null 指標

strrchr (以前用 rindex)

從字串右邊開始找字串中的字元

    char *strrchr(const char *s, int c);


    char *p, *buf = "abcdefabcdef";
    p = strrchr(buf, 'd');
    printf("%s\n", p);
    p = strrchr(buf, 'z');
    printf("%s\n", p);

=== 執行結果 ===
def
(null)

找到,回傳找到的指標
找不到,回傳 null 指標

strstr

從字串左邊開始找字串中的字串

    char *strstr(const char *haystack, const char *needle);


    char *p, *buf = "abcdefabcdef";
    p = strstr(buf, "def");
    printf("%s\n", p);
    p = strstr(buf, "zzz");
    printf("%s\n", p);

=== 執行結果 ===
defabcdef
(null)

找到,回傳找到的指標
找不到,回傳 null 指標


複製

strcat

串接字串

    char *strcat(char *dest, const char *src);


    char buf[128] = "abc";
    strcat(buf, "def");
    printf("%s\n", buf);

=== 執行結果 ===
abcdef


strcpy

複製整個字串

    char *strcpy(char *dest, const char *src);


    char buf[128] = "abc";
    strcpy(buf, "def");
    printf("%s\n", buf);

=== 執行結果 ===
def



查詢

strlen

回傳字串長度

    size_t strlen(const char *s);


    char buf[128] = "abc";
    printf("%d\n", strlen(buf));

=== 執行結果 ===
3



分解

strtok_r

依傳入的delim字元,分解字串後回傳結果

char *strtok_r(char *str, const char *delim, char **saveptr);


char string[] = "a/bbb///cc;xxx:yyy:";
char *pick1 = ":;", *pick2 = "/";
char *str1, *str2, *token, *subtoken;
char *saveptr1 = NULL, *saveptr2 = NULL;
int j;

for (j = 1, str1 = string; ; j++, str1 = NULL) {
token = strtok_r(str1, pick1, &saveptr1);
if (token == NULL)
break;
printf("%d: %s\n", j, token);

for (str2 = token; ; str2 = NULL) {
subtoken = strtok_r(str2, pick2, &saveptr2);
if (subtoken == NULL)
break;
printf(" --> %s\n", subtoken);
}
}

=== 執行結果 ===
1: a/bbb///cc
--> a
--> bbb
--> cc
2: xxx
--> xxx
3: yyy
--> yyy

*注意:
    1. 呼叫後會改到原始字串。
    2. 輸入的delim字串,每個字元都是分割符號。
    3. 需呼叫多次,而且第二次之後的呼叫, str 要傳入 NULL

strsep

回傳包含空白的分割字串

char *strsep(char **stringp, const char *delim);


char string[] = "a/bbb///cc;xxx::yyy:";
char *pick = ":;";
char *str, *token;

str = string;
do {
token = strsep(&str, pick);
printf("i=%d, %s, str=%s\n", i, token?:"*", str);
} while (token);

=== 執行結果 ===
i=0, a/bbb///cc, str=xxx::yyy:
i=1, xxx, str=:yyy:
i=2, , str=yyy:
i=3, yyy, str=
i=4, , str=(null)
i=5, *, str=(null)

*注意:
    1. 呼叫後會改到原始字串及指標。
    2. 輸入的delim字串,每個字元都是分割符號。
    3. 需呼叫多次。

註:strtok 無法使用在多執行緒, 有互蓋的問題。strtok_r和strsep都可以用在多執行緒。


記憶體相關


    #include <string.h>
    void *memset(void *s, int c, size_t n);
    void *memcpy(void *dest, const void *src, size_t n);

    #include <stdlib.h>
    void *malloc(size_t size);
    void *calloc(size_t nmemb, size_t size);
    void free(void *ptr);

memset

設定成一樣的值

    char buf[512];
    memset(buf, '\0', sizeof(buf));

memcpy

複製記憶體

    char buf_src[512], buf_dst[512];
    memcpy(buf_dst, buf_src, sizeof(buf_dst));

malloc

分配記憶體(內容可能是亂碼)

    char *p = malloc(512);

calloc

分配記憶體,並把值全部清空

    char *p = calloc(1, 512);

free

釋放記憶體(malloc、calloc都需要)

    char *p = malloc(512);
    free(p);


    char *p = calloc(1, 512);
    free(p);




沒有留言:

張貼留言