字符分隔问题!帮忙解决的啊!

zwm00 2006-12-23 09:45:55
有如下格式的数据:
12 45 56 56 55
58 87 45 56 45
从文本文档读入后:
要得到一个2行5列的数组
结果得到了空字符串,在有回车的地方(每行结尾,怎么去掉的啊)
各位帮帮忙啊!不胜感激!
代码如下:
public c(,) as integer
Dim Str As IO.StreamReader = New IO.StreamReader(IO.File.OpenRead(file_name), System.Text.Encoding.GetEncoding("gb2312"))
Dim rs As String = Str.ReadToEnd
dim cc() as string=rs.split(VbCrLf)
dim i as integer=0
dim j as integer =0
dim a as integer
dim b as integer =UBound(cc)
for i=0 to b
dim d() as string=cc(i).split(nothing)
a=UBound(d)
             redim c(b,a)
for j=0 to a
            response.write(d(j))
next
next
response.write("数据写出结束")
Str.Close()
...全文
121 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zwm00 2006-12-23
  • 打赏
  • 举报
回复
非常感谢:dhxyxiaofeng(芝麻狐),感谢他的热心帮助!!!
问题很快得到解决,呵呵!
主要就是下面这句,
Dim strConvert As String = cc(i).Replace(vbLf, "")

今晚发的贴现在就结了!
再次感谢:dhxyxiaofeng(芝麻狐),
dhxyxiaofeng 2006-12-23
  • 打赏
  • 举报
回复
分析了一下

cc(0) = 12 45 56 56 55 (注意:这里有两个字符 CR 和 LF)
cc(1) = 58 87 45 56 45

这句 : dim cc() as string=rs.split(VbCrLf)

VbCrLf 是 CR 和 LF 的组合符。但是在用 rs.split(VbCrLf) 取数组时,没有将 LF 作为分隔符忽略掉。(这个还没明白为什么)

问题出现在cc(1,0) = LF。

所以又改了一下:

For i = 0 To b
Dim strConvert As String = cc(i).Replace(vbLf, "")
Dim d() As String = strConvert.Split(Nothing)
a = UBound(d)
ReDim c(b, a)
For j = 0 To a
'response.write(d(j))
MsgBox(d(j))
Next
Next
dhxyxiaofeng 2006-12-23
  • 打赏
  • 举报
回复
用正则表达式改了一下你的代码。我是用WINFORM调的。看看对你有没有帮助

For i = 0 To b

Dim mc As MatchCollection = Regex.Matches(cc(i), "\d{2}", RegexOptions.Compiled)
Dim d(mc.Count - 1) As Object

mc.CopyTo(d, 0)

a = UBound(d)
ReDim c(b, a)
For j = 0 To a
'response.write(d(j))
MsgBox(Convert.ToString(d(j)))
Next
Next


引用:
Imports System.Text.RegularExpressions
zwm00 2006-12-23
  • 打赏
  • 举报
回复
是的啊!放到一个字符数组里面后再转换成数值型的!
也可以只直接转换放到一个数值型的数组里面!!
问题的关键是每一行的后面出现了一个多余的字符!怎么去掉!
dhxyxiaofeng 2006-12-23
  • 打赏
  • 举报
回复
你就想把这个序列放到一个字符数组中吗?
zwm00 2006-12-23
  • 打赏
  • 举报
回复
补充:部分代码省略了!

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧