(筆記) 如何使用C語言實現split()? (C/C++) (C) (JavaScript)
Abstract
寫過JavaScript或ASP的朋友,應該常常用到split()這個函數,他可以輕易地將string轉成array,C語言並沒有相對應的函數,只有strtok()較為接近,稍微加工後,就可以在C語言實現split()。
Introduction
使用環境 : IE 7.0 + Visual Studio 2008
在JavaScript,可以輕易的將string轉成array。
split.htm / JavaScript
1 <!--
2 (C) OOMusou 2009 http://oomusou.cnblogs.com
3
4 Filename : split.htm
5 Compiler : IE 7.0
6 Description : javaScript's split()
7 Release : 05/09/2009
8 -->
9 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
10 <html xmlns="http://www.w3.org/1999/xhtml">
11 <head>
12 <script language="javascript" type="text/javascript">
13 function Button1_onclick() {
14 str = "10,20,30";
15 arr = str.split(",");
16
17 for(i=0; i < 3; i++)
18 document.getElementById("div1").innerHTML += arr[i] + "<br>";
19 }
20 </script>
21 </head>
22 <body>
23 <input id="Button1" type="button" value="button" onclick="return Button1_onclick()" />
24 <div id="div1">
25 </div>
26 </body>
27 </html>
2 (C) OOMusou 2009 http://oomusou.cnblogs.com
3
4 Filename : split.htm
5 Compiler : IE 7.0
6 Description : javaScript's split()
7 Release : 05/09/2009
8 -->
9 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
10 <html xmlns="http://www.w3.org/1999/xhtml">
11 <head>
12 <script language="javascript" type="text/javascript">
13 function Button1_onclick() {
14 str = "10,20,30";
15 arr = str.split(",");
16
17 for(i=0; i < 3; i++)
18 document.getElementById("div1").innerHTML += arr[i] + "<br>";
19 }
20 </script>
21 </head>
22 <body>
23 <input id="Button1" type="button" value="button" onclick="return Button1_onclick()" />
24 <div id="div1">
25 </div>
26 </body>
27 </html>
split.c / C
1 /*
2 (C) OOMusou 2009 http://oomusou.cnblogs.com
3
4 Filename : split.c
5 Compiler : Visual C++ 9.0
6 Description : Demo how to implement split() in C
7 Release : 05/09/2009 1.0
8 */
9
10 #include <stdio.h>
11 #include <string.h>
12
13 void split(char **arr, char *str, const char *del) {
14 char *s = strtok(str, del);
15
16 while(s != NULL) {
17 *arr++ = s;
18 s = strtok(NULL, del);
19 }
20 }
21
22 int main() {
23 char *str = "10,20,30";
24 char *arr[3];
25 const char *del = ",";
26 int i = 0;
27 split(arr, str, del);
28
29 while(i<3)
30 printf("%s\n", *(arr+i++));
31 }
2 (C) OOMusou 2009 http://oomusou.cnblogs.com
3
4 Filename : split.c
5 Compiler : Visual C++ 9.0
6 Description : Demo how to implement split() in C
7 Release : 05/09/2009 1.0
8 */
9
10 #include <stdio.h>
11 #include <string.h>
12
13 void split(char **arr, char *str, const char *del) {
14 char *s = strtok(str, del);
15
16 while(s != NULL) {
17 *arr++ = s;
18 s = strtok(NULL, del);
19 }
20 }
21
22 int main() {
23 char *str = "10,20,30";
24 char *arr[3];
25 const char *del = ",";
26 int i = 0;
27 split(arr, str, del);
28
29 while(i<3)
30 printf("%s\n", *(arr+i++));
31 }
執行結果
將strtok()稍微加工,將結果塞到array中,就跟JavaScript的split()一模一樣了。
完整程式碼下載
split_javascript.7z (JavaScript)
split_c.7z (C)