C语言实现字符串切片

    因要用C语言实现读取配置文件,配置文件中字符串以逗号分隔,所以要根据索引获取到相应位置的字符串,也要能根据字符串获取在一串字符中的索引,代码记录如下:

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

int findSubstringIndexSplitByComma(const char *pSrc, const char *pDst); 
int getSubstringByIndexSplitByComma(const char *pSrcString,const int index,char *pSubString,const int pSubStringSize);

void main(void)
{
    char *p_src_string = "S1,S2,S3,S4,S5,S6,S7";
    char *p_need_find_string = "S6";
    char sub_string[10] = {};
    int index = 0;
    
    index = findSubstringIndexSplitByComma(p_src_string,p_need_find_string);
    if(-1 != index)
    {
        printf("The index of SubString[%s] in SrcString[%s] is [%d]\n",p_need_find_string,p_src_string,index);
    }
    
    if(-1 != getSubstringByIndexSplitByComma(p_src_string,index,sub_string,sizeof(sub_string)))
    {
        printf("Get sub string from SrcString[%s] by index[%d]:SubString--[%s]\n",p_src_string,index,sub_string);        
    }
    else
    {
        printf("Can not find SubString in [%s] by index[%d]\n",p_src_string,index);
    }

}
//if can not find substring in srcString,return -1
//if can find,return the num of comma before the substring
//for example:
//srcstring S1,S2,S3  subString:S2,return 1
//srcstring S1,S2,S3  subString:S4,return -1
int findSubstringIndexSplitByComma(const char *pSrc, const char *pDst)  
{  
    int i = 0;
    int j = 0; 
    int comma_cnt = 0;
    
    for (i=0; pSrc[i]!='\0'; i++)  
    {
        if(',' == pSrc[i])
        {
            comma_cnt++;
        }
        
        if(pSrc[i]!=pDst[0])  
            continue;         
        
        j = 0;  
        while(pDst[j]!='\0' && pSrc[i+j]!='\0')  
        {  
            j++;  
            if(pDst[j]!=pSrc[i+j]) 
            {
                break;    
            }  
        }  
        if(pDst[j]=='\0')  
            return comma_cnt;  
    }  
    return -1;  
}

//if can not find substring by index in srcstring,return -1
//if can find,return 0,and cpy this subString to pSubString,so you need ensure the pSubString has enough space to save the substring
//srcString ",,,",if index = 1,substring is empty,if the substring space is not enough,cut short the subString in srcString
//pay attention to the index = 0,if the index is 0,find_index = 0
int getSubstringByIndexSplitByComma(const char *pSrcString,const int index,char *pSubString,const int pSubStringSize)
{
    int comma_cnt = 0;
    int i = 0;
    int j = 0;
    int find_index = 0;

    //get srcString comma cnt,if equal the index,start copy to the pSubString
    for(i = 0;pSrcString[i] != '\0';i++)
    {
        if(comma_cnt == index)
        {
            break;
        }
        if(',' == pSrcString[i])
        {
            comma_cnt++;
        }
    }
    //can not find the index of comma_cnt,just return 
    if('\0' == pSrcString[i])
    {
        return -1;
    }
    //find 
    find_index = i;
    for(j = 0;('\0' != pSrcString[j + find_index]);j++)
    {
        if(j < pSubStringSize)
        {
            if(',' == pSrcString[j + find_index])
            {
                break;
            }
            else
            {
                pSubString[j] = pSrcString[j + find_index];
            }            
        }
        else
        {
            printf("The subString space is not enough\n");
            break;
        }
    }
    pSubString[j] = '\0';
    
    return 0;
}

    运行结果如下:

[root@localhost test]# gcc test.c -o test
[root@localhost test]# ./test 
The index of SubString[S6] in SrcString[S1,S2,S3,S4,S5,S6,S7] is [5]
Get sub string from SrcString[S1,S2,S3,S4,S5,S6,S7] by index[5]:SubString--[S6]

    如果大家有更好的实现方法,请多多指教。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值