惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
O
OpenAI News
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
Webroot Blog
Webroot Blog
GbyAI
GbyAI
S
SegmentFault 最新的问题
Cyberwarzone
Cyberwarzone
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
J
Java Code Geeks
Google DeepMind News
Google DeepMind News
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
博客园 - 【当耐特】
S
Secure Thoughts
酷 壳 – CoolShell
酷 壳 – CoolShell
AWS News Blog
AWS News Blog
Engineering at Meta
Engineering at Meta
S
Security Affairs
H
Help Net Security
Microsoft Security Blog
Microsoft Security Blog
D
DataBreaches.Net
云风的 BLOG
云风的 BLOG
Hugging Face - Blog
Hugging Face - Blog
Google DeepMind News
Google DeepMind News
Spread Privacy
Spread Privacy
T
Threatpost
Forbes - Security
Forbes - Security
C
Cisco Blogs
Scott Helme
Scott Helme
Attack and Defense Labs
Attack and Defense Labs
Simon Willison's Weblog
Simon Willison's Weblog
腾讯CDC
The Last Watchdog
The Last Watchdog
Cloudbric
Cloudbric
Last Week in AI
Last Week in AI
Recorded Future
Recorded Future
小众软件
小众软件
V
Vulnerabilities – Threatpost
美团技术团队
人人都是产品经理
人人都是产品经理
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
Hacker News - Newest:
Hacker News - Newest: "LLM"
I
Intezer
月光博客
月光博客
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园 - 司徒正美
C
Cybersecurity and Infrastructure Security Agency CISA
Martin Fowler
Martin Fowler
博客园 - 聂微东

博客园 - Kiven

50个常用SQL语句 [摘]在ASP.NET MVC中使用DropDownList SQL Server里面可能经常会用到的日期格式转换方法 [摘]Asp.net MVC 3中Session与ViewBag传值到Js中 « 给初学C++者的50条忠告 C++/CLI学习入门数组 asp.net页面刷新后样式就发生了改变 System::String^ / std::string / char * 间的转换(部分) char*与System::String^的相互转换 Windows Phone 7 常用控件之Map Windows Phone 7 常用绘图控件 [网摘]深入浅出解读微软云计算:让云触手可及 【网摘日记】云计算五层架构 [MVP在线交流]微软原型设计工具SketchFlow企业级应用(二) Web项目VS2005转VS2008平台注意事宜。 [俱乐部在线交流]微软原型设计工具SketchFlow企业级应用(一) [武汉站]Windows 7社区发布活动圆满成功! [武汉站]Windows 7 社区发布活动 宏博软件“移动公教站”之“微软新技术预览”系列资料
[摘]C/C++实现js的split函数功能
Kiven · 2012-10-04 · via 博客园 - Kiven

今天在网上找了一下,发觉有比较多的方法,现在列出来,需要的朋友可以参考一下。

功能最丰富的就是第一种方法了:

1 vector<string> Split(const string& s,const string& match,bool removeEmpty=false,bool fullMatch=false)
2 //参数s为需要肢解的字符串
3 //参数match为肢解匹配字符串
4 //参数removeEmpty为是否删除空字符
5 //参数fullMatch为是否只保留全匹配的字符串
6
7 下面为代码区:
8 #include <string>
9 #include
10
11 namespace Daniweb
12 {
13     using namespace std;
14
15     typedef string::size_type (string::*find_t)(const string& delim,
16                                                 string::size_type offset) const;
17
18     ///


19 /// Splits the string s on the given delimiter(s) and
20 /// returns a list of tokens without the delimiter(s)
21 ///
22 /// The string being split
23 /// The delimiter(s) for splitting
24 /// Removes empty tokens from the list
25 ///
26 /// True if the whole match string is a match, false
27 /// if any character in the match string is a match
28 ///
29 /// A list of tokens
30     vector<string> Split(const string& s,
31                          const string& match,
32                          bool removeEmpty=false,
33                          bool fullMatch=false)
34     {
35         vector<string> result;                 // return container for tokens
36         string::size_type start = 0,           // starting position for searches
37                           skip = 1;            // positions to skip after a match
38         find_t pfind = &string::find_first_of; // search algorithm for matches
39
40         if (fullMatch)
41         {
42             // use the whole match string as a key
43 // instead of individual characters
44 // skip might be 0. see search loop comments
45             skip = match.length();
46             pfind = &string::find;
47         }
48
49         while (start != string::npos)
50         {
51             // get a complete range [start..end)
52             string::size_type end = (s.*pfind)(match, start);
53
54             // null strings always match in string::find, but
55 // a skip of 0 causes infinite loops. pretend that
56 // no tokens were found and extract the whole string
57             if (skip == 0) end = string::npos;
58
59             string token = s.substr(start, end - start);
60
61             if (!(removeEmpty && token.empty()))
62             {
63                 // extract the token and add it to the result list
64                 result.push_back(token);
65             }
66
67             // start the next range
68             if ((start = end) != string::npos) start += skip;
69         }
70
71         return result;
72     }
73 }

 第二种方法:

1 #include
2 #include <string>
3 #include
4 using namespace std;
5
6 vector<string> splitEx(const string& src, string separate_character)
7 {
8     vector<string> strs;
9    
10   int separate_characterLen = separate_character.size();//分割字符串的长度,这样就可以支持如“,,”多字符串的分隔符
11     int lastPosition = 0,index = -1;
12     while (-1 != (index = src.find(separate_character,lastPosition)))
13     {
14         strs.push_back(src.substr(lastPosition,index - lastPosition));
15         lastPosition = index + separate_characterLen;
16     }
17     string lastString = src.substr(lastPosition);//截取最后一个分隔符后的内容
18     if (!lastString.empty())
19         strs.push_back(lastString);//如果最后一个分隔符后还有内容就入队
20     return strs;
21 }
22
23 int _tmain(int argc, _TCHAR* argv[])
24 {
25     string s = "123,456,789,0,888";
26     string del = ","
27     vector<string> strs = splitEx(s, del); 
28     for ( unsigned int i = 0; i < strs.size(); i++) 
29     { 
30         cout << strs[i].c_str() << endl;
31     } 
32     return 0
33 }

第三种方法:

1 #include
2 #include <string>
3 #include
4
5 using namespace std;
6
7 void split(const string& src, const string& separator, vector<string>& dest)
8 {
9     string str = src;
10     string substring;
11     string::size_type start = 0, index;
12
13     do
14     {
15         index = str.find_first_of(separator,start);
16         if (index != string::npos)
17         {   
18             substring = str.substr(start,index-start);
19             dest.push_back(substring);
20             start = str.find_first_not_of(separator,index);
21             if (start == string::npos) return;
22         }
23     }while(index != string::npos);
24     //the last token
25     substring = str.substr(start);
26     dest.push_back(substring);
27 }
28
29
30 int main()
31 {
32     string src = "Accsvr:tcp     -h   127.0.0.1 -p /t 20018   ";
33     vector<string> d, s;
34     vector<string>::iterator p, q;
35     split(src,":",d);
36     for(p=d.begin();p!=d.end();++p)
37     {
38         cout << *p << endl;
39         s.clear();
40         split(*p," /t/n",s);
41         for (q=s.begin();q!=s.end();++q)
42             cout << "/t" << *q << endl;
43     }
44     return 0;
45

第四种方法:

1 #include
2 #include
3 #include <string.h>
4
5 void split(char *src, const char *separator, char **dest, int *num)
6 {
7     char *pNext;
8     int count = 0;
9     if (src == NULL || strlen(src) == 0) return;
10     if (separator == NULL || strlen(separator) == 0) return;
11     pNext = strtok(src,separator);
12     while(pNext != NULL)
13     {
14         *dest++ = pNext;
15         ++count;
16         pNext = strtok(NULL,separator);
17     }
18     *num = count;
19 }
20
21 int main()
22 {
23     char src[] = "Accsvr:tcp  -h    127.0.0.1      -p/n    20018";
24     char *dest[128];
25     char *dest2[128];
26     int num = 0, num2 = 0;
27     int i, j;
28
29     split(src,":",dest,&num);
30
31     for (i=0;i32     {
33         printf("|%s|/n",dest[i]);
34         split(dest[i]," /t/n",dest2,&num2);
35         for (j=0;j36         {
37             printf("|%s|/n",dest2[j]);
38         }
39     }
40     return 0;
41 }

第五种方法:

1 #include
2 #include
3 #include <string.h>
4
5 void split(char *src, const char *separator, char **dest, int *num)
6 {
7     char *pSeparator, *pStart, *pEnd;
8     unsigned int sep_len;
9     int count = 0;
10    
11     if (src == NULL || strlen(src) == 0) return;
12    
13     pSeparator = (char *)malloc(16);
14     if (pSeparator == NULL) return;
15    
16     if (separator == NULL || strlen(separator) == 0) strcpy(pSeparator," ");/* one blank by default */
17     else strcpy(pSeparator,separator);
18
19     sep_len = strlen(pSeparator);
20        
21     pStart = src;
22    
23     while(1)
24     {
25         pEnd = strstr(pStart, pSeparator);
26         if (pEnd != NULL)
27         {
28             memset(pEnd,'/0',sep_len);
29             *dest++ = pStart;
30             pEnd = pEnd + sep_len;
31             pStart = pEnd;
32             ++count;
33         }
34         else
35         {
36             *dest = pStart;
37             ++count;
38             break;
39         }
40     }
41     *num = count;
42     if (pSeparator != NULL) free(pSeparator);
43 }
44
45 int main()
46 {
47     char src[] = "Accsvr:tcp  -h    127.0.0.1    -p    20018";
48     char *dest[128];
49     char *dest2[128];
50     int num = 0, num2 = 0;
51     int i, j;
52
53     split(src,":",dest,&num);
54
55     for (i=0;i56     {
57         printf("|%s|/n",dest[i]);
58         split(dest[i],"/t",dest2,&num2);
59         for (j=0;j60         {
61             printf("|%s|/n",dest2[j]);
62         }
63     }
64     return 0;
65 }

摘自:http://www.cnblogs.com/jackson-leung/archive/2012/01/28/