ExtensionMethod.NET Home of 875 C#, Visual Basic, F# and Javascript extension methods

Split

Extension method to split string by number of characters.

Source

/// <summary>
		/// Extension method to split string by number of characters.
		/// </summary>
		/// <param name="startindex">The zero-based position to split the specified string.</param>
		/// <param name="length">The number of characters to split</param>
		public static string[] Split(this string str, int startindex, int length)
		{
			string[] strrtn = new string[3];
			if (startindex == 0)
				strrtn = new string[] { str.Substring(startindex, length), str.Remove(startindex, length) };
			else if (startindex > 0)
				strrtn = new string[] { str.Substring(startindex, length), str.Substring(0, startindex), str.Remove(0, length + startindex) };
			return strrtn;
		}

Example

string[] A = "Ahmer-Sohail-Shamsi".Split(6, 6);
MessageBox.Show(A[0]);
MessageBox.Show(A[1]);
MessageBox.Show(A[2]);

/*
OUTPUT:

Sohail
Ahmer-
-Shamsi
*/

Author: Ahmer Sohail

Submitted on: 14 sep 2017

Language: C#

Type: System.String

Views: 4487