本文主要讲解在 Linux平台下 string.h 库中的字符串处理函数以及其中额外带的内存操作函数。

任何语言中涉及的常规操作

其中使用频率从上到下依次减小

  • 计算字符串的长度:strlen()
  • 比较字符串是否相等:strcmp()strncmp()
  • 查询一个字符在字符串中的索引位置:由于有指针的原因,直接返回对应字符位置的指针,更容易操作
  • 分割字符串:采用偏移数组起始位置与strncpy()memcpy()实现
  • 查找某个字符串在字符串中出现的位置:strstr()
  • 字符串全部变为大写或小写:
  • 查询字符串中是否存在某个字符:strchr()strnchr()

字符串处理函数

为了提高程序在不同机器上的移植性,利用 typedef 定义新类型名,即typedef unsigned int size_t

在 Linux 系统中 string.h 的库函数关于字符串处理的有以下函数内容。

函数 说明
char *strcat(char *restrict, const char *restrict); 把最后一个参数连接到第二个参数的后面
char *strchr(const char * cs, int c); 返回字符 c 在 cs 中第一次出现的位置,若不存在,返回 NULL
int strcmp(const char * ct, const char * cs); 比较字符串 ct 和 cs,根据字符串的大小相应返回负数、零、正数
int strcoll(const char * ct, const char * cs); 比较字符串 ct 和 cs,根据字符串大小相应返回负数、零、正数。与strcmp()区别是根据 LC_COLLATE 参数提供的字典顺序比较
char *strcpy(char *ct, const char *cs); 把 cs(包含’\0’)赋值到 ct,并返回 ct
size_t strcspn(const char * cs, const char * ct); 返回 cs 不包含 ct 的字符的前缀的长度,取补
char *strdup(const char * cs); 不属于标准 C 函数,自动开辟内存将 cs 赋值到对应内容,然后返回指针。与strcpy()区别是此函数不需要接受一个已经开辟好内存的指针。注意:此函数在使用完后必须使用free()释放内存空间。
char *strerror(int errnum); 从内部数组中搜索错误号 errnum,并返回一个指向错误消息字符串的指针。 生成的错误字符串取决于开发平台和编译器。
int strerror_r(int errnum, char *buf, size_t n) 根据 errnum 错误码获取错误信息并存放到 n 大小的 buf。
size_t strlen(const char * str); 计算字符串 str 的长度
char *strncat(char *dest, const char *src, size_t n); 把 src 所指向的字符串追加到 dest 所指向的字符串的结尾,直到 n 字符长度为止
char *strrchr(const char *, int); 在参数 str 所指向的字符串中搜索最后一次出现字符 c(一个无符号字符)的位置
int strncmp(const char * str1, const char * str2, size_t n); 把 str1 和 str2 进行比较,最多比较前 n 个字节。
char *strncpy(char *dest, const char *src, size_t n); 把 src 所指向的字符串复制到 dest,最多复制 n 个字符
size_t strspn(const char * str1, const char * str2); 检索字符串 str1 中第一个不在字符串 str2 中出现的字符下标
char *strpbrk(const char *str1, const char *str2); 检索字符串 str1 中第一个匹配字符串 str2 中字符的字符,不包含空结束字符
char *strstr(const char *haystack, const char *needle); 在字符串 haystack 中查找第一次出现字符串 needle 的位置,不包含终止符 ‘\0’
char *strtok(char *str, const char *delim) 分解字符串 str 为一组字符串,delim 为分隔符
char *strtok_r(char str, const char *delim, char *saveptr); 属于 Linux 平台提供的函数,以 delim 为分隔符分割 str 字符串并返回第一个被分割的字符串,剩余没被分割的保存在 saveptr 中

使用例子

<!--hexoPostRenderEscape:<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;string.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdlib.h&gt;</span></span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">include</span><span class="meta-string">&lt;stdio.h&gt;</span></span></span><br><span class="line"></span><br><span class="line"><span class="meta">#<span class="meta-keyword">define</span> str3 <span class="meta-string">&quot;abc&quot;</span></span></span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">int</span> <span class="title">main</span><span class="params">()</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//--------------------------------------------------------------------------------------</span></span><br><span class="line">    <span class="comment">//          比较字符串</span></span><br><span class="line">    <span class="comment">//--------------------------------------------------------------------------------------</span></span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;\n*********************************\n&quot;</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">char</span>* str1 = <span class="string">&quot;bbc&quot;</span>;</span><br><span class="line">    <span class="keyword">char</span>* str2 = <span class="built_in">malloc</span>(<span class="number">3</span> * <span class="keyword">sizeof</span>(<span class="keyword">char</span>));</span><br><span class="line">    <span class="built_in">strcpy</span>(str2, str1);</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;str1=%d, str2=%d, str3=%d, str4=%d\n&quot;</span>,</span><br><span class="line">                                                <span class="built_in">strlen</span>(str1),</span><br><span class="line">                                                <span class="built_in">strlen</span>(str2),</span><br><span class="line">                                                <span class="built_in">strlen</span>(str3), </span><br><span class="line">                                                <span class="built_in">strlen</span>(<span class="string">&quot;abc&quot;</span>));</span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;strcmp str1 equal str3 is %d\n&quot;</span>, <span class="built_in">strcmp</span>(str1, str3));</span><br><span class="line"></span><br><span class="line">    <span class="comment">//--------------------------------------------------------------------------------------</span></span><br><span class="line">    <span class="comment">//          剪切字符串</span></span><br><span class="line">    <span class="comment">//--------------------------------------------------------------------------------------</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;\n*********************************\n&quot;</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 剪切具体长度的字符串</span></span><br><span class="line">    <span class="keyword">char</span> *str4 = <span class="string">&quot;abc&quot;</span>;</span><br><span class="line">    <span class="keyword">char</span> *str5 = <span class="built_in">malloc</span>(<span class="number">2</span> * <span class="keyword">sizeof</span>(<span class="keyword">char</span>));</span><br><span class="line">    <span class="built_in">strcpy</span>(str5, str4+<span class="number">1</span>);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;substring:str4 sub(1, strlen(str4)) str5 = %s\n&quot;</span>, str5);</span><br><span class="line">    <span class="built_in">free</span>(str5);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 剪切后缀</span></span><br><span class="line">    <span class="keyword">char</span>       *str6 = <span class="string">&quot;abc.mp4&quot;</span>;</span><br><span class="line">    <span class="keyword">const</span> <span class="keyword">char</span> ch    = <span class="string">&#x27;.&#x27;</span>;</span><br><span class="line">    <span class="keyword">char</span>       *ret;</span><br><span class="line">    ret = <span class="built_in">strrchr</span>(str6, ch);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;substring:str6 suffix = %s\n&quot;</span>, ret + <span class="number">1</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">//--------------------------------------------------------------------------------------</span></span><br><span class="line">    <span class="comment">//          判断字符与字符串是否存在</span></span><br><span class="line">    <span class="comment">//--------------------------------------------------------------------------------------</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;\n*********************************\n&quot;</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 查询字符串是否存在</span></span><br><span class="line">    <span class="keyword">char</span> *str7       = <span class="string">&quot;abcdef&quot;</span>;</span><br><span class="line">    <span class="keyword">const</span> <span class="keyword">char</span> *str8 = <span class="string">&quot;def&quot;</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;contain:str7 contain str8 = %s\n&quot;</span>, <span class="built_in">strstr</span>(str7, str8));</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 查询某个字符是否存在</span></span><br><span class="line">    <span class="keyword">char</span> *str9 = <span class="string">&quot;hello world&quot;</span>;</span><br><span class="line">    <span class="keyword">const</span> <span class="keyword">char</span> char1 = <span class="string">&#x27;a&#x27;</span>, char2 = <span class="string">&#x27;e&#x27;</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;contain: str9 contain char1 = %s\n&quot;</span>, <span class="built_in">strchr</span>(str9, char1));</span><br><span class="line">    </span><br><span class="line">    </span><br><span class="line">    <span class="comment">//--------------------------------------------------------------------------------------</span></span><br><span class="line">    <span class="comment">//          拼接字符串</span></span><br><span class="line">    <span class="comment">//--------------------------------------------------------------------------------------</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;\n*********************************\n&quot;</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 拼接字符串</span></span><br><span class="line">    <span class="keyword">char</span> *str10 = <span class="string">&quot;hello&quot;</span>;</span><br><span class="line">    <span class="keyword">char</span> *str11 = <span class="string">&quot;world&quot;</span>;</span><br><span class="line">    <span class="keyword">char</span> *str12 = <span class="built_in">malloc</span>(<span class="built_in">strlen</span>(str10) + <span class="built_in">strlen</span>(str11));</span><br><span class="line">    <span class="built_in">strcat</span>(str12, str10);</span><br><span class="line">    <span class="built_in">strcat</span>(str12, str11);</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;concat: str10 concat str11 = %s\n&quot;</span>, str12);</span><br><span class="line">    <span class="built_in">free</span>(str12);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">//--------------------------------------------------------------------------------------</span></span><br><span class="line">    <span class="comment">//          字符串转换</span></span><br><span class="line">    <span class="comment">//--------------------------------------------------------------------------------------</span></span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;\n*********************************\n&quot;</span>);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">    <span class="comment">// 字符串全部大小写</span></span><br><span class="line"><span class="meta">#<span class="meta-keyword">ifdef</span> WIN32</span></span><br><span class="line">    <span class="keyword">char</span> *str10 = <span class="string">&quot;hello world&quot;</span>;</span><br><span class="line">    <span class="built_in">printf</span>(<span class="string">&quot;convert: str10 convert up = %s&quot;</span>, strupr(str10));</span><br><span class="line"><span class="meta">#<span class="meta-keyword">endif</span></span></span><br><span class="line"></span><br><span class="line">    <span class="comment">// url 转换为 utf8 没有基础的实现</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>:hexoPostRenderEscape-->

引用

strcmp 和 strcoll的区别

strcpy和strdup比较和详解

strcmp、wcscmp、_mbscmp