如何 return 满足条件的文本文件中的字符串?
How to return a string from a text file with condition met?
Public Sub openDB()
Dim Lines As New List(Of String)
Try
' Open the file using a stream reader.
Using sr As New StreamReader("Config.txt")
Dim line As String
' Read the stream to a string and write the string to the console.
line = sr.ReadLine()
Do Until String.IsNullOrEmpty(line)
Lines.Add(line)
line = sr.ReadLine
Loop
End Using
Catch e As Exception
Console.WriteLine("The file could not be read:")
Console.WriteLine(e.Message)
End Try
Dim dbname As String = g_DatabaseName
Dim server As String = Lines.Where(Function(str) str.Contains("server =")).ToString
Dim user As String = ""
Dim password As String = ""
conn = New MySqlConnection
conn.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false; Convert Zero Datetime=True", server, user, password, dbname)
conn.Open()
End Sub
我尝试 return 文本文件中的一些字符串,因此我使用 StreamReader 读取文件并将它们存储到列表中。现在我尝试声明一个变量以从字符串列表中获取 "localhost",但下面的代码对我不起作用。
Dim server As String = Lines.Where(Function(str) str.Contains("server
=")).ToString
Enumerable.Where
不是 return 单个字符串,但可能是多个字符串,使用 ToString
给你的不是第一个匹配行,而是类型的名称 System.Linq.Enumerable+WhereArrayIterator1[System.String]
.
要么声明为IEnumerable(Of String)
,要么用First
/FirstOrDefault
得到符合条件的第一行:
Dim serverLine As String = Lines
.Where(Function(str) str.Contains("server ="))
.FirstOrDefault()
您还可以使用 FirstOrDefault
的重载(Nothing
如果没有这一行):
Dim serverLine As String = Lines.FirstOrDefault(Function(str) str.Contains("server ="))
提取Localhost
:
Dim server As String = serverLine.Substring(serverLine.IndexOf("server =") + "server =".Length).Trim(""""c, " "c)
Public Sub openDB()
Dim Lines As New List(Of String)
Try
' Open the file using a stream reader.
Using sr As New StreamReader("Config.txt")
Dim line As String
' Read the stream to a string and write the string to the console.
line = sr.ReadLine()
Do Until String.IsNullOrEmpty(line)
Lines.Add(line)
line = sr.ReadLine
Loop
End Using
Catch e As Exception
Console.WriteLine("The file could not be read:")
Console.WriteLine(e.Message)
End Try
Dim dbname As String = g_DatabaseName
Dim server As String = Lines.Where(Function(str) str.Contains("server =")).ToString
Dim user As String = ""
Dim password As String = ""
conn = New MySqlConnection
conn.ConnectionString = String.Format("server={0}; user id={1}; password={2}; database={3}; pooling=false; Convert Zero Datetime=True", server, user, password, dbname)
conn.Open()
End Sub
我尝试 return 文本文件中的一些字符串,因此我使用 StreamReader 读取文件并将它们存储到列表中。现在我尝试声明一个变量以从字符串列表中获取 "localhost",但下面的代码对我不起作用。
Dim server As String = Lines.Where(Function(str) str.Contains("server =")).ToString
Enumerable.Where
不是 return 单个字符串,但可能是多个字符串,使用 ToString
给你的不是第一个匹配行,而是类型的名称 System.Linq.Enumerable+WhereArrayIterator1[System.String]
.
要么声明为IEnumerable(Of String)
,要么用First
/FirstOrDefault
得到符合条件的第一行:
Dim serverLine As String = Lines
.Where(Function(str) str.Contains("server ="))
.FirstOrDefault()
您还可以使用 FirstOrDefault
的重载(Nothing
如果没有这一行):
Dim serverLine As String = Lines.FirstOrDefault(Function(str) str.Contains("server ="))
提取Localhost
:
Dim server As String = serverLine.Substring(serverLine.IndexOf("server =") + "server =".Length).Trim(""""c, " "c)