Avatar of jmpatton
 asked on

c# Substring Help

Im checking the substring of an item and I keep getting and index out of range error.

if (memberSequenceList.Items[sequencePosition].StatusFlags.Substring(17).Equals("1"))


The StatusFlags substring in question looks like this '0000000000000100000000'

Can someone help me get around this or point out what I may be doing wrong?


Avatar of undefined
Last Comment
Fernando Soto

8/22/2022 - Mon

how many items are in the memberSequenceList and how is the value sequencePosition set?
That's where your index out of bounds is coming from.

There are 26 members in memberSequenceList and the sequencePosition is 10.  

memberSequenceList.Items[10].StatusFlags = "00000000000001000000000000000000"

So thats why im so confused on the error.  Because im asking if (memberSequenceList.Items[10].StatusFlags.Substring(17).Equals("1")).

If its not equal to "1", I would think that it would just drop down to the next block of code.

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Fernando Soto

Hi jmpatton;

You state that StatusFlags is equal to this string "0000000000000100000000" and the command you are using is StatusFlags.Substring(17).Equals("1")). The SubString starting at index 17 of the string which is the 18th character in that string because strings are zero index based to the end of the string which makes Substring(17) equal to the string "00000" which will never equal a string of length of one character. Therefore the Substring method that you should use is this implementation SubString( StartIndex, Length ) therefore your statement should be this:

if (memberSequenceList.Items[sequencePosition].StatusFlags.Substring(17, 1).Equals("1"))


Open in new window

Hope that helps
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck