String Array string [] to integer array Int [], String integer
Raw data:
string input = "3,7,2,8,1,9,1,34,67,78,22";
To process:
string[] stringArray = { "3", "7", "2", "8", "1", "9", "1", "34", "67", "78", "22" };
The final solution is:
int[] intArray = { 3, 7, 2, 8, 1, 9, 1, 34, 67, 78, 22 };
Okay, let's create a class:
Class AE {private string _ InputValue; private char _ Delimiter; public AE (string inputValue, char delimiter) {this. _ InputValue = inputValue; this. _ Delimiter = delimiter ;}}Source Code
Add a method to the class to convert a string to a string array:
Public string [] StringToStringArray () {return _ InputValue. Split (new char [] {_ Delimiter}, StringSplitOptions. RemoveEmptyEntries );}Source Code
Another method is to convert the string array to an integer array:
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
If your. NET environment is above 3.0, there is a method of Array. ConvertAll <string, int> which is more convenient:
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
Test the preceding method on the console: