Home
Manage Your Code
Snippet: Convert String To Array (C#)
Title: Convert String To Array Language: C#
Description: Converts a String To an Array Views: 4566
Author: Dan Kengott Date Added: 12/11/2006
Copy Code  
1		private static string[] ConvertToArray(string strIds, string separator) {
2			int size = (strIds != null && strIds.Length > 0)
3						? 1
4						: 0;
5			//Determine how many semi-colon separated values are in string
6			string temp = strIds;
7			int pos;
8			while (temp.IndexOf(separator) > -1) {
9				size++;
10				pos = temp.IndexOf(separator);
11				temp = temp.Substring(pos + 1).Trim();
12			}
13			string[] strArray = new string[size];
14			for (int i = 0; i < size; i++) {
15
16				pos = (strIds.Trim().IndexOf(separator) == -1)
17						? strIds.Trim().Length
18						: strIds.IndexOf(separator);
19				//Now get the string within the single quotes; trimming them off
20				string val = strIds.Trim().Substring(0, pos).Trim();
21				strArray[i] = val;
22				if (strIds.Length > (pos + 1)) {
23					strIds = strIds.Substring(pos + 1).Trim();
24				}
25			}
26			return strArray;
27		}
28