0

This is perhaps too easy but I do not know from where to start.

I have stings like "'02HEX'aspoodsasas'CR''LF'"

Now I want to extract stings between char(02) and char(12);

Until now I did following

string s = string.Format("{0}{1}{2}", (char)02, "12345678", (char)12);
int chindx = s.IndexOf((char)02)+1;
s = s.Substring(chindx, 8)

My problem is how to determine length of my substring if I know position of start character and position of my ending character in my string

2 Answers 2

8

Just subtract:

string middle = text.Substring(start, end - start);

(That's assuming you don't want the character at position end - if you do, just add one.)

For example:

string text = "hi there";
int start = 3; // 't'
int end = 6; // 'r'
string middle = text.Substring(start, end - start); // "the"
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I really need more sleep, When I see where I get stacked.
Thank you.But SubString(startPoint ,endPoint) often comes out instinctly.
1

A simple subtraction should do the trick. End position - Start position (minus the length of the first token)

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.