0
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.

Community
  • 1
  • 1
Karl
  • 410
  • 11
  • 25
  • 4
    Shorter often means less clear – Tim Schmelter Nov 29 '16 at 14:43
  • What happens if the line you're parsing look like, 1*234955555553829*10034902948. What exactly are you expecting as a result? – Shar1er80 Nov 29 '16 at 14:55
  • I updated the question with more details. I guess some RegEx is the key here. The parsing line has always the format like stated in the enhanced details. So an odd behaviour won't happen. I expect the result to be "A.Müller-Schulz" in this case. – Karl Nov 29 '16 at 15:01
  • Is the data you're working with constant? Meaning will what you're looking for always start with *1003? – Shar1er80 Nov 29 '16 at 15:23
  • Yes, data is constant. Always starts with *1003. – Karl Nov 29 '16 at 15:47

2 Answers2

2

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.

the_lotus
  • 12,668
  • 3
  • 36
  • 53
  • Sorry, my bad. Thanks for checking. +1 for the codereview hint, didn't know that `:)` – Karl Nov 30 '16 at 15:24
1

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

Shar1er80
  • 9,001
  • 2
  • 20
  • 29
  • Thanks for this substantial insight. I mark this as correct answer as it seems there is not shorter way to do this. Shorter (RegEx) would be definitely less clear. – Karl Nov 30 '16 at 15:26