随机位置批量插入内容 怎么实现??
例:表1有字段1(ID)、字段2(内容)
原有数据:
字段1(ID)――字段2(内容)
1――2、3、4、5、6、7、8
2――2、3、4、5、6、7、8
3――2、3、4、5、6、7、8
4――2、3、4、5、6、7、8
5――2、3、4、5、6、7、8
定义a,b, 批量更新后为:
字段1(ID)――字段2(内容)
1――2、a3、4、5、b6、7、8
2――2、b3、4、a5、6、7、8
3――2、a3、4、5、b6、7、8
4――2、b3、4、5、a6、7、8
5――2、3、b4、5、6、a7、8
就是批量在字段2随机位置插入2个定义好的内容
老师高手能给个具体代码 带注释那种吗,我搞了好几天了,不知道怎么弄??
------解决方案--------------------
- VBScript code
'str---原字符串
'instr_str---需要随即插入的字符串,每个词请用都好隔开
'instr_len---插入的个数
function random_instr(str,instr_str,instr_len)
if trim(str)="" then '为空情况下的处理
random_instr=""
exit function
end if
if trim(instr_str)="" then '为空情况下的处理
random_instr=""
exit function
end if
if trim(instr_len)="" or not isnumeric(instr_len) then '为空情况下的处理
instr_len=1
end if
random_arr=split(instr_str,",")'分割需要随即插入的字符串
if UBound(random_arr)+1<instr_len then'当需要随即插入的字符串的个数小于设定的个数的时候,取需要随即插入的字符串的个数
instr_len=UBound(random_arr)+1
end if
eninser=""'保存已插入的字符串
for i=1 to instr_len
RANDOMIZE
random=round(len(str)*rnd())'随即插入的位置
inser_stred= eninser_str(instr_str,eninser)
if trim(eninser)="" then'保存已插入的字符串
eninser=inser_stred
else'保存已插入的字符串
eninser=eninser& "," &inser_stred
end if
stemp=left(str,random)
if random=0 then stempd=inser_stred & str else stempd= stemp&inser_stred end if 'random等于0时插入在第一个位置
str=replace(str,stemp,stempd)'顺序插入
next
random_instr=str
end function
function eninser_str(ByRef instrstrd,ByRef eninser)
random_arrs=split(instrstrd,",")'分割需要随即插入的字符
RANDOMIZE
randomd=round(UBound(random_arrs)*rnd())'随即取得要插入的字符串
strd=random_arrs(randomd)
if eninser="" then '为空情况下的处理
eninser_str=strd
exit function
end if
if instr(","& strd &",",","& eninser &",")>0 then'判断是否已插入数据
eninser_str instrstrd,eninser
else
eninser_str=strd
exit function
end if
end function
response.Write random_instr("2、3、4、5、6、7、8","A,F,D",2)