原始數據: 要處理為: 最終處理為: 好吧,我們創建一個類: class Ae { private string _InputValue; private char _Delimiter; public Ae(string inputValue, char delimiter) { this._In ...
原始數據:
string input = "3,7,2,8,1,9,1,34,67,78,22";
要處理為:
string[] stringArray = { "3", "7", "2", "8", "1", "9", "1", "34", "67", "78", "22" };
最終處理為:
int[] intArray = { 3, 7, 2, 8, 1, 9, 1, 34, 67, 78, 22 };
class Ae { private string _InputValue; private char _Delimiter; public Ae(string inputValue, char delimiter) { this._InputValue = inputValue; this._Delimiter = delimiter; } }Source Code
public string[] StringToStringArray() { return _InputValue.Split(new char[] { _Delimiter }, StringSplitOptions.RemoveEmptyEntries); }Source Code
public void StringArrayToIntArray() { string[] stringArray = StringToStringArray(); int length = stringArray.Length; int[] intArray = new int[length]; for (int i = 0; i < length; i++) { try { intArray[i] = Convert.ToInt32(stringArray[i]); } catch (Exception) { // ... } } }Source Code
如果你的.NET環境是3.0以上,有一個方法Array.ConvertAll<string, int>更加便捷:
public void StringArrayToIntArray() { string[] stringArray = StringToStringArray(); int length = stringArray.Length; int[] intArray = new int[length]; intArray = Array.ConvertAll<string, int>(stringArray, int.Parse); }Source Code