達可達與我
我與達可達
posts - 36, comments - 28, trackbacks - 0
我的連結
藍色小舖
小舖部落格
Home
Contact
Syndication
Login
News
每月文章
December, 2007 (1)
November, 2007 (3)
October, 2007 (11)
September, 2007 (1)
April, 2007 (4)
March, 2007 (6)
February, 2007 (5)
January, 2007 (2)
December, 2006 (3)
隨筆分類
C#
(rss)
JavaScript
(rss)
SQL
(rss)
生活隨筆
(rss)
影像集
小舖Blog圖片
My New Blog
vlog 168
編程一路發
(rss)
Saturday, December 29, 2007
讀取CSV文字檔至陣列
早上看到C#版上的問題,也練習寫了一個不常用到二維的陣列,
在找維度大小時卡了一下...
陣列 => 維度大小 Rank ==>
int[,] =>2
int[,,] =>3
如果是維度各別的長度,則是用 GetUpperBound(維度),可取得最大的長度
參考看看 ^^
string[,] aryResult; using (StreamReader sr = new StreamReader("C:\\z.txt")) { //讀取文字檔 string txt = sr.ReadToEnd().Trim(); //有幾"列" string[] aryStr = txt.Split('\n'); int hight = aryStr.Length; //有幾"行" int width = aryStr[0].Split(' ').Length; //確定後再宣告陣列大小 aryResult = new string[hight, width]; //帶值進去 for (int h = 0; h < hight; h++) { string[] arySplit = aryStr[h].Split(' '); for (int w = 0; w < width; w++) { aryResult[h, w] = arySplit[w]; } } } //印出結果 for (int h = 0; h <= aryResult.GetUpperBound(0); h++) { for (int w = 0; w <= aryResult.GetUpperBound(1); w++) { Console.WriteLine("ary[{0},{1}]='{2}'", h, w, aryResult[h, w]); } }
posted @
10:15 AM
|
Feedback (0)