2

So I'm currently working on a program that reads a large txt file line by line as strings. For a particular section of the file the format is like this:

text1 : text2 : text3 : text4 : text5 : text6

I know how to split the string and find different counts.

What I want to do is check that the text in text5 starts with a certain expression SORT, and then for that line print text3.

foreach (string str in File.ReadLines(@"/filelocation"))
{
    if (str.StartsWith("fusedef"))
    {
        string text3 = str.Split(':')[2];
        string text5 = str.Split(':')[4];

    if (text5.StartsWith("SORT_"))
    {
        Console.WriteLine(text3);
    }
}

(As far as I know, counting a split string starts at 0 but correct me if I'm wrong, only started c# a few weeks ago. Thanks!)

5
  • Your split strings are all going to start with a space if your string actually is of the form "text 1 : text2 : text 3 : text 4" So you might want to remove those. Commented Jul 31, 2017 at 15:08
  • You also don't need to call split multiple times. Commented Jul 31, 2017 at 15:09
  • Where are you printing a count? Commented Jul 31, 2017 at 15:10
  • 1
    What is the exact problem? Where are you stuck? Please be clear, edit your question, and also give some example "fusedef" lines. See How to Ask. Commented Jul 31, 2017 at 15:12
  • The text in each string is not literally text1, text2 etc, it's work-sensitive so I cannot post it here. Thanks for the suggestions though I'll bear them in mind for the future @Stuart. Not printing a count, just wanted to make sure I was counting correctly @ Skintkingle Commented Jul 31, 2017 at 15:31

2 Answers 2

2

You need to remove any char that could potentially confuse the StartsWith. In particular those empty spaces before the string start.

There is an overload of string.Split that allows you to set more than one char to split on and then remove eventually empty strings returned by this split

string[] blocks = str.Split(new char[] {':', ' '}, StringSplitOptions.RemoveEmptyEntries);

if (blocks[4].StartsWith("SORT_"))
{
    Console.WriteLine(blocks[2]);
}

In alternative you could Trim the blocks strings

string[] blocks = str.Split(':');

if (blocks[4].Trim().StartsWith("SORT_"))
{
    Console.WriteLine(blocks[2]);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a million! This sorted my issues @Steve
0

Splitting the string will output a string array. To really make use of C# and how simple you can make things why not try operating on your string using a List?

If you know the order that items will appear in you can just call up a string from any position in your list.

    foreach (string str in File.ReadLines(@"/filelocation")) {
        if (str.StartsWith("fusedef")) {
            List<string> list = str.Split(':').ToList();
             if (list[5].StartsWith("SORT_")) 
                Console.WriteLine(list[3]);
        }
     }

Hope this helps!

Edit: you may want to remove the leading and trailing spaces from your separator. or split your string using ' : ' rather than ':'.

Comments

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.