Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 29-Aug-18 5:34am
Comments
M4rLask 29-Aug-18 10:36am    
Try using an XML file, so just read the value of the game node
ZurdoDev 29-Aug-18 10:39am    
You can use the Split function on String and split on the ":" and get the second.
String someNumber = strLineInFile.Split(':')[1].Trim(); // more or less.

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:
C#
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:
C#
int assassinsCreedCount;
if (int.TryParse(theValueYouWanted, out assassinsCreedCount))
   {
   // You got it.
   ...
   }
 
Share this answer
 
Comments
CaseBody 29-Aug-18 11:25am    
Hi, Thank you for your response!
I tried adding it to my code, But all i got where loads of errors. Is there anything i need to import?

CaseBody
OriginalGriff 29-Aug-18 11:47am    
Oh. You don't use C# ...
Try this:
Private Shared findIt As Regex = New Regex("(?<=Assasins Creed:\s+)\d+", RegexOptions.Multiline Or RegexOptions.CultureInvariant Or RegexOptions.Compiled)
And this:
Dim m As Match = findIt.Match(textFromTheFile)

If m.Success Then
Dim theValueYouWanted As String = m.Value
...
End If
And
Dim theValueYouWanted As String = "hhh"
Dim assassinsCreedCount As Integer

If int32.TryParse(theValueYouWanted, assassinsCreedCount) Then
...
End If
CaseBody 29-Aug-18 11:57am    
Hi, thank you for the fast response! Still alot of errors sadly, If you would like me to send a screenshot of the errors or whatever let me know
OriginalGriff 29-Aug-18 12:11pm    
Never screenshot errors. Look at them, think about exactly what it is saying, double click on the first one, and look for why it's saying what it's saying.

You'll never get anywhere without doing that!
It's almost certainly that you've just gone "copy" and "paste", without doing "think" in the middle. :laugh:
CaseBody 29-Aug-18 12:19pm    
Hey, your pretty correct about the copy-pasting.
But basicly everything is an error so i dont realy know where to start
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

VB
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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900