ASP 过滤数组重复数据函数(加强版)


Posted in Javascript onMay 31, 2010

函数代码:

<%'******************************************************* 
'过滤数组重复函数名称:array_no(cxstr1,cxstr2,cxstr3) 
'cxstr1:任意的字符串,自动识别 
'cxstr2:cxstr1中分割符号。 
'cxstr3:提取结果中的某一位置字串,等于0时返回为全部,大于数组下标时返回最后. 
'使用于二维数组 
'******************************************************* 
function array_no(cxstr1,cxstr2,cxstr3) 
if len(cxstr3) > 0 then 
if not IsNumeric(cxstr3) then 
array_no = "对不起,参数3类型必需为数字" 
Exit Function 
end if 
else 
array_no = "对不起,参数3类型必需为数字" 
Exit Function 
end if 
if isarray(cxstr1) then 
array_no = "对不起,参数1不能为数组" 
Exit Function 
end if 
if cxstr1 = "" or isempty(cxstr1) then 
array_no = "没有数据" 
Exit Function 
end if 
ss = split(cxstr1,cxstr2) 
cxs=cxstr2&ss(0)&cxstr2 
sss=cxs 
for m = 0 to ubound(ss) 
cc = cxstr2&ss(m)&cxstr2 
if instr(sss,cc)=0 then 
sss = sss&ss(m)&cxstr2 
end if 
next 
array_no = right(sss,len(sss)-len(cxstr2)) 
array_no = left(array_no,len(array_no)-len(cxstr2)) 
if cxstr3 <> 0 then 
cx_sp = split(array_no,cxstr2) 
if cxstr3 > ubound(cx_sp) then 
array_no = cx_sp(ubound(cx_sp)) 
else 
array_no = cx_sp(cxstr3) 
end if 
end if 
end function%>

下面是测试代码:

<%s1 = "abc,aa,bb,cdef,bc,abcdef,hhgg,gggg,cde,edc" 
s2 = "1,2,3,11,22,33,12,13,14,11,33,333,14" 
s3 = "" 
s4 = "sdf,abc,12,2,2,abc" 
s5 = split(s4) 
response.write "字串为字符时:"&array_no(s1,",",0)&"<br>" 
response.write "字串为数字时:"&array_no(s2,",",0)&"<br>" 
response.write "字串为空时:"&array_no(s3,",",0)&"<br>" 
response.write "字串为混合时:"&array_no(s4,",",0)&"<br>" 
response.write "字串为数组时:"&array_no(s5,",",0)&"<br>" 
response.write "字串为未知变量时:"&array_no(s33,",",0)&"<br>" 
response.write "提取某一位时,没有超过下标时:"&array_no(s1,",",2)&"<br>" 
response.write "提取某一位时,超过下标时:"&array_no(s1,",",200)&"<br>"%>

测试结果:
字串为字符时:abc,aa,bb,cdef,bc,abcdef,hhgg,gggg,cde,edc 
字串为数字时:1,2,3,11,22,33,12,13,14,333 
字串为空时:没有数据 
字串为混合时:sdf,abc,12,2 
字串为数组时:对不起,参数1不能为数组 
字串为未知变量时:没有数据 
提取某一位时,没有超过下标时:bb 
提取某一位时,超过下标时:edc

三水点靠木增强版本: 解决了数组常见错误

<% 
'******************************************************* 
'过滤数组重复函数名称:array_no(cxstr1,cxstr2,cxstr3) 
'cxstr1:任意的字符串,自动识别 
'cxstr2:cxstr1中分割符号。 
'cxstr3:提取结果中的某一位置字串,等于0时返回为全部,大于数组下标时返回最后. 
'使用于二维数组 
'******************************************************* 
function array_no(cxstr1,cxstr2,cxstr3) 
if len(cxstr3) > 0 then 
if not IsNumeric(cxstr3) then 
array_no = "对不起,参数3类型必需为数字" 
Exit Function 
end if 
else 
array_no = "对不起,参数3类型必需为数字" 
Exit Function 
end if 
if isarray(cxstr1) then 
array_no = "对不起,参数1不能为数组" 
Exit Function 
end if 
if cxstr1 = "" or isempty(cxstr1) then 
array_no = "没有数据" 
Exit Function 
end if 
do while instr(cxstr1,",,")>0 
cxstr1=replace(cxstr1,",,",",") 
loop 
if right(cxstr1,1)="," then 
cxstr1=left(cxstr1,len(cxstr1)-1) 
end if 
ss = split(cxstr1,cxstr2) 
cxs=cxstr2&ss(0)&cxstr2 
sss=cxs 
for m = 0 to ubound(ss) 
cc = cxstr2&ss(m)&cxstr2 
if instr(sss,cc)=0 then 
sss = sss&ss(m)&cxstr2 
end if 
next 
array_no = right(sss,len(sss)-len(cxstr2)) 
array_no = left(array_no,len(array_no)-len(cxstr2)) 
if cxstr3 <> 0 then 
cx_sp = split(array_no,cxstr2) 
if cxstr3 > ubound(cx_sp) then 
array_no = cx_sp(ubound(cx_sp)) 
else 
array_no = cx_sp(cxstr3) 
end if 
end if 
end function s1 = "abc,aa,bb,cdef,bc,abcdef,hhgg,gggg,cde,edc,333,,,,,333,7,,,," 
s2 = "1,2,3,11,22,33,12,13,14,11,33,333,14,333,,,,,333,7,,,," 
s3 = "" 
s4 = "sdf,abc,12,2,2,abc,333,,,,,333,7,,,," 
s5 = split(s4) 
response.write "字串为字符时:"&array_no(s1,",",0)&"<br>" 
response.write "字串为数字时:"&array_no(s2,",",0)&"<br>" 
response.write "字串为空时:"&array_no(s3,",",0)&"<br>" 
response.write "字串为混合时:"&array_no(s4,",",0)&"<br>" 
response.write "字串为数组时:"&array_no(s5,",",0)&"<br>" 
response.write "字串为未知变量时:"&array_no(s33,",",0)&"<br>" 
response.write "提取某一位时,没有超过下标时:"&array_no(s1,",",2)&"<br>" 
response.write "提取某一位时,超过下标时:"&array_no(s1,",",200)&"<br>" 
%>

主要是增加了判断
do while instr(cxstr1,",,")>0 
cxstr1=replace(cxstr1,",,",",") 
loop 
if right(cxstr1,1)="," then 
cxstr1=left(cxstr1,len(cxstr1)-1) 
end if
Javascript 相关文章推荐
window.open的功能全解析
Oct 10 Javascript
jquery validator 插件增加日期比较方法
Feb 21 Javascript
JavaScript字符串插入、删除、替换函数使用示例
Jul 25 Javascript
bootstrap data与jquery .data
Jul 07 Javascript
IE8中动态创建script标签onload无效的解决方法
Dec 22 Javascript
create-react-app构建项目慢的解决方法
Mar 14 Javascript
深入浅析JS中的严格模式
Jun 04 Javascript
AngularJS 监听变量变化的实现方法
Oct 09 Javascript
Node.js 的 GC 机制详解
Jun 03 Javascript
简单了解vue中的v-if和v-show的区别
Oct 08 Javascript
vue keep-alive 动态删除组件缓存的例子
Nov 04 Javascript
在JavaScript中如何使用宏详解
May 06 Javascript
javascript 子窗体父窗体相互传值方法
May 31 #Javascript
js post方式传递提交的实现代码
May 31 #Javascript
JS 类型转换常见方法小结
May 31 #Javascript
javascript 传统事件模型构造的事件监听器实现代码
May 31 #Javascript
LazyLoad 延迟加载(按需加载)
May 31 #Javascript
基于jquery的气泡提示效果
May 31 #Javascript
niceTitle 基于jquery的超链接提示插件
May 31 #Javascript
You might like
PHP中文URL编解码(urlencode()rawurlencode()
2010/07/03 PHP
一组PHP可逆加密解密算法实例代码
2014/01/21 PHP
thinkphp的静态缓存用法分析
2014/11/29 PHP
在WordPress中使用wp_count_posts函数来统计文章数量
2016/01/05 PHP
简介PHP的Yii框架中缓存的一些高级用法
2016/03/29 PHP
PHP对象实例化单例方法
2017/01/19 PHP
javascript中substr,substring,slice.splice的区别说明
2010/11/25 Javascript
jquery动态添加删除div 具体实现
2013/07/20 Javascript
深入理解JavaScript系列(25):设计模式之单例模式详解
2015/03/03 Javascript
理解javascript中的严格模式
2016/02/01 Javascript
JS 清除字符串数组中,重复元素的实现方法
2016/05/24 Javascript
JavaScript日期对象(Date)基本用法示例
2017/01/18 Javascript
jquery获取select选中值的文本,并赋值给另一个输入框的方法
2018/08/21 jQuery
小程序从手动埋点到自动埋点的实现方法
2019/01/24 Javascript
vue实现导航菜单和编辑文本的示例代码
2020/07/04 Javascript
Python 新建文件夹与复制文件夹内所有内容的方法
2018/10/27 Python
python中dir()与__dict__属性的区别浅析
2018/12/10 Python
django组合搜索实现过程详解(附代码)
2019/08/06 Python
使用python绘制温度变化雷达图
2019/10/18 Python
Django 再谈一谈json序列化
2020/03/16 Python
OpenCV 之按位运算举例解析
2020/06/19 Python
HTML5通过调用canvas对象的getContext()方法来获取绘图环境
2014/06/23 HTML / CSS
详解通过focusout事件解决IOS键盘收起时界面不归位的问题
2019/07/18 HTML / CSS
Michael Kors美国官网:美式奢侈生活风格的代表
2016/11/25 全球购物
捷克家居装饰及图书音像购物网站:Velký košík
2018/04/16 全球购物
如何利用XMLHTTP检测URL及探测服务器信息
2013/11/10 面试题
缓刑人员的思想汇报
2014/01/11 职场文书
大学老师推荐信
2014/02/25 职场文书
实习单位评语
2014/04/26 职场文书
超市商业计划书
2014/05/04 职场文书
大专生找工作自荐书
2014/06/10 职场文书
重阳节活动总结
2014/08/27 职场文书
毕业生评语大全
2015/01/04 职场文书
2015年生产车间工作总结
2015/04/22 职场文书
政审证明材料
2015/06/19 职场文书
SpringBoot项目中控制台日志的保存配置操作
2021/06/18 Java/Android