views:

189

answers:

2

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

+4  A: 

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"
Jon Skeet
Thanks, I really need more sleep, When I see where I get stacked.
adopilot
+1  A: 

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

Simon