1

I have a text file that I'm searching through and I want to skip the 10 first characters in every line.

Each line starts with [10:16:01], thats what I want to skip and I want to find the next number in the line and add the numbers together.

This is what I've managed to do so far

foreach (string line in lines)
{
    string a = line.Remove(0, 10);
    if (a.Contains("whateverimsearchingfor"))
    {

            string resultString = Regex.Match(a, @"\d+").Value;
            int result = Int32.Parse(resultString);

        total += result
 }

Thanks in advance!

Sorry when I try and build I get:

ArgumentOutOfRangeException was unhandled index and count must refer to a location within the string

It points to the string a = line.remove(0, 10)

Hope this is enough.

2
  • 1
    "It won't compile"? Why not tell us what line you're getting the error on? And maybe tell us what the error is. Commented Jun 25, 2013 at 21:06
  • 3
    Ahhhh ... so it compiles, but fails in execution. Please edit the question and remove the "it won't compile". Commented Jun 25, 2013 at 21:10

3 Answers 3

5

The problem appears to be that some of the lines in your file are too short, so line.Remove will fail because there aren't enough characters in the string to remove. Use this code to skip any lines that are too short to be processed:

foreach (string line in lines)
{
    if (line.Length < 10)
        continue;

    string a = line.Remove(0, 10);
    ...
}

Or this, if you had other processing below the code you posted, which you wanted to run even if the line was too short:

foreach (string line in lines)
{
    if (line.Length >= 10)
    {
        string a = line.Remove(0, 10);
        ...
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much, this works :) Gonna accept your answer in 2 mins! :D Got some other issues now but they should be manageable!
Hmm do you know if there is a limited line of strings i can use this code on? getting errors on more lines of strings.
@Winkz I'm sorry, I'm not sure what you're asking.
4

If you want to process only lines with the number:

foreach (string line in lines.Where(l => l.Length >= 10))

This skips lines that are shorter than 10 characters.

You are getting the ArgumentOutOfRangeException on string.Remove because(MSDN):

Either startIndex or count is less than zero. -or- startIndex plus count specify a position outside this instance.

Comments

0

I see you've accepted an answer but I'll leave this here anyways.

Your question is a bit ambiguous and it sounds like regexes aren't your only option. If you just want to skip the first 10 characters always, you can just use substrings and skip regexes. Look at this example here for sub strings.

Here's a simple example:

String input = "0123456789Hello World";
String sub = input.Substring(10, input.Length - 10);
Console.WriteLine(sub);

The first parameter is where you want to start your substring, and the second is the number of characters you want from the original.

In your code:

foreach(String line in lines)
{
     String sub = line.Substring(10, line.Length - 10);
}

Edit:

Or, you can use the simpler Substring call. The call will simply return the entire string after the given index.

foreach(String line in lines)
{
    String sub = line.Substring(10);
}

2 Comments

You said, remove actually changes the string. Actually, it doesn't. If you write string s2 = s1.Remove(0, 10); the resulting s2 contains a new string, but s1 is not changed. Strings are immutable. Also, if you want a substring that starts at position 10 and goes to the end of the string, you can just write string sub = line.Substring(10); No need to specify the length. See msdn.microsoft.com/en-us/library/hxthx5h6.aspx
@JimMischel I edited my post to reflect those suggestions. I should have looked at the documentation for .Remove.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.