字符串处理的常用函数

                                          

1函数名:strcpy
功能:拷贝一个字符串到另一个字符串里面
用法:char *strcpy(char *destin, char *source);
程序例:

#include<stdio.h>
#include <string.h>
int main(){
   char string[10];
   char *str1 = "abcdefghi";

   strcpy(string,str1);
   printf("%s\n", string);
   return 0;
}

2函数名:strcat
功能:字符串拼接函数,拼接的两个数组不能是相同的。
用法:char *strcat(char *destin, char *source);
程序例:

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

int main(){
   char destination[25];
   char *blank = " ", *c = "C++", *Borland ="Borland";

   strcpy(destination,Borland);
   strcat(destination, blank);
   strcat(destination, c);

   printf("%s\n",destination);
   return 0;
}

3函数名:strchr
功能:在一个串中查找给定字符的第一个匹配之处
用法:char *strchr(char *str, char c);
程序例:

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

int  main(void){
    char string[15];
    char *ptr, c = 'r';

    strcpy(string,"This is a string");
    ptr= strchr(string, c);
    if (ptr)
       printf("The character %c is at position: %d\n", c,ptr-string);
    else
       printf("The character was not found\n");
   return 0;
}

4函数名:strcmp
功能:串比较
用法:int strcmp(char *str1, char *str2);
看Asic码,str1>str2,返回值>0;两串相等,返回0
程序例:

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

int  main(void){
     char *buf1 = "aaa",  *buf2 = "bbb", *buf3 = "ccc";
     int ptr;

    ptr= strcmp(buf2, buf1);
    if (ptr > 0)
       printf("buffer2 is greater than buffer 1\n");
    else
       printf("buffer 2 is less than buffer 1\n");

    ptr= strcmp(buf2, buf3);
    if (ptr > 0)
       printf("buffer 2 is greater than buffer 3\n");
    else
       printf("buffer 2 isless than buffer 3\n");

    return 0;
}


5函数名:strncmp
功能:比较字符串的前maxlen个字符

形式:int
strncmp(char *str1, char *str2, int maxlen)
程序例:

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

int main(void){
     char *buf1 = "aaabbb", *buf2 = "bbbccc",*buf3 = "ccc";
     int ptr;

    ptr= strncmp(buf2,buf1,3);
    if (ptr > 0)
       printf("buffer 2 is greaterthan buffer 1\n");
   else
      printf("buffer 2 is less than buffer 1\n");

   ptr= strncmp(buf2,buf3,3);
   if (ptr > 0)
      printf("buffer 2 is greaterthan buffer 3\n");
   else
     printf("buffer 2 is less than buffer 3\n");

   return(0);
}

6函数名:strncpy
功能:串拷贝,将第二个字符串前maxlen个字符拷贝给第一个字符串
用法:char *strncpy(char *destin, char *source, int maxlen);
程序例:

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

int main(void){
     char string[10];
     char *str1 = "abcdefghi";

    strncpy(string,str1, 3); //只复制三个字符
    string[3] = '\0';
   printf("%s\n", string);
   return 0;
}

 7函数名:strrchr
功能:在串中查找指定字符的最后一个出现
用法:char *strrchr(char *str, char c);
程序例:

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

int main(void){
   char string[15];
   char *ptr, c = 'r';

   strcpy(string,"This is a string");
   ptr = strrchr(string, c);
   if (ptr)
      printf("The character %c is at position: %d\n", c,ptr-string);
   else
      printf("The character was not found\n");
  return 0;
}

8 C/C++中的Split函数1C/C++中的Split函数是strtok()其函数原型如下:char
* strtok (char * str, const char * delimiters);
函数说明
strtok()用来将字符串分割成一个个片段。参数str指向欲分割的字符串,参数delimiters则为分割字符串,当strtok()在参数str的字符串中发现到参数delimiters的分割字符时则会将该字符改为'\0'字符。在第一次调用时,strtok()必需给予参数str字符串,往后的调用则将参数str设置成NULL。每次调用成功则返回下一个分割后的字符串指针。

#include<stdio.h>
#include <string.h>
int main (){
   char str[] ="a,b,c,d*e";
   const char *split = ",";
   char * p;
   p = strtok(str,split);
   while(p!=NULL) {
        printf("%s\n",p);
        p = strtok(NULL,split);
   }
   getchar();
   return 0;
}
/*
本例中,实现对字符串'a,b,c,d*e"用逗号(,)来作界定符对字符串进行分割。
输出结果将如下所示:
a
b
c
d*e

因为delimiters支持多个分割符,我们将本示例中的语句行
constchar * split = ",";
改成constchar * split = ",*";//用逗号(,)和星号(*)对字符串进行分割

这样输出结果将如下所示:
a
b
c
d
e
*/
时间: 2024-08-01 05:51:42

字符串处理的常用函数的相关文章

javascript字符串的连接常用函数与连接实例

var str="hello"; str+="world"; 实际上,这段代码在幕后执行的步骤如下: (1) 创建存储"hello"的字符串. (2) 创建存储"world"的字符串. (3) 创建存储连接结果的字符串. (4) 把str的当前内容复制到结果中. (5) 把"world"复制到结果中. (6) 更新str,使它指向结果. 每次完成字符串连接都会执行步骤2到6,使得这种操作非常消耗资源.如果重

PHP第八课 字符串拆分常用函数

课程概要: 通过这节课能够对字符串进行基本的操作. 字符串知识点: 1.字符串的处理介绍 2.常用的字符串输出函数 3.常用的字符串格式化函数 4.字符串比较函数 5.正则表达式在字符串中的应用 6.与per1兼用的正则表达式 1.pathinfo();//返回域名的path信息 2.parse_url(); 3.parse_str();//用来拆分参数用的 pathinfo(); <?php $str="http://blog.csdn.net/junzaivip"; $arr

C语言中字符串常用函数strcat与strcpy的用法介绍

以下是对C语言中字符串常用函数strcat与strcpy的使用方法进行了详细的分析介绍,需要的朋友可以参考下   strcpy原型声明:extern char *strcpy(char* dest, const char *src);头文件:#include <string.h>功能:把从src地址开始且含有NULL结束符的字符串复制到以dest开始的地址空间说明:src和dest所指内存区域不可以重叠且dest必须有足够的空间来容纳src的字符串. 返回指向dest的指针.函数实现: 复制代

mysql中字符串截取常用函数

mysql教程中字符串截取常用函数 mysql 字符串截取函数: left()返回字符串str的最左面len个字符, right()返回字符串str的最右面len个字符, substring()从字符串str的起始位置pos返回一个子串, substring_index().回从字符串str的第count个出现的分隔符delim之后的子串.如果count是正数,返回最后的分隔符到左边(从左边数) 的所有字符.如果count是负数,返回最后的分隔符到右边的所有字符(从右边数). 还有 mid(st

ThinkPHP字符串函数及常用函数汇总_php实例

本文汇总了ThinkPHP的字符串处理函数及其他一些常用函数,可供开发人员参考使用.详情如下: get_client_ip() 获取客户端的IP地址 msubstr($str, $start=0, $length, $charset="utf-8″, $suffix=true) $str:要截取的字符串 $start=0:开始位置,默认从0开始 $length:截取长度 $charset="utf-8″:字符编码,默认UTF-8 $suffix=true:是否在截取后的字符后面显示省略

Python字符串和文件操作常用函数分析_python

本文实例分析了Python字符串和文件操作常用函数.分享给大家供大家参考.具体如下: # -*- coding: UTF-8 -*- ''' Created on 2010-12-27 @author: sumory ''' import itertools def a_containsAnyOf_b(seq,aset): '''判断seq中是否含有aset里的一个或者多个项 seq可以是字符串或者列表 aset应该是字符串或者列表''' for item in itertools.ifilte

基于php常用函数总结(数组,字符串,时间,文件操作)

数组:[重点1]implode(分隔,arr) 把数组值数据按指定字符连接起来 例如: $arr=array('1','2','3','4'); $str=implode('-',$arr); explode([分隔],arr)按指定规则对一个字符串进行分割,返回值为数组 别名join array_merge()合并一个或多个数组 array_combine(array keys, array values) 创建一个数组,用一个数组的值作为其键名,另一个数组的值作为其值 例如: $a = ar

asp+XMLHTTP组件做采集常用函数收集

asp+|xml|采集|函数 asp+XMLHTTP组件做采集常用函数其中的html代码处理函数很管用,写得相当漂亮 <%'=================================================='函数名:GetHttpPage'作 用:获取网页源码'参 数:HttpUrl ------网页地址'==================================================Function GetHttpPage(HttpUrl)If IsNul

JavaScript常用函数列表

JavaScript常用函数列表,方便自己查询. click()   对象.click()   使对象被点击.     closed   对象.closed   对象窗口是否已关闭true/false     clearTimeout(对象)   清除已设置的setTimeout对象     clearInterval(对象)   清除已设置的setInterval对象     confirm("提示信息")   弹出确认框,确定返回true取消返回false     cursor:样