我一直在尝试导入一个文本文件,确切地说是.po格式,其中包含常规文本。
[文字示例]
#:C:\ Users \ andrei \ Desktop \ sitemile_themes \ ProjectTheme / archive-project.php:69
#:C:\ Users \ andrei \ Desktop \ sitemile_themes \ ProjectTheme / taxonomy.php:108 msgstr“所有已发布项目”
msgstr“Всезапросы”#:C:\ Users \ andrei \ Desktop \ sitemile_themes \ ProjectTheme / archive-project.php:70
#:C:\ Users \ andrei \ Desktop \ sitemile_themes \ ProjectTheme / taxonomy.php:109 #,php-format
msgstr“%s中最新发布的项目” msgstr“Последниезапросыв%s”
导入后,我需要浏览导入的文本,找到以msgstr“example”和msgstr“example 2”开头的字符串,然后将它们导入到listview1中,它有2列,因为msgid是一个项目,而msgstr是一个子项目,在导入后会是这样的。
[示例Listview1]
Column One | Column Two
Example | Example 2
--------------------------
并导入'msgid'和'msgstr'下引号中的每个字符串 我将文本导入vb.net表单没有问题,但我似乎无法通过导入的文本并拔出'msgid'示例“'和'msgstr”示例2“'字符串。任何人都知道如何实现这个目标?
编辑:对不起,我忘了显示我目前正在尝试使用的代码! 要导入文件,我使用此代码 Dim fileReader As System.IO.StreamReader
fileReader =
My.Computer.FileSystem.OpenTextFileReader("C:\Users\pasha\Desktop\en_US.po")
Dim stringReader As String
stringReader = fileReader.ReadToEnd()
TextBox1.Text = stringReader
哪个正常,但由于某种原因,它将它放在我的文本框中(启用了多行),忽略了原始文本文件所包含的行。
对于扫描字符串我使用
For Each line In TextBox1.Text
If line.ToString.Contains("msgid") Then
ListView1.Items.Add(line.ToString.Replace("msgid ", ""))
End If
Next
但由于某些原因,这段代码甚至没有添加它找到的单行,我认为它甚至根本找不到任何东西。很抱歉新手编码,我是所有这一切的新手。
答案 0 :(得分:0)
这将假设你总是拥有" msgid"和" msgstr"同时。你可以通过检查第一个"所以,如果你有额外的空格,它不会影响结果。如果它总是那样,那么你可以像那样使用它。
Dim fileReader As System.IO.StreamReader
fileReader = My.Computer.FileSystem.OpenTextFileReader("C:\dump\test.po")
Dim stringReader As String = ""
stringReader = fileReader.ReadToEnd()
Dim msgList As New List(Of KeyValuePair(Of String, String))
Do Until InStr(stringReader.ToLower, "msgid") = 0
Dim xmsgid As String = ""
Dim xmsgstr As String = ""
stringReader = Strings.Mid(stringReader, InStr(stringReader.ToLower, "msgid") + 7)
xmsgid = Strings.Left(stringReader, InStr(stringReader, Chr(34)) - 1)
stringReader = Strings.Mid(stringReader, InStr(stringReader.ToLower, "msgstr") + 8)
xmsgstr = Strings.Left(stringReader, InStr(stringReader, Chr(34)) - 1)
msgList.Add(New KeyValuePair(Of String, String)(xmsgid, xmsgstr))
Loop
在字符串中走得更慢,但至少你看到了这个概念。
Dim fileReader As System.IO.StreamReader
fileReader =
My.Computer.FileSystem.OpenTextFileReader("C:\dump\test.po")
Dim stringReader As String = ""
stringReader = fileReader.ReadToEnd()
Dim msgList As New List(Of KeyValuePair(Of String, String))
Do Until InStr(stringReader.ToLower, "msgid") = 0
Dim xmsgid As String = ""
Dim xmsgstr As String = ""
stringReader = Strings.Mid(stringReader, InStr(stringReader.ToLower, "msgid"))
stringReader = Strings.Mid(stringReader, InStr(stringReader.ToLower, Chr(34)) + 1)
xmsgid = Strings.Left(stringReader, InStr(stringReader, Chr(34)) - 1)
stringReader = Strings.Mid(stringReader, InStr(stringReader.ToLower, "msgstr"))
stringReader = Strings.Mid(stringReader, InStr(stringReader.ToLower, Chr(34)) + 1)
xmsgstr = Strings.Left(stringReader, InStr(stringReader, Chr(34)) - 1)
msgList.Add(New KeyValuePair(Of String, String)(xmsgid, xmsgstr))
Loop