回 帖 发 新 帖 刷新版面

主题:探讨下:如何把让数组里面的字符串连接起来

<%
dim strsql,conn,rs
set conn=server.createobject("adodb.connection")
conn.connectionstring="DRIVER={SQL Server};SERVER=192.168.1.102;uid=sa;pwd=sa;DATABASE=goto63net" 
set rs=server.createobject("adodb.recordset")
conn.open
rs.open"select top 9 topic_title,postdate from bbs_topics",conn,1,1
'这里打开出一个表取出9条纪录来
'------方法一------------------------------------
'这里设置strTemp1,strTemp2,strTemp3把数据保存起来
dim strTemp1,strTemp2,strTemp3
dim i :i=1   '声明设置变量值
strTemp1=""
strTemp2=""
strTemp3="" 
while not rs.eof
if i<=3 then     '字符串连接肯定是最费时的了
strTemp1=strTemp1&"<P>"&rs("topic_title")&"--"&rs("postdate")&"</p>"
elseif i<=6 then
strTemp2=strTemp2&"<P>"&rs("topic_title")&"--"&rs("postdate")&"</p>"
elseif i<=9 then
strTemp3=strTemp3&"<P>"&rs("topic_title")&"--"&rs("postdate")&"</p>"
end if
i=i+1
rs.movenext
wend
'-------方法二-------------------------------------------------------
rs.MoveFirst()   '回到首行
dim strTemp(9)    '设置一个9的数组
i=0
while not rs.eof
strTemp(i)="<P>"&rs("topic_title")&"--"&rs("postdate")&"</p>"
i=i+1
rs.movenext
wend
'那我在怎么把3一组连接成字符串 然后直接在下面调用就好拉
dim strTemp4:strTemp4=""
dim strTemp5:strTmep5=""
dim strTemp6:strTmep6=""

'strTemp4=strTemp(0)&strTemp(2)&strTemp(2) 
'我自己想是这么做,但和上面的方法一的连接字符串一样了,有什么好的方法啊


rs.close
set rs=nothing
conn.close
set conn=nothing

%>
<table style="font-size:12px;" width="50%" align="center" height="100" border="1" cellspacing="0" cellpadding="0">
  <tr><td colspan="3" style="font-size:16px;font-weight:800;color:#0033CC;">这是方法一显示结果:</td></tr>
  <tr>
    <td><%=strTemp1%></td>
    <td><%=strTemp2%></td>
    <td><%=strTemp3%></td>
  </tr>
</table>
<table style="font-size:12px;" width="50%" align="center" height="100" border="1" cellspacing="0" cellpadding="0">
  <tr><td colspan="3" style="font-size:16px;font-weight:800;color:#0033CC;">这是方法二显示结果:</td></tr>
  <tr>
    <td><%=strTemp4%></td>
    <td><%=strTemp5%></td>
    <td><%=strTemp6%></td>
  </tr>
</table>

关键是这句:
'strTemp4=strTemp(0)&strTemp(2)&strTemp(2) 
'我自己想是这么做,但和上面的方法一的连接字符串一样了,有什么好的方法啊

怎么连接才能减少字符串连接的开销呢。

回复列表 (共6个回复)

沙发

join可以连接,但怎么他分成三部分呢。
这也是个问题啊。

板凳

想出这个办法,但肯定不是最好的,还请大家把自己的方法给我看看:
while not rs.eof
if i<=2 then    
strTemp5(i)="<P>"&rs("topic_title")&"--"&rs("postdate")&"</p>"
elseif i<=5 then
strTemp6(i-2)="<P>"&rs("topic_title")&"--"&rs("postdate")&"</p>"
elseif i<=8 then
strTemp7(i-5)="<P>"&rs("topic_title")&"--"&rs("postdate")&"</p>"
end if
i=i+1
rs.movenext
wend
显示数据就用:
  <tr>
    <td><%=join(strTemp5)%></td>
    <td><%=join(strTemp6)%></td>
    <td><%=join(strTemp7)%></td>
  </tr>

这个因该比连接字符串更好了。

3 楼

怎么没人来解答下啊。

4 楼

在问个问题是前面的程序比下面的代码那个效率高:
while not rs.eof
response.write "<tr><td>"&rs("postdate")&"</td></tr>"
rs.movenext
wend
这段代码的效率 能比前面的效率高吗? 

5 楼


<%
while not rs.eof
%>
 <tr><td><%=rs("postdate")%></td></tr>
<%
wend
%>
效率高

6 楼


身为asp程序员这么久一直还在用
<%
while not rs.eof
%>
 <tr><td><%=rs("postdate")%></td></tr>
<%
wend
%> 
我还真是菜啊。。。。
看来要学的东西还是很多啊。
哎。。。。。。。
不知道还有多少人还在用这种方法写程序呢。,
都是网上教程惹的祸,都是教人写这种垃圾代码的我好悲愤啊

我来回复

您尚未登录,请登录后再回复。点此登录或注册