ASP数组字符串操作类

在处理数据库,特别是在处理权限时,时常需要使用关系数据结构来定义一对多、多对多的关系结构,一般都采用一个数据表来关联两个表记录的关系,然而使用这种方法比较繁琐,因为必须增加额外的处理代码维护和使用这种关系,而且每次都必须反复对数据库进行查询。我采用一个字段来简化这种操作,这个字段保存一个记录对应另一个表的多个记录的id,每个目标ID都转换为字符串,每个ID之间用“,”分隔,然而这就需要一个专门处理ID串的函数从字串中提取ID或加入ID,为了方便,特此编写了一个完整的数组字符串操作类。

'*********************************
'**********CArray ****************
'*********************************
'名称:数组字符串操作类
'用途:操作数组串,具有对串项的分解、查询、定位、增减,组串的功能,一般用于数据库中ID值的操作

class CArray
 dim m_aryStr
 dim m_val()
 dim m_cnt
 dim m_sepa
 '初始化
 private sub class_initialize()
  m_cnt=0
  m_aryStr=""
  m_sepa=","
 end sub 
 '设置分隔符号
 property let Sepa(byval str)
  str=trim(str)
  if str<>"" then  m_sepa=str&""
 end property
 
  '串操作
 property let AryStr(byval str)
  m_aryStr=trim(str)&""
  if str<>"" then
   SplitStr()
  else
   m_cnt=0
  end if
 end property
 property get AryStr()
  AryStr=m_aryStr
 end property
 '数组维数
 property get ItemCount()
  ItemCount=m_cnt
 end property
 '方法
 public function GetItem(byval idx)
  GetItem=""
  if idx>=0 and idx<m_cnt then
   GetItem=m_Val(idx)
  end if
 end function
 public function FindItem(byval startidx,byval keystr,byval cap)
  dim i,tstr
  keystr=""&trim(keystr)
  if cap=FALSE then keystr=Ucase(keystr)
  FindItem=-1
  if m_cnt>0 and startidx<m_cnt then
   for i=startidx to m_cnt-1
    tstr=m_val(i)
    if cap=FALSE then tstr=ucase(tstr)
    if tstr=keystr then
     FindItem=i
     exit function
    end if
   next
  end if
 end function
 
 public sub AddItem(byval str)
  str=trim(str)
  if str<>"" then
   m_cnt=m_cnt+1
   redim Preserve m_val(m_cnt)
   m_val(m_cnt-1)=str
   BuildAry()
  end if
 end sub
 
 public sub SetItem(byval idx,byval str)
  str=trim(str)
  if idx>=0 and idx<m_cnt and  str<>"" then
   m_val(idx)=str
   BuildAry()
  end if
 end sub
 
 public function RemoveItem(byval idx)
  dim i
  RemoveItem=false
  if m_cnt>0 and idx<m_cnt then
   for i=idx+1 to m_cnt-1
    m_val(i-1)=m_val(i)
   next
   m_cnt=m_cnt-1
   redim Preserve m_val(m_cnt)
   BuildAry()
   RemoveItem=True
  end if
 end function
 public function GetInterSection(byval str)
  dim i,j,tstr
  dim ary
  set ary=new carray
  ary.arystr=str
  tstr=""
  if m_cnt>0 and ary.itemcount>0 then
   for i=0 to m_cnt-1
    if ary.finditem(0,m_val(i),false)>=0 then
     tstr=appsepa(tstr,",")&m_val(i)
    end if
   next
  end if
  GetInterSection=tstr
 end function
 '*****************局部函数********************
 private sub SplitStr()
  dim ary,i
  on error resume next
   ary=split(m_aryStr,m_sepa)
   if isarray(ary) then
    m_cnt=ubound(ary)+1
    redim m_val(m_cnt)
    for i=0 to m_cnt-1
     m_val(i)=trim(ary(i))
    next
   else
    m_cnt=0
   end if
 end sub
 '
 private sub BuildAry()
  dim i,str
  str=""
  if m_cnt>0 then
   for i=0 to m_cnt-1
    str=Appsepa(str,m_sepa)&m_val(i)
   next
   m_aryStr=str
  else
   m_aryStr=""
  end if
 end sub
 
end Class

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值