Billy
Billy

Reputation: 905

Split string from start and end positions

C# string

string str =@"
'START: Store

'START: Store.Car1
  Here sampel description of car 1
'END:Store.Car1

'START: Store.Car2
  Here sampel description of car 2
'END:Store.Car2

'END: Store"

Here, I want to get the string content between the 'START: Store and 'END: Store. Currently, I am using the the following code to split using Start:

string[] newString= str.Split(new string[]{
                        "'START:"
                    }, StringSplitOptions.None); 

How to split using start and end positions? Thanks in advance!

Upvotes: 0

Views: 1040

Answers (1)

adolja
adolja

Reputation: 189

string str =@"
'START: Store

'START: Store.Car1


Here sampel description of car 1
'END:Store.Car1

'START: Store.Car2
  Here sampel description of car 2
'END:Store.Car2

'END: Store";

var startString: = "'START: Store";
var startIndex = str.IndexOf(startString) + startString.Length;
var endIndex = str.LastIndexOf("'END: Store");
var concatedString = str.Substring(startIndex, endIndex - startIndex);

Upvotes: 4

Related Questions