CLX C++ Libraries
Home >> cstring

Declaration

size_t strlen(const CharT* str);
int strcmp(const CharT* s1, const CharT* s2);
int strncmp(const CharT* s1, const CharT* s2, size_t n);
int strcoll(const CharT* s1, const CharT* s2);
CharT* strcpy(CharT* dest, const CharT* src);
CharT* strncpy(CharT* dest, const CharT* src, size_t n);
CharT* strcat(CharT* dest, const CharT* src);
CharT* strncat(CharT* dest, const CharT* src, size_t n);
const CharT* strchr(const CharT* str, CharT c);
CharT* strchr(CharT* str, CharT c);
const CharT* strrchr(const CharT* str, CharT c);
CharT* strrchr(CharT* str, CharT c);
const CharT* strstr(const CharT* str, const CharT* substr);
CharT* strstr(CharT* str, const CharT* substr);
size_t strspn(const CharT* str, const CharT* spanset);
size_t strcspn(const CharT* src, const CharT* spanset);
const CharT* strpbrk(const CharT* str, const CharT* spanset);
CharT* strpbrk(CharT* str, const CharT* spanset);
long int wcstol(const CharT* str, CharT** end, int base);
unsigned long int strtoul(const CharT* str, CharT** end, int base);
double strtod(const CharT* str, CharT** end);
size_t strftime(CharT* str, size_t n, const CharT* fmt, const tm* tmptr);
size_t strxfrm(CharT* dest, const CharT* src, size_t n);

Overview

cstring は,C の文字列操作関数を char/wchar_t に関わらず同じ関数名で呼ぶためのラッパ関数群です. 引数として char 型の変数が指定された場合は std::strxxx を呼び,wchar_t 型の変数が指定された場合は std::wcsxxx を呼びます.尚,std::strtok() と std::wcstok() は関数の仕様が異なるため,strtok() は定義していません.

Example

example_cstring.cpp

#include <iostream>
#include "clx/cstring.h"

int main(int argc, char* argv[]) {
    char s1[256] = "Hello, world!";
    char s2[256] = "Hello, world.";
    char s3[256] = "3.1415926";
    
    std::cout << "string: " << s1 << std::endl;
    if (clx::strcmp(s1, s2) == 0) std::cout << "strcmp: match" << std::endl;
    else std::cout << "strcmp: unmatch" << std::endl;
    
    std::cout << "strlen: " << clx::strlen(s1) << std::endl;
    
    clx::strcat(s1, s2);
    std::cout << "strcat: " << s1 << std::endl;
    
    char* last = NULL;
    double pi = clx::strtod(s3, &last);
    std::cout << "strtod: " << pi << std::endl;
    
    return 0;
}
Result
string: Hello, world!
strcmp: unmatch
strlen: 13
strcat: Hello, world!Hello, world.
strtod: 3.14159

example_wcstring.cpp

#ifndef CLX_USE_WCHAR
#define CLX_USE_WCHAR
#endif

#include <iostream>
#include "clx/cstring.h"

int main(int argc, char* argv[]) {
    std::wcout.imbue(std::locale("japanese"));
    
    wchar_t s1[256] = L"こんにちは、こんにちは。";
    wchar_t s2[256] = L"こんにちは!こんにちは!";
    wchar_t s3[256] = L"3.1415926";
    
    std::wcout << L"string: " << s1 << std::endl;
    if (clx::strcmp(s1, s2) == 0) std::wcout << L"strcmp: match" << std::endl;
    else std::wcout << L"strcmp: unmatch" << std::endl;
    
    std::wcout << L"strlen: " << clx::strlen(s1) << std::endl;
    
    clx::strcat(s1, s2);
    std::wcout << L"strcat: " << s1 << std::endl;
    
    wchar_t* last = NULL;
    double pi = clx::strtod(s3, &last);
    std::wcout << L"strtod: " << pi << std::endl;
    
    return 0;
}
Result
string: こんにちは、こんにちは。
strcmp: unmatch
strlen: 12
strcat: こんにちは、こんにちは。こんにちは!こんにちは!
strtod: 3.14159