欢迎访问宙启技术站
智能推送

常用的字符串操作函数及其用法

发布时间:2023-06-13 07:35:49

字符串操作函数是编程中非常重要的一部分,因为字符串操作是实现输入、输出、文件读取和处理XML、HTML等中的文本的基础。本文将讨论常用的字符串操作函数及其用法。

1. strlen(求字符串长度)

strlen函数用于求一个字符串的长度,返回值为该字符串的长度。其语法如下:

#include <string.h>
size_t strlen(const char *s);

其中s是要求长度的字符串,size_t是unsigned int的等价类型。

下面是一个例子:

#include<stdio.h>
#include<string.h>

int main()
{
    char str[] = "hello world!";
    int len;
    len = strlen(str);
    printf("String length: %d
", len);
    return 0;
}

输出为:

String length: 12

2. strcpy(复制字符串)

strcpy函数用于将一个字符串复制到另一个字符串数组中,其语法如下:

#include <string.h>
char *strcpy(char *dest, const char *src);

其中dest是目标字符串,src是要复制的源字符串。

下面是一个例子:

#include<stdio.h>
#include<string.h>

int main()
{
   char source[] = "hello world!";
   char dest[50];
   strcpy(dest, source);
   printf("The copy of the string is : %s
", dest);
   return 0;
}

输出为:

The copy of the string is : hello world!

3. strcat(连接字符串)

strcat函数用于将字符串2连接到字符串1的末尾,其语法如下:

#include <string.h>
char *strcat(char *dest, const char *src);

其中dest是目标字符串,src是要连接的源字符串。

下面是一个例子:

#include<stdio.h>
#include<string.h>

int main()
{
   char source[] = " world!";
   char dest[20] = "hello";
   strcat(dest, source);
   printf("The concatenation of two strings is : %s
", dest);
   return 0;
}

输出为:

The concatenation of two strings is : hello world!

4. strncat(连接指定长度的字符串)

strncat函数与strcat函数相似,但它可以指定连接的长度,其语法如下:

#include <string.h>
char *strncat(char *dest, const char *src, size_t n);

其中dest是目标字符串,src是要连接的源字符串,n是连接的长度。

下面是一个例子:

#include<stdio.h>
#include<string.h>

int main()
{
   char source[] = " world!";
   char dest[20] = "hello";
   strncat(dest, source, 3);
   printf("The concatenation of two strings is : %s
", dest);
   return 0;
}

输出为:

The concatenation of two strings is : hello wo

5. strcmp(比较字符串)

strcmp函数用于比较两个字符串,其语法如下:

#include <string.h>
int strcmp(const char *s1, const char *s2);

其中s1和s2是要比较的字符串。

如果s1等于s2,返回0;如果s1小于s2,返回负数;如果s1大于s2,返回正数。

下面是一个例子:

#include<stdio.h>
#include<string.h>

int main()
{
   char str1[20] = "hello";
   char str2[20] = "world!";
   int result;
   result = strcmp(str1, str2);
   printf("The result is : %d
", result);
   return 0;
}

输出为:

The result is : -15

6. strstr(寻找子串)

strstr函数用于在源串中寻找指定子串的位置,其语法如下:

#include <string.h>
char *strstr(const char *haystack, const char *needle);

其中haystack是要查找子串的字符串,needle是要查找的子串。

如果找到了子串,则返回 次出现的位置的指针;否则,返回NULL。

下面是一个例子:

#include<stdio.h>
#include<string.h>

int main()
{
   char str[20] = "hello world!";
   char s[10] = "world";
   char *p;
   p = strstr(str, s);
   printf("The substring is : %s
", p);
   return 0;
}

输出为:

The substring is : world!

7. strchr(查找指定字符)

strchr函数用于在字符串中查找指定字符,其语法如下:

#include <string.h>
char *strchr(const char *str, int c);

其中str是要查找的字符串,c是要查找的字符。

如果找到了字符c,则返回该字符在字符串中 次出现的位置的指针;否则,返回NULL。

下面是一个例子:

#include<stdio.h>
#include<string.h>

int main()
{
   char str[20] = "hello world!";
   char *p;
   p = strchr(str, 'w');
   printf("The first occurrence of character w in the string is at position: %d
", p-str+1);
   return 0;
}

输出为:

The first occurrence of character w in the string is at position: 7

8. sprintf(格式化输出到字符串)

sprintf函数用于将格式化的输出写入字符串,其语法如下:

#include <stdio.h>
#include <stdarg.h>
int sprintf(char *str, const char *format, ...);

其中str是要写入的字符串,format是格式化的输出字符串。

下面是一个例子:

#include<stdio.h>
#include<string.h>

int main()
{
   int i = 55;
   double d = 12.345;
   char str[100];
   sprintf(str, "Integer value: %d, floating point value: %lf", i, d);
   printf("The formatted string: %s
", str);
   return 0;
}

输出为:

The formatted string: Integer value: 55, floating point value: 12.345000

9. strtok(分割字符串)

strtok函数用于按照指定的分隔符对字符串进行分割,其语法如下:

#include <string.h>
char *strtok(char *str, const char *delim);

其中str是要分割的字符串,delim是分隔符字符串。

次调用strtok时,需要传递要分割的字符串;以后每次调用时,传递NULL。

下面是一个例子:

#include<stdio.h>
#include<string.h>

int main()
{
   char str[80] = "hello world, my name is John.";
   const char s[2] = " ";
   char *token;
   token = strtok(str, s);
   while(token != NULL)
   {
      printf("%s
", token);
      token = strtok(NULL, s);
   }
   return 0;
}

输出为:

hello
world,
my
name
is
John.

总结:

以上是常用的字符串操作函数及其用法,这些函数是文本处理中必不可少的重要函数。在实际应用中,我们可以根据需求选择合适的函数来进行字符串操作,从而简化开发流程并提高工作效率。