c# 4.0 - How to split a string without specifying any separator in asp.net with C#? -


i have string 1000 length. need split , assign different controls. not have character separator.
since each string assigning controls not contain same length. of doing using substring in specifying length. becoming hectic me length huge.

please suggest me there way split , assign in simpler way?

you can use string constructor:

 var input = "some 1000 character long string ...";  var inputchars = input.tochararray();   control1.value = new string(inputchars, 0, 100);   // chars 0-100 of input  control2.value = new string(inputchars, 100, 20);  // chars 100-120 of input  control3.value = new string(inputchars, 120, 50);  // chars 120-170 of input  ... 

or using substring:

 var input = "some 1000 character long string ...";   control1.value = input.substring(0, 100);   // chars 0-100 of input  control2.value = input.substring(100, 20);  // chars 100-120 of input  control3.value = input.substring(120, 50);  // chars 120-170 of input 

you this

var parts = new []  {      tuple.create(0, 100),      tuple.create(100, 20),      tuple.create(120, 50), }  var inputparts = parts.select(p => input.substring(p.item1, p.item2))                       .toarray(); control1.value = inputparts[0]; control2.value = inputparts[1]; control3.value = inputparts[3]; 

this makes easier maintain number of controls grows larger. can store list of 'parts' statically, can reused elsewhere in application without duplicating code.

if controls same type, can this:

 var parts = new []   {      new { control = control1, offset = 0, length = 100 },      new { control = control2, offset = 100, length = 20 },      new { control = control3, offset = 120, length = 50 },  }   foreach(var part in parts)  {      part.control.value = new string(inputchars, part.offset, offset.length);      // or part.control.value = input.substring(part.offset, offset.length);  } 

Comments