split函数的实现

split作为字符串分割函数非常有用,但在C++里面没有这个函数。自己实现一个分割函数:

1、遇到多个分隔符连在一起,则不做分割

2、()内的分隔符不起作用

3、如果只有(,没有)不影响分隔符

#include <iostream>
#include <cstring>

using namespace std;
void splitString(const char* aString, char aSeperator)
{
   if(NULL == aString)
       return;

   const char* start = aString;
   const char* end = aString;
   int len = strlen(aString);
   bool isCprocessing = false;

   while(*start != ‘\0‘)
   {
       while(*end != ‘\0‘ && *end != aSeperator && *end != ‘(‘)
       {
           ++end;
       }//end while

       // ‘(‘和‘)‘配对

       if(!isCprocessing && ‘(‘ == *start)
       {
           ++end;
           while(*end != ‘\0‘ && *end != ‘)‘)
           {
               ++end;
           }
           if(‘\0‘ == *end)
               end = start + 1;

       }

       if(‘(‘ == *start)
           isCprocessing = true;

       if(‘(‘ == *end)
       {
           const char* temp = end++;    //先保存这个位置,在括号不配对时,记得将end置位
           while(*end != ‘\0‘ &&  *end != ‘)‘)
           {
               ++end;
           }//end while
           if(‘\0‘ == *end)
               end = temp;
       }

       if(‘\0‘ == *end)             //none aSeperator
       {
           cout << start << endl;
           break;
       }

       //剩下最后一种,遇到分隔符,此时统计分隔符的个数
       char* tem = new char[len + 1];
       char* temStart = tem;

        while(aSeperator == *end )
        {
            *tem++ = *end++;     //end指向分隔符的下一项
        }
        *tem = ‘\0‘;
        if(strlen(temStart) == 1)
        {
            while(start != end - 1 && *start != ‘\0‘)
                cout << *start++;
            cout << endl;
            start = end;
            delete tem;
        }
        else
        {
            delete tem;
        }

   }//end while

}

int main()
{
    char str[100];
    memset(str, 0 , 100);
    cout << "please input str: " << endl;
    cin >> str;

    char ch = ‘+‘;
    splitString(str, ch);
    cout << "Hello World!" << endl;
    return 0;
}
时间: 2024-08-01 22:41:54

split函数的实现的相关文章

split函数

Python中有split()和os.path.split()两个函数,具体作用如下:split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表(list)os.path.split():按照路径将文件名和路径分割开 一.函数说明1.split()函数 语法:str.split(str="",num=string.count(str))[n] 参数说明:str:   表示为分隔符,默认为空格,但是不能为空('').若字符串中没有分隔符,则把整个字符串作为列表的

python中join和split函数

一个是分割,一个是连接. 惯例,先看内部帮助文档 Help on method_descriptor: join(...) S.join(iterable) -> string Return a string which is the concatenation of the strings in the iterable. The separator between elements is S. (END) 将可迭代对象(包含的应该是str类型的,不然会报错)连接起来, 返回值是str,用法如

perl:split函数用法

本文和大家重点讨论一下Perl split函数的用法,Perl中的一个非常有用的函数是Perl split函数-把字符串进行分割并把分割后的结果放入数组中.这个Perl split函数使用规则表达式(RE),如果未特定则工作在$_变量上. Perl split函数 Perl中的一个非常有用的函数是Perl split函数-把字符串进行分割并把分割后的结果放入数组中.这个Perl split函数使用规则表达式(RE),如果未特定则工作在$_变量上. Perl split函数可以这样使用: $inf

SQL中实现SPLIT函数几种方法

例1 代码如下 复制代码 create function f_split(@SourceSql varchar(8000),@StrSeprate varchar(10))returns @temp table(a varchar(100))--实现split功能 的函数--date :2003-10-14as begin declare @i int set @SourceSql=rtrim(ltrim(@SourceSql)) set @i=charindex(@StrSeprate,@So

awk中split函数的用法

The awk function split(s,a,sep) splits a string s into an awk array a using the delimiter sep. time=12:34:56 echo $time | awk '{split($0,a,":" ); print a[1]}' 12   echo $time | awk '{split($0,a,":" ); print a[3]}' 34   echo $time | awk

python 中join()函数strip() 函数和 split() 函数的详解及实例

1.join()函数 Python中有join()和os.path.join()两个函数,具体作用如下: join():                连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符)连接生成一个新的字符串 语法:  'sep'.join(seq) 参数说明sep:分隔符.可以为空seq:要连接的元素序列.字符串.元组.字典上面的语法即:以sep作为分隔符,将seq所有的元素合并成一个新的字符串 返回值:返回一个以分隔符sep连接各个元素后生成的字符串 os.p

Python中的split()函数的使用方法

函数:split() Python中有split()和os.path.split()两个函数,具体作用如下:split():拆分字符串.通过指定分隔符对字符串进行切片,并返回分割后的字符串列表(list)os.path.split():按照路径将文件名和路径分割开 一.函数说明1.split()函数语法:str.split(str="",num=string.count(str))[n] 参数说明:str:   表示为分隔符,默认为空格,但是不能为空('').若字符串中没有分隔符,则把

oracle实现split函数功能

转载: http://blog.csdn.net/jojo52013145/article/details/6758279在实际的应用中,为了让PL/SQL 函数返回数据的多个行,必须通过返回一个 REF CURSOR 或一个数据集合来完成.REF CURSOR 的这种情况局限于可以从查询中选择的数据,而整个集合在可以返回前,必须进行具体化. 9i 通过引入的管道化表函数纠正了后一种情况.表函数是返回整个行的集(通常作为一个集合)的函数,可以直接从 SQL 语句中进行查询,就好像它是一个真正的数

Java——String.split()函数

在java doc里有 String[] java.lang.String.split(String regex) Splits this string around matches of the given regular expression. This method works as if by invoking the two-argument split method with the given expression and a limit argument of zero. Tra

Delphi中怎样将字符串按给定字符分隔(类似split函数的功能)

Delphi中怎样将字符串按给定字符分隔(类似split函数的功能) 分类:            Delphi2007-05-16 11:094911人阅读评论(2)收藏举报 delphiintegerstringbutton文本编辑function 今天偶尔要做的Delphi程序,其中涉及到了字符串处理,里面有一个功能类似于VB里的split()函数的功能,于是查了很久才查到些资料,现将这些资料整理一下,方便大家. 首先是一个网友自己编的函数.实现了和split()函数的功能. unit U