-2
string Idstr="ID03I010102010210AEMPD4677EID03I020102020208L8159734ID03I030102030210IPS1406974PT03T010109981815938030202PT03T0201109899488666030201PT03T0301109818159381030203PT03T040112919818159381030201";

string[] stringSeparators = new string[] { "ID03I0" };
string[] result;

result = IdStr.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);

This is the result:

result[0]=10102010210AEMPD4677E
result[1]=20102020208L8159734
result[3]=30102030210IPS1406974PT03T010109981815938030202PT03T0201109899488666030201PT03T0301109818159381030203PT03T040112919818159381030201

Desired result:

result[0]=ID03I010102010210AEMPD4677E
result[1]=ID03I020102020208L8159734
result[3]=ID03I030102030210IPS1406974PT03T010109981815938030202PT03T0201109899488666030201PT03T0301109818159381030203PT03T040112919818159381030201

As you can see I want to include delimiter ID03I0 to the elements.

NOTE: I know I can include it by hardcoding it. But that's not the way I want to do it.

cbr
  • 12,563
  • 3
  • 38
  • 63
SamuraiJack
  • 5,131
  • 15
  • 89
  • 195

2 Answers2

3
result = IdStr.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries)
    .Select(x => stringSeparators[0] + x).ToArray();

This adds the seperator to the beginning at every element within your array.

EDIT: Unfortunately with this approach you are limited to use just one single delimiter. So if you want to add more you´d use Regex instead.

MakePeaceGreatAgain
  • 35,491
  • 6
  • 60
  • 111
2

Following Regex pattern should work.

string input = "ID03I010102010210AEMPD4677EID03I020102020208L8159734ID03I030102030210IPS1406974PT03T010109981815938030202PT03T0201109899488666030201PT03T0301109818159381030203PT03T040112919818159381030201";  
string delimiter = "ID03I0";//Modify it as you need
string pattern = string.Format("(?<=.)(?={0})", delimiter);
string[] result = Regex.Split(input, pattern);

Online Demo

Adapted from this answer.

Community
  • 1
  • 1
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189