2012-04-26 52 views
1

我需要帮助使一个功能,打开一个文件,并为每一个新行,在PHP中使像explode("\n", $var)变量。我试过VB.NET对于每一个新行

Dim words As String = GetFileContents(OpenFileDialog1.FileName) 
For Each word As String In words 
    Dim doHash As String = MD5(word) 
    If String.Equals(doHash, hash.Text) Then 
     Label2.Text = "derp" 
    Else 
     Label2.Text = "lol" 
    End If 
Next 

但它使每个字母成为一个新的变量。

+0

您可能正在寻找['String.Split'](http://msdn.microsoft.com/zh-CN/library/tabh47cf%28v=vs.100%29.aspx)。以下是C#中爆炸的示例:http://www.dotnetperls.com/explode-function – 2012-04-26 17:52:55

+0

您可以提供'GetFileContents'的代码吗?它看起来像是将值作为单个String而不是String的数组返回。如果你要迭代它们,你需要一个数组。 – mellamokb 2012-04-26 17:52:57

+1

阅读['String.Split'](http://msdn.microsoft.com/en-us/library/system.string.split.aspx)和/或['File.ReadLines'](http:///msdn.microsoft.com/en-us/library/dd383503.aspx)。 – vcsjones 2012-04-26 17:53:14

回答

1

您想使用System.IO.File.ReadLines(OpenFileDialog1.FileName)。这将导致For Each循环分别获取每条线。

具体做法是:

For Each word As String In File.ReadLines(OpenFileDialog1.FileName) 
    Dim doHash As String = MD5(word) 
    If String.Equals(doHash, hash.Text) Then 
     Label2.Text = "derp" 
    Else 
     Label2.Text = "lol" 
    End If 
Next 

我注意到,你的PHP例子肯定是分裂的新行,但你的循环变量称为word。你的文件每行有一个单词吗?没关系,但我想仔细检查一下你是否可以得到每个,而不是每个单词(如果每行有多个单词)。

+0

每行有一个字,是的。我将看看System.IO.File.ReadLines(OpenFileDialog1.FileName) – user1328301 2012-04-26 18:02:18

+0

它给我一个语法错误@ System.IO.File.ReadLines System.IO被导入 ------------ ------- For Each File.ReadLines(word)As String In words ------------------- 就是我正在使用的 – user1328301 2012-04-26 18:04:09

+0

//编辑http://pastebin.com/0AAfwVQt 我改变了,但我得到的错误 “Word未声明” – user1328301 2012-04-26 18:06:58