Like many other answers, using StringBuilder directly or indirectly with string.Join will be much better. However, in your Split and Join example, I think we can do even better.
// first, find the n-th (totalremove-th) separating space. int index = 0; for(int i = 0; i < totalremove; i++) { index = infoseperated.IndexOf(' ', index + 1); if(index == -1) return ""; // eg, asked to remove 5 values, but only 3 in the string. } // return everything after that point. return infoseperated.Substring(index + 1);
As long as the infoseperated object has no double spaces or whitespace at the beginning or something like that, it will be more efficient than splitting and reassembling the string.
source share