9.6 将字符串拆分为字符序列

 

将字符串拆分为字符序列。
统计某个网页源码里中括号外的逗号数量。网页源码部分内容如下:

<html>
<b></b>
<table cellpadding="2.5px" rules="all" style=";background-color: rgb(255,255,255);border: 1px solid;border-collapse: collapse; border-color: rgb(187,187,187)">
<colgroup><col width="25px" style="background-color: rgb(218,231,245)" /><col /><col /><col /><col /><col /><col /></colgroup>
<thead><tr style=" background-color: rgb(218,231,245);text-align: center;color: rgb(22,17,32)"><th></th><th>A</th><th>B</th><th>C</th><th>D</th><th>E</th><th>F</th></tr></thead> 
…

使用函数 A.split() 将字符串拆分成字符序列。

脚本:

A B C
1 =file(“code.html”).read()
2 =A1.split() 0 0
3 for A2 if A3==“[” =B2+=1
4 else if A3==“]” =B2-=1
5 else if A3==“,”&&B2==0 >C2+=1

A1 从文件中读取字符串
A2 使用 A.split() 函数将字符串拆分成字符序列
B3-C3 出现左中括号,B2 加 1,用来匹配括号
B4-C4 出现右中括号,B2 减 1,用来匹配括号
B5-C5 如果出现逗号,且中括号匹配时,C2 计数加 1。C2 就是括号外逗号的数量。

运行结果:

A2:

Members
<
h
t
m

C2:

Value
27