VB 读取文件的空格问题

miocn 2005-10-24 12:11:17
我初学VB ,用VB 从文件中读取一断中英文混合的片段:
Get #FileNumber, LOF(FileNumber) - 127, strFileTag
strBfileTag = StrConv(strFileTag, vbFromUnicode)

'具体的内容
strTag = StrConv(MidB$(strBfileTag, 1,30), vbUnicode)

strTag 的内容就该是"你好世界 "
strTag后面的空格却怎么也去不掉。 我查看了一下空格的内容,发现安全可靠不是一般的asc(32) 而是01 请问各位大哥,这是什么原因。 以及怎么解决这个问题!
...全文
259 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
winehero 2005-10-24
  • 打赏
  • 举报
回复
01是个不可见的控制字符,视觉上感觉是一空格。。。
用楼上的方法去掉01字符就可以了。
孙小雄 2005-10-24
  • 打赏
  • 举报
回复
strTag=Replace(strTag,asc(01),"")
miocn 2005-10-24
  • 打赏
  • 举报
回复
谢谢两位的帮助,但是我用过之后,还是一样啊,我看结果仍然是这样。这是十六进制值

00000000h: 61 61 61 61 61 61 61 61 61 61 01 01 01 01 01 01 ; aaaaaaaaaa......
00000010h: 01 01 01 01 01 01 01 01 01 01 01 01 01 01 ; ..............

另外,我想请问的是, 为什么明明我文件中没有这些01空格,但读出来时却有了。 谢谢
Public Class Form1 Inherits System.Windows.Forms.Form Public Filename As String = "引用英汉词典.txt" '定义连接数据的文本TXT Public Myword(6500, 1) As String '定义二维数组 Public words As Integer = 0 '记录连接数据的文本TXT 单词个数  Private Sub Form1_load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim main As String Dim chang As Integer ' 单词长度 Dim i As Integer = 0 '数组的开始位置 Dim n As String '读取相应的单词 Dim m As String '读取单词的中文解释 Dim stringchang As Integer '计算单词后字符串的长度 TextBox1.Text = "" TextBox2.Text = "" FileOpen("英汉小辞典") :|bin:|引用英汉小辞典) '打开文件,相对路径 Do While Not EOF(1) main = LineInput(1) chang = InStr(main, " ") '查找空格的位子。 n = Microsoft.VisualBasic.Left(main, chang - 1) '截取空格字符:单词 Myword(i, 0) = n '保存新的单词 ListBox1.Items.Add(n) stringchang = Len(main) - chang m = Trim(Microsoft.VisualBasic.Right(main, stringchang)) '剩下的字符串赋给变量值m Myword(i, 1) = m '保存中文的翻译(解释) i = i + 1 Loop words = i FileClose(1) End Sub Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged Try TextBox1.Text = Myword(ListBox1.SelectedIndex, 0) TextBox2.Text = Trim(Myword(ListBox1.SelectedIndex, 1)) Catch ex As Exception Exit Sub End Try End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i As Integer = -1 '以数组来进行起始查询 If TextBox1.Text = "" Then MessageBox.Show("不能输入空字符,请重新输入") TextBox2.Text = "" TextBox1.Focus() Exit Sub Else For i = i + 1 To words If LCase(TextBox1.Text) = LCase(Myword(i, 0)) Then TextBox2.Text = Trim(Myword(i, 1)) Exit Sub End If Next MessageBox.Show("不存在您所需要的单词,你需要添加一个新的") End If End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim i As Integer = 0 Dim k As Integer Dim EnterWords, ch As String AB: EnterWords = InputBox("请输入想要添加的单词", "添加一个新的单词") '先输入单词 If EnterWords = "" Then MessageBox.Show("必须要输入单词") GoTo AB End If AC: ch = InputBox("需要您输入中文意思", "添加一个新的中文意思") '输入中文意思 If ch = "" Then MessageBox.Show("请输入中文翻译") GoTo AC End If Do While LCase(Myword(i, 0)) < LCase(EnterWords) i = i + 1 If words = i Then '找完数据库再进行添加 Myword(i, 0) = EnterWords '把新添加的单词赋给I位置 Myword(i, 1) = ch '把新添加的单词(中文意思)赋给I的位置 words = words + 1 FileOpen(1, Filename, OpenMode.Output) '打开一个文件 For i = 0 To words - 1 PrintLine(1, Myword(i, 0) & " " & Myword(i, 1)) Next ListBox1.Items.Clear() FileClose(1) '关闭文件 ListBox1.Items.Clear() Form1_load(sender, e) MessageBox.Show("添加成功") Exit Sub End If Loop If LCase(Myword(i, 0)) = LCase(EnterWords) Then MessageBox.Show("词库里已经存在这个单词了") Exit Sub Else For k = words To i + 1 Step -1 Myword(k + 1, 0) = Myword(k, 0) Myword(k + 1, 1) = Myword(k, 1) Next k Myword(i, 0) = EnterWords '把添加的单词赋给I的位置 Myword(i, 1) = ch '把添加的单词(中文意思)赋给I的位置 words = words + 1 FileOpen(1, Filename, OpenMode.Output) '打开一个文件 For i = 0 To words - 1 PrintLine(1, Myword(i, 0) & " " & Myword(i, 1)) '数组里的单词写入文本文件(TXT) Next FileClose(1) '文件将会关闭 ListBox1.Items.Clear() Form1_load(sender, e) MessageBox.Show("添加成功") Exit Sub End If End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim ch As String Dim j As Integer If -1 = ListBox1.SelectedIndex Then MsgBox("请选择单词再进行修改") ListBox1.Focus() Exit Sub End If AD: ch = InputBox("请输入修改单词的中文意思", "修改单词", Trim(Myword(ListBox1.SelectedIndex, 1))) If ch = "" Then MessageBox.Show("不能删除以前的中文解释") GoTo AD End If Myword(ListBox1.SelectedIndex, 1) = ch FileOpen(1, Filename, OpenMode.Output) For j = 0 To words - 1 PrintLine(1, Myword(j, 0) & " " & Myword(j, 1)) Next FileClose(1) '文件关闭 ListBox1.Items.Clear() Form1_Load(sender, e) End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim i, j, k As Integer k = MsgBox("您确定是否删除吗?", MsgBoxStyle.YesNo) If 6 = k Then For i = ListBox1.SelectedIndex To words Myword(i, 0) = Myword(i + 1, 0) Myword(i, 1) = Myword(i + 1, 1) Next words = words - 1 FileOpen(1, Filename, OpenMode.Output) ' 打开文件 For j = 0 To words - 1 PrintLine(1, Myword(j, 0) & " " & Myword(j, 1)) Next FileClose(1) '文件关闭 MsgBox("单词已经删除了") ListBox1.Items.RemoveAt(ListBox1.SelectedIndex) ListBox1.Refresh() TextBox1.Text = "" TextBox2.Text = "" Exit Sub Else Exit Sub End If End Sub End Class

7,765

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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