0

I am in need of parsing an array or characters that is a fixed length but can have just about any combination of letter or number. My 50 digit array looks like this: NL1NAMEOFCO-B032144221111000100600000-A35499001

This array represents a vast combination of settings within our product. I need to extract all reference designators in the array. The first 3 characters represent a particular model NL1, the next 8 characters represent a company NAMEOFCO. The ‘-‘ will always be in the same location. The B (digit 13) represents some value, etc, etc. Also, some values are represented by 2 digits. Digits 20 & 21 (which store the value 22), represent some specific settings.

So by now you get the idea. I can parse the array and extract the values I need by using the following code:

String Company = ConfigCode[3].ToString() +
                 ConfigCode[4].ToString() + 
                 ConfigCode[5].ToString() + 
                 ConfigCode[6].ToString() + 
                 ConfigCode[7].ToString() + 
                 ConfigCode[8].ToString() +
                 ConfigCode[9].ToString() +
                 ConfigCode[10].ToString();

This works without any problems, but to me, there should be an easier way of doing this. I would have thought the following would work, but it does not.

String Company = ConfigCode[3..10].ToString();

Can someone explain to me why it doesn’t work and what would be a better way of extracting the information I need?

Thanks!

1
  • 4
    string.Substring(startIndex, length) is a bit nicer. Commented Dec 17, 2014 at 14:31

2 Answers 2

2

I believe that String.Substring method is what you're looking for. The signature for the overloaded method you're looking for is:

public string Substring(
    int startIndex,
    int length

)

The documentation for it is here: http://msdn.microsoft.com/en-us/library/aka44szs(v=vs.110).aspx

For example, your Company name would be (going by the description of a character length of 8):

string CompanyName = configCode.Substring(3, 8);

Sign up to request clarification or add additional context in comments.

3 Comments

Not length of 8? The code shows 6, but the question mentions 8 chars.
@ryanyuyu good catch, I was going by the code :] I'll update
Thanks! Substring is exactly what I was looking for. Also corrected mistake in question. Thanks again everyone!
0

Like mentioned before, you can use the Substring extension method like so:

String Company = ConfigCode.Substring(3, 8);

The square-bracket operators for strings, like in ConfigCode[3], actually return individual chars at that specific index. And C# isn't as pretty as other programming languages where stuff like array[3..10] actually gives you a portion of an array (or in this case, a string).

1 Comment

Interesting. Thanks for the info!

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.