前天有個台大土木系的問我C++
今天又有個不知道哪裡土木系的問我C#...
到底土木系是在做什麼大事業我完全搞不懂啊orz
我在想如果沒意外的話大概星期四還會有個土木系來問我怎樣學界王拳
不過在學界王拳之前要先學會講笑話
但笑話這種東西是很主觀的
到底是界王好笑還是俉空好笑
就像醫學院的學生指著心電圖說哈哈哈好好笑
或是工程師看到一個function裡有67個if在那科科科智障
所以我想說的其實是...
嗯好...離題了...讓我們回來做正事
可能有人會問啦
"啊你上回不是說大家都籨int開始教嗎,為什麼這次是字串?"
傻孩子
那麼容易被你猜到我還要當食神嗎
不過我還是有自己的理由
你在程式裡面看到的所有文字幾乎都是string格式
而string是由一堆char所堆起來的
所以我們先來介紹這兩個
首先打開我們之前上次的網頁
先打開VS Express for Web
左上角選檔案->最近使用過的專案與方案
上次我們的方案是c:\....(一堆表示你資料夾的文字)\WebSite1.sln
點選開啟他
然後我們在方案總管找出來Default.aspx.cs這支檔案 點它兩下
上次我們的檔案應該長這樣子
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void btnConfirm_Click(object sender, EventArgs e) { lblWelcome.Text = "你好! "+txtName; } }
現在我們在這行
protected void Page_Load(object sender, EventArgs e)上方輸入這個
string hello="你好! ";
看起來會像這樣
public partial class _Default : System.Web.UI.Page { string hello="你好! "; protected void Page_Load(object sender, EventArgs e) { } protected void btnConfirm_Click(object sender, EventArgs e) { lblWelcome.Text = "你好! "+txtName; } }
讓我們來解釋一下
string hello="你好! ";
這行代表了什麼
string是字串的意思
hello是這個字串我們給他的名稱
你要叫他helloX或是HelluJudy都可以
前面代表 我們宣告名為hello的變數他的型態是string
=就是等於
"你好! " 代表 字串 你好!
是的 就是兩個字一個驚嘆號跟一個空格 你好!
我們把這行翻譯成中文的話就是
名叫做hello的字串他其實是你好!
有一點頭昏了嗎?
那接下來你有得昏了
接著我們把
lblWelcome.Text = "你好! "+txtName.Text;
這行的"你好! "
換成這樣
lblWelcome.Text = hello+txtName.Text;
然後現在我們按F5執行偵錯
你會發現
跟原來一模一樣啊
還是 你好! XXX 這樣
不過那個你好已經不是我們原本直接塞字進去 而是使用字串了
回到VS express我們把偵錯停止
(shift+F5或是直接點VS Express上面的紅色方塊)
接下來我們在
string hello="你好! ";
下一行加上
string welcome;
這行代表著
我們宣告了一個名為welcome的字串
不過他沒有任何內容
如果我們直接拿來使用的話他會爆炸
所以我們在
protected void btnConfirm_Click(object sender, EventArgs e) { lblWelcome.Text = "你好! "+txtName; }
這裡面
lblWelcome.Text = "你好! "+txtName;
這一行之前加上
welcome = " 歡迎你";
這樣我們就有給welcome內容了
所以名為welcome的字串他現在代表的是 歡迎你
空格加上三個中文
之後在
lblWelcome.Text = "你好! "+txtName;
之後加上welcome
像這樣
lblWelcome.Text = hello+txtName.Text+welcome;
我們再按一次F5來偵錯
我們在後面多加上了歡迎你了
全部的aspx.cs檔大概長的像這樣子
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : System.Web.UI.Page { string hello="你好! "; string welcome; protected void Page_Load(object sender, EventArgs e) { } protected void btnConfirm_Click(object sender, EventArgs e) { welcome = " 歡迎你"; lblWelcome.Text = hello+txtName.Text+welcome; } }
我們現在幾乎可以在這個網頁上顯示任何優雅(或低級)的文字了
好了 再十分鍾要下班了
今天就到這裡為止了
這就是基本的字串操作
如果有任何不懂歡迎聯絡我
各位再會
沒有留言:
張貼留言