vb.net中的for循环为现有字符串添加一个计数器 [英] for loop in vb.net add a counter to existing string
本文介绍了vb.net中的for循环为现有字符串添加一个计数器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个for循环,如下所示:
I have a for loop which goes like this:
for i = 0 as integer to 100
result &= "Name" & sqldr("name")
result &= "Lastname" & sqldr("lastname")
result &= "dob" & sqldr("dob")
next
sqldr是sql数据读取器(此处不重要)
我希望我的最终结果是:
The sqldr is the sql datareader (not important here)
I want my end result to be:
Name1 = Sam
Lastname1 = Davis
dob1 = 01/01/1966
Name2 =
...
Name3 =
,依数据库中的记录数量而定. 如何在此for循环中实现这一点?
and so on depending on how many records are in the database. How do I make this happen in this for loop?
推荐答案
首先,您应该使用StringBuilder,因为它比连接字符串更有效.
Well, first of all you should be using a StringBuilder as it is more efficient than concatenating strings.
因此,以下内容应会产生预期的结果(对不起,我或熟悉C#):
So the following should yield the expected result (sorry I am or aquainted to C#):
Dim sb as StringBuilder = new StringBuilder() ' that is where I am not so sure
for i = 0 as integer to 100
sb.AppendFormat("Name{0} = {1}", i, sqldr("name")
sb.AppendFormat("Lastname{0} = {1}", i, sqldr("lastname")
sb.AppendFormat("dob{0} = {1}", i, sqldr("dob")
next
result = sb.ToString()
这篇关于vb.net中的for循环为现有字符串添加一个计数器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文