Karl
Karl

Reputation: 410

How can this code be written shorter/clearer?

newRow("OrderReference") = line.Substring(line.IndexOf("*1003") + 5, line.IndexOf("*", line.IndexOf("*1003") + 5) - line.IndexOf("*1003") - 5)

There you have it. Very long and ugly. I was thinking about this:

Dim indexPlus = line.IndexOf("*1003") + 5
Dim indexMinus = line.IndexOf("*1003") - 5
newRow("OrderReference") = line.Substring(indexPlus, line.IndexOf("*", indexPlus) - indexMinus)

But that introduces new and meaningless vars. Unsatisfying.

Maybe RegEx is the savior here?

Unfortunately I mustn't change the input data :-(

The input data consist of the BWA-format (popular with books). Here you can see the part in question: enter image description here

All codes in this example set are required. Only corresponding values change.

Upvotes: 0

Views: 87

Answers (2)

Shar1er80
Shar1er80

Reputation: 9041

Given that your data is always constant, and what you're looking for always begins with "*1003", you don't need to use Regex (Even though you could). Just use what you're already using but with some corrections.

using System;

public class Program
{
    public static void Main()
    {
        string input = "L10113540   VD44444     VD2002100234949     000116161       04201261\r\n";
        input += "  KN00010000000129000LPEUR003000001*1003A.Muller-Schulz*1017Bastei\r\n";
        input += "Lubbe.61204 Laund.Meine Schuld*1019KL*102990300*1030NO*1032EUR*1131KT";

        int start = input.IndexOf("*1003");
        int end = input.IndexOf("*", start + 1);
        string result = input.Substring(start + 5, end - start - 5);

        Console.WriteLine(result);

        // Your code
        start = input.IndexOf("*1003") + 5;
        end = input.IndexOf("*1003") - 5;
        result = input.Substring(start, input.IndexOf("*", start) - end);

        Console.WriteLine(result);

    }
}

Result

A.Muller-Schulz
A.Muller-Schulz*1017Baste

You can see that what you posted in your question, doesn't give the results you want. All you're really looking for is just the next asterisk after the first "*1003". You can see the difference between your code and what I've given.

.NET Fiddle Example

Upvotes: 1

the_lotus
the_lotus

Reputation: 12748

I don't even think your second code works. It seems more like this.

Dim index = line.IndexOf("*1003") + 5
newRow("OrderReference") = line.Substring(index, line.IndexOf("*", indexPlus) - index)

10 - 5 - 2 isn't the same as 10 - (5 - 2) but instead it's the same as 10 - (5 + 2).

Next time, check out the codereview stack exchange.

Upvotes: 2

Related Questions