自己写的实现分割的c的函数。

// test_C_split.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string.h>
int GetSplitItem( char *strInText, int intInSplitComand, int intInItemLen, char *strOutGetItem );
int _tmain(int argc, _TCHAR* argv[])
{
//================ローカル============
char strText[256];
char strItem[256];
int  intRet = 0;
memset(strText,0x00,sizeof(strText));
memset(strItem,0x00,sizeof(strItem));
//===============test case 1======================================
printf("*************** Test Case 1 ***********************/n");
strcpy(strText,"1,2,3,4,5,6,7,8,9,10");
//================6============
GetSplitItem( strText,6,1,strItem );
printf("位置=[6]--DATA=[%s]/n",strItem);
//================1============
GetSplitItem( strText,1,1,strItem );
printf("位置=[1]--DATA=[%s]/n",strItem);
//================10============
GetSplitItem( strText,10,1,strItem );
printf("位置=[10]--DATA=[%s]/n",strItem);
//===============test case 2======================================
printf("*************** Test Case 2 ***********************/n");
memset(strText,0x00,sizeof(strText));
memset(strItem,0x00,sizeof(strItem));
//----------------1---2----3------4---------5-----6--------7--------8-----9----10----11--12---------13---14-15-
strcpy(strText,"adsa,2333,核か,あdsふぁ,陳鉄,あdsふぁ,そらん,課か加,日本,ちゃr,CHR,あdsふぁ,23,234,ads");
//================1============
memset(strItem,0x00,sizeof(strItem));
GetSplitItem( strText,1,20,strItem );
printf("位置=[1]--DATA=[%s]/n",strItem);
//================6============
memset(strItem,0x00,sizeof(strItem));
GetSplitItem( strText,6,20,strItem );
printf("位置=[6]--DATA=[%s]/n",strItem);
//================10============
memset(strItem,0x00,sizeof(strItem));
GetSplitItem( strText,10,20,strItem );
printf("位置=[10]--DATA=[%s]/n",strItem);
//================15============
memset(strItem,0x00,sizeof(strItem));
GetSplitItem( strText,15,20,strItem );
printf("位置=[15]--DATA=[%s]/n",strItem);
//===============test case 3======================================
printf("*************** Test Case 3 ***********************/n");
memset(strText,0x00,sizeof(strText));
memset(strItem,0x00,sizeof(strItem));
strcpy(strText,",,,,,,,,,,,,,,");
//================1============
memset(strItem,0x00,sizeof(strItem));
GetSplitItem( strText,1,20,strItem );
printf("位置=[1]--DATA=[%s]/n",strItem);
//================6============
memset(strItem,0x00,sizeof(strItem));
GetSplitItem( strText,6,20,strItem );
printf("位置=[6]--DATA=[%s]/n",strItem);
//================10============
memset(strItem,0x00,sizeof(strItem));
GetSplitItem( strText,10,20,strItem );
printf("位置=[10]--DATA=[%s]/n",strItem);
//================15============
memset(strItem,0x00,sizeof(strItem));
GetSplitItem( strText,15,20,strItem );
printf("位置=[15]--DATA=[%s]/n",strItem);
//===============test case 4======================================
printf("*************** Test Case 4 ***********************/n");
memset(strText,0x00,sizeof(strText));
memset(strItem,0x00,sizeof(strItem));
//----------------1---2----3------4---------5-----6--------7--------8-----9-10----11--12-----13---14-15-
strcpy(strText,"adsa,2333,核か,あdsふぁ,陳鉄,あdsふぁ,そらん,課か加,日本,,CHR,あdsふぁ,23,234,");
//================1============
memset(strItem,0x00,sizeof(strItem));
intRet = GetSplitItem( strText,1,2,strItem );
printf("位置=[1]--DATA=[%s] --- 戻り値=[%d]/n",strItem,intRet);
//================6============
memset(strItem,0x00,sizeof(strItem));
GetSplitItem( strText,6,20,strItem );
printf("位置=[6]--DATA=[%s]/n",strItem);
//================10============
memset(strItem,0x00,sizeof(strItem));
GetSplitItem( strText,10,20,strItem );
printf("位置=[10]--DATA=[%s]/n",strItem);
//================15============
memset(strItem,0x00,sizeof(strItem));
GetSplitItem( strText,15,20,strItem );
printf("位置=[15]--DATA=[%s]/n",strItem);
getchar();
return 0;
}
//====================================================
//--SPLIT関数---------------------------------
// ----strInText---------振分の文字列---------
// ----intInSplitComand--項目の位置(個数)---
// ----intInItemLen------項目の長さ-----------
// ----strOutGetItem-----取得の項目-----------
//====================================================
int GetSplitItem( char *strInText, int intInSplitComand, int intInItemLen, char *strOutGetItem ) {
//================ローカル変数================
int  intBegainPlace =0;            //開始位置
int  intEndPlace = 0;              //終了位置
int  intSplitCommandCnt = 0;       //区分文字の集計値
char *pStr;                        //臨時変数
pStr = strInText;
    //================開始位置を取得する==========
if (  intInSplitComand == 1 ) {
intBegainPlace = 0;
}
else {
while ( *pStr != '/0' ) {
//--開始位置を集計--
intBegainPlace++;
//--区分文字を集計する--
if ( *pStr == ',' ) {
intSplitCommandCnt++;
}
        
//--取得項目の位置の場合--
if ( intSplitCommandCnt == (intInSplitComand - 1) ) {
break;
}
*pStr++;
}
}
//============区分文字の集計値をクリア========
intSplitCommandCnt = 0;
pStr = strInText;
//================終了位置を取得する==========
while ( *pStr != '/0' ) {
//--終了位置を集計--
intEndPlace++;
//--区分文字を集計する--
if ( *pStr == ',' ) {
    intSplitCommandCnt++;
}
        
//--取得項目の位置の場合--
if ( intSplitCommandCnt == intInSplitComand ) {
    break;
}
*pStr++;
}
//================項目が最後の位置のチェック、最後の位置の場合、集計しない
if ( intEndPlace != strlen(strInText) ) {
intEndPlace = intEndPlace - 1;
}
//==============実際取得項目の長さ>予定取得項目の長さの場合、エラー======
if ( (intEndPlace - intBegainPlace) > intInItemLen ) {
return -1;
}
//============終了位置と開始位置間、空白の場合=======
if ( intEndPlace-intBegainPlace == 0 ) {
    memset(strOutGetItem,0x00,sizeof(strOutGetItem));
return 0;
}
//==============取得した項目を格納===========
memcpy( strOutGetItem, strInText + intBegainPlace, intEndPlace-intBegainPlace );
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值