C 标准库 - string.h之strspn使用

strspn

  • Returns the length of the initial portion of str1 which consists only of characters that are part of str2.
  • The search does not include the terminating null-characters of either strings, but ends there.
  • 检索字符串 dest 中第一个不在字符串 src 中出现的字符下标。返回 dest 所指向的空终止字节串的最大起始段( span )长度,段仅由 src 所指向的空终止字节字符串中找到的字符组成。
  • 若 dest 或 src 不是指向空终止字节字符串的指针,则行为未定义。
size_t strspn( const char *dest, const char *src );

Parameters

dest

  • C string to be scanned.
  • 指向要分析的空终止字节字符串的指针

src

  • C string containing the characters to match.
  • 指向含有要搜索的字符的空终止字节字符串的指针

Return value

  • The length of the initial portion of str1 containing only characters that appear in str2.
    Therefore, if all of the characters in str1 are in str2, the function returns the length of the entire str1 string, and if the first character in str1 is not in str2, the function returns zero.
    size_t is an unsigned integral type.
  • 仅由来自 src 所指向的空终止字节字符串的字符组成的最大起始段长度。
  • 该函数返回 dest 中第一个不在字符串 src 中出现的字符下标。

Example

//
// Created by zhangrongxiang on 2018/2/5 17:28
// File strspn
//

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

//C 库函数 size_t strspn(const char *str1, const char *str2) 检索字符串 str1 中第一个不在字符串 str2 中出现的字符下标。

int main() {
    size_t len;
    const char str1[] = "ABCDEFG02018ABCDEFG02018";
    const char str2[] = "ABCD";
    const char str3[] = "2018";
    const char str4[] = "AB";
    const char str5[] = "AC";
    const char str6[] = "aC";
    len = strspn(str1, str2);
    printf("%d\n", (unsigned int) len); //4
    len = strspn(str1, str3);
    printf("%d\n", (unsigned int) len); //0
    len = strspn(str1, str4);
    printf("%d\n", (unsigned int) len); //2
    len = strspn(str1, str5);
    printf("%d\n", (unsigned int) len); //1
    len = strspn(str1, str6);
    printf("%d\n", (unsigned int) len); //0

    char strtext[] = "129th";
    char cset[] = "1234567890";

    len = strspn(strtext, cset);
    printf("The initial number has %d digits.\n", (int) len);//3

    const char *string = "abcde312$#@";
    const char *low_alpha = "qwertyuiopasdfghjklzxcvbnm";

    len = strspn(string, low_alpha);
    printf("%d\n",(int)len);//5
    //After skipping initial lowercase letters from 'abcde312$#@'
    printf("After skipping initial lowercase letters from '%s'\n"
               "The remainder is '%s'\n", string, string + len);//The remainder is '312$#@'

    return (0);
}

参考文章

转载注明出处

原文地址:https://www.cnblogs.com/zhangrxiang/p/8419492.html

时间: 2024-11-05 20:06:48

C 标准库 - string.h之strspn使用的相关文章

C 标准库 - &lt;string.h&gt;

C 标准库 - <string.h> 简介 string .h 头文件定义了一个变量类型.一个宏和各种操作字符数组的函数. 库变量 下面是头文件 string.h 中定义的变量类型: 序号 变量 & 描述 1 size_t 这是无符号整数类型,它是 sizeof 关键字的结果. 库宏 下面是头文件 string.h 中定义的宏: 序号 宏 & 描述 1 NULL这个宏是一个空指针常量的值. 库函数 下面是头文件 string.h 中定义的函数: 序号 函数 & 描述 1

C 标准库 - string.h之strpbrk使用

strpbrk Locate characters in string,Returns a pointer to the first occurrence in str1 of any of the characters that are part of str2, or a null pointer if there are no matches. The search does not include the terminating null-characters of either str

C 标准库 - string.h之strncpy使用

strncpy 把 src 所指向的字符串复制到 dest,最多复制 n 个字符.当 src 的长度小于 n 时,dest 的剩余部分将用空字节填充. char *strncpy(char *destination, const char *source, size_t num) Parameters destination Pointer to the destination array where the content is to be copied. 指向用于存储复制内容的目标数组. s

C 标准库 - string.h之strcat使用

strcat Appends a copy of the source string to the destination string. The terminating null character in destination is overwritten by the first character of source, and a null-character is included at the end of the new string formed by the concatena

C 标准库 - string.h之strlen使用

strlen Returns the length of the C string str. The length of a C string is determined by the terminating null-character: A C string is as long as the number of characters between the beginning of the string and the terminating null character (without

C 标准库 - string.h之memchr使用

memchr Locate character in block of memory,Searches within the first num bytes of the block of memory pointed by ptr for the first occurrence of ch (interpreted as an unsigned char), and returns a pointer to it. 在参数 ptr 所指向的字符串的前 count 个字节中搜索第一次出现字符

一、Python的标准库String

一.Python的标准库String 1.查看武器 a. help(type()) name = "jane"print(help(type(name))) b. capitalize() name = "jane" print(name.capitalize()) 效果:Jane c. center() name = "jane" print(name.center(50, '-')) 效果:-----------------------jan

【C++ Primer每日刷】之三 标准库 string 类型

标准库 string 类型 string 类型支持长度可变的字符串,C++ 标准库将负责管理与存储字符相关的内存,以及提供各种有用的操作.标准库string 类型的目的就是满足对字符串的一般应用. 与其他的标准库类型一样,用户程序要使用 string 类型对象,必须包含相关头文件.如果提供了合适的 using 声明,那么编写出来的程序将会变得简短些: #include <string> using std::string; 1.1 string 对象的定义和初始化 string 标准库支持几个

标准库string类型用法(一)

标准库string类型 1. string对象的定义与初始化 string s1;                             默认构造函数,s1为空串 string s2(s1);                      将s2初始化为s1的一个副本 string s3("nwpu");            将s3初始化为一个字符串字面值副本 string s4(n, 'b');                 将s4初始化为字符‘b’的n个副本 2. string对