检索文本文件中的特定数据(visual basic .NET) [英] Retrieve specific data in a text file (visual basic .NET)
问题描述
嗨!我一直有这个问题,而我似乎无法找到办法。我希望你能提供帮助。
所以,假设有一个名为的文本文件:
Games.txt
使命召唤:50
Assasins信条:23
看门狗:140
我的问题是,如果我想知道Assasins Creed的数量我该怎么做?
i希望论坛上有人我知道如何做到这一点,感谢你提前帮助。
我尝试过:
我试过通过知道字符串的长度来找到它。然后读取并删除前15个字符(
Assasins Creed:
)这将留下我:23.但这是一个非常糟糕的做法,而且我总是要知道确切的线。更好的解决方案是
它并不复杂,有几种方法可以做到。
您可以将文件读取为系列行,并找到以Assassins Creed:开头的行(string.StartsWith将告诉你)。剩下的就是你的号码。
您可以将整个文件读作一个字符串(File.ReadAllText会这样做),然后使用正则表达式查找你想要的数据:
私人 静态 Regex findIt = new 正则表达式(
(?< = Assasins Creed:\\\\ +)\\\\ +,
RegexOptions.Multiline
| RegexOptions.CultureInvariant
| RegexOptions.Compiled
);
...
匹配m = findIt.Match(textFromTheFile);
if (m.Success)
{
string theValueYouWanted = m.Value;
...
}无论如何,一旦有了数据,就可以使用int.TryParse将其转换为数字:
< pre lang =c#> int assassinsCreedCount;
if ( int .TryParse(theValueYouWanted, out assassinsCreedCount))
{
// 你知道了。
...
}
这个伪代码,你可能需要调整语法
1.打开File into streamreader
2.逐行循环
3.检查值并显示
Dim FILE_NAME 作为 字符串 = C:\ Users \Owner \Documents\Games.txt
如果 System.IO.File.Exists(FILE_NAME)= True 然后
Dim objReader As 新 System.IO.StreamReader(FILE_NAME)
Dim TextLine 作为 字符串
Dim LineArray()作为 字符串
Dim LineKey 作为 字符串
Dim LineValue As String
objReader.Peek()<> -1
TextLine = objReader.ReadLine()
LineArray = TextLine.Split(' :' )
LineKey = LineArray( 0 )
LineValue = LineArray( 1 )
' 如果LineKey =Assasins Creed,则LineValue将等于所需金额
循环
结束
Hi! Ive been having this issue, And i just cant seem to find a way to do it. I hope you can help.
So, lets say a have a text file called:
"Games.txt"
Call of Duty: 50
Assasins Creed: 23
Watch Dogs: 140
My question is, If i wanted to know the number of "Assasins Creed" How would i do it?
i hope someones on the forum knows how to do this, Thanks for you help in advance.
What I have tried:
I have tried finding it by knowing the line, length of the string. Then reading that and removing the first 15 charachters (
"Assasins Creed:"
) That would leave me with: 23. But this is a pretty bad way of doing it, And i always have to know the exact line. A better sollution would be
It's not complex, there are several ways you could do it.
You could read the file as a series of lines, and find the line starting with "Assassins Creed:" (string.StartsWith will tell you that). The rest of the line is your number.
You could read the whole file as a string (File.ReadAllText will do that) and then use a Regular Expression to find the data you want:
private static Regex findIt = new Regex( "(?<=Assasins Creed:\\s+)\\d+", RegexOptions.Multiline | RegexOptions.CultureInvariant | RegexOptions.Compiled ); ... Match m = findIt.Match(textFromTheFile); if (m.Success) { string theValueYouWanted = m.Value; ... }Wither way, once you have the data, you can use int.TryParse to convert it to a number:
int assassinsCreedCount; if (int.TryParse(theValueYouWanted, out assassinsCreedCount)) { // You got it. ... }
This pseudo-code, you may need to adjust syntax
1. Open File into streamreader
2. Loop through line by line
3. Check value and display
Dim FILE_NAME As String = "C:\Users\Owner\Documents\Games.txt" If System.IO.File.Exists(FILE_NAME) = True Then Dim objReader As New System.IO.StreamReader(FILE_NAME) Dim TextLine As String Dim LineArray() As String Dim LineKey As String Dim LineValue As String Do While objReader.Peek() <> -1 TextLine = objReader.ReadLine() LineArray = TextLine.Split(':') LineKey = LineArray(0) LineValue = LineArray(1) ' if LineKey = "Assasins Creed" then LineValue will equal the desired amount Loop End
这篇关于检索文本文件中的特定数据(visual basic .NET)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!