日期:2014-05-18 浏览次数:20917 次
string str="1-2-3,4-5,6-7-8-9"; string[] splits = str.Split(','); List<string[]> strings = new List<string[]>(); foreach (string s in splits) { strings.Add(s.Split('-')); } string[][] stringArray = strings.ToArray();//可以转换为string[][]
------解决方案--------------------
一条语句是不行的,参考以下:
string s = "1-2-3,4-5,6-7-8-9"; string[] temp = s.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); string[][] result = new string[temp.Length][]; for (int i = 0; i < temp.Length; i++) { result[i] = temp[i].Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries); }
------解决方案--------------------
string s = "1-2-3,4-5,6-7-8-9"; string[]ss = new string[s.Split(',').Length]; string[][]sa = new string[ss.Length][]; ss = s.Split(','); for(int i=0;i<ss.Length;i++) { sa[i] = ss[i].Split('-'); }
------解决方案--------------------
var str = "1-2-3,4-5,6-7-8-9"; var strArray = str.Split(','); var result = new String[strArray.Length][]; for (var i = 0; i < strArray.Length; i++) { var subArray = strArray[i].Split('-'); result[i] = new string[subArray.Length]; for (var j = 0; j < subArray.Length; j++) { result[i][j] = subArray[j]; } }
------解决方案--------------------
protected void Page_Load(object sender, EventArgs e) { Label1.Text = "1-2-3,4-5,6-7-8-9"; Label2.Text = GetString(ddt()); } private string[][] ddt() { string str = "1-2-3,4-5,6-7-8-9"; string[] strArray = str.Split(','); string[][] result = new string[strArray.Length][]; for (int i = 0; i < strArray.Length; i++) { string[] subArray = strArray[i].Split('-'); result[i] = new string[subArray.Length]; for (int j = 0; j < subArray.Length; j++) { result[i][j] = subArray[j]; } } return result; } // {{1,2,3},{4,5},{6,7,8,9}} private string GetString(string[][] strArr) { string str = ""; for (int i = 0; i < strArr.Length; i++) { if (str != "") { str = str + "," ; } // end