各种Split功能演示

it2024-08-13  66

#include "stdafx.h"

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

using namespace std;

int SplitCount(const char *src, const char *split);int Split(char *src, const char *split, char **dst, int count);

void TestSplit1();void TestSplit2();void TestSplit3();

int _tmain(int argc, _TCHAR* argv[]){ TestSplit3(); return 0;}

void TestSplit3(){ wchar_t s[] = L"123-sldkf-123ls-343434-dfjdlkfj-dflcmvn"; wchar_t *delim = L"-"; wchar_t *p;

p = wcstok(s, delim); while (p != NULL) { printf("%ws\n", p); p = wcstok(NULL, delim); }}

void TestSplit2(){ char s[] = "123-sldkf-123ls-343434-dfjdlkfj-dflcmvn"; char *delim = "-"; char *p;

p = strtok(s, delim); while (p != NULL) { printf("%s\n", p); p = strtok(NULL, delim); }}

void TestSplit1(){ char s[] = "123-sldkf-123ls-343434-dfjdlkfj-dflcmvn"; char *delim = "-"; char **p; int n = 0; string str[6];

n = SplitCount(s, delim); p = (char **)malloc(256);

Split(s, delim, p, n); for (int i = 0; i < n; i++) { str[i] = p[i]; }

free(p);}

int SplitCount(const char *src, const char *split){ int n = 0; int len; const char *p;

len = strlen(split); p = src; if (*p != '\0') { n++; while (*p != '\0') { // Compare bytes if (memcmp(p, split, len) == 0) { p += len; if (*p == '\0') break; // n++; } else { // Next char p++; } } }

return n;

}

int Split(char *src, const char *split, char **dst, int count){ int n = 0; int len; char *p;

len = strlen(split); p = src; if (*p != '\0' && n < count) { dst[n++] = p; while (*p != '\0' && n < count) { // Compare bytes if (memcmp(p, split, len) == 0) { // Fill memset(p, 0, len); p += len; if (*p == '\0' || n >= count) break; // dst[n++] = p; } else { // Next char p++; } } }

return n;

}

转载于:https://www.cnblogs.com/Floki/p/7451758.html

最新回复(0)