从开始和结束位置分割字符串

时间:2019-04-29 07:30:29

标签: c# asp.net

C#字符串

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"

在这里,我想获取“ START:存储”和“ END:存储”之间的字符串内容。目前,我正在使用以下代码通过“开始”进行拆分:

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

如何使用开始位置和结束位置进行拆分?预先感谢!

1 个答案:

答案 0 :(得分:3)

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);