2014年1月4日 星期六

ASP.NET C# 字串處理


1.串連兩個字串

範例:
    
public partial class Training1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string s1 = "This is : ";
            string s2 = "ASP.NET";
            Response.Write(s1 + s2);
        }
    }

結果:
This is : ASP.NET


2.大小寫轉換 ToUpper()(大寫)、ToLower()(小寫)

範例:

    
        protected void Page_Load(object sender, EventArgs e)
        {
            string bookName = "Visual C# for ASP.NET";
            Response.Write(bookName.ToUpper());
            Response.Write(bookName.ToLower());
        }
結果:
VISUAL C# FOR ASP.NET
visual c# for asp.net 



3.去除前後空白 Trim()

範例:

    
    public partial class Training1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string bookName = "    Visual C# for ASP.NET     ";
            Response.Write("["+bookName.Trim()+"]");
        }
    }
結果:
[Visual C# for ASP.NET] 
註:
Trim()只可以去除前後空字串,不可去除Visual C# for ASP.NET中的空白字元,如需要去除字串中的空字元須使用Replace()
若只想去除字串前的空白字串可使用TrimStart()
若只想去除字串前的空白字串可使用TrimEnd()



4.字串長度 Length.ToString()

範例:

    
    public partial class Training1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string bookName = "Visual C# for ASP.NET";
            Response.Write("字串長度 : "+bookName.Length.ToString());
        }
    }
結果:
字串長度 : 21


5.取出部分字串 Substring()

範例:

    
    public partial class Training1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string question = "Do you like ASP.NET";
            Response.Write(question.Substring(12));
        }
    }
結果:
ASP.NET?
註:
Substring()的第一個數字指定要從哪個字開始取出到最後一個字元,
以上面範例為從第13個字元取到最後一個字元,第一個位置為0。

如果要取出字串中區間字元的話則使用Substring(12,7)
則取出第13個字元到後面7個
範例:
    
    public partial class Training1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string question = "Do you like ASP.NET?";
            Response.Write(question.Substring(12,7));
        }
    }
結果:
ASP.NET


6.判斷某個字出現的位置  IndexOf()

範例:

    
    public partial class Training1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string companyName = "ASPNET Company";
            Response.Write("字母A出線的位置"+companyName.IndexOf("A"));
        }
    }
結果:
字母A出現的位置0 

註:
IndexOf()方法是會分大小寫的,因此把A換成a的話

    
    public partial class Training1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string companyName = "ASPNET Company";
            Response.Write("字母a出現的位置"+companyName.IndexOf("a"));
        }
    }
結果:
字母a出現的位置11

 若要IndexOf()不區分大小寫的話需使用CurrentCultureIgnoreCase

    
    public partial class Training1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string companyName = "ASPNET Company";
            Response.Write("字母a(不區分大小寫)出現的位置"+companyName.IndexOf("a",StringComparison.CurrentCultureIgnoreCase));
        }
    }

結果:
字母a(不區分大小寫)出現的位置0 


StringComparison.CurrentCultureIgnoreCase就是不區分大小寫


    
    public partial class Training1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string companyName = "ASPNET Company";
            Response.Write("字母A出現的位置"+companyName.IndexOf("A"));

            Response.Write("字母A出現的位置" + companyName.IndexOf("A",7));
        }
    }

結果:
字母A出現的位置0字母A出現的位置-1
註:
若IndexOf("A",7)則是從第8個字母開始找,如沒有找到則回傳-1

7.取代字串的內容  Replace()

範例:
    
    public partial class Training1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string companyName = "ASPNET Company";
            Response.Write(companyName.Replace("ASPNET","JAVA"));
        }
    }

結果:
JAVA Company

  • 使用Replace()可去除字串中所有的空白,以下為將空白替換成空字串,以達到空白的效果


    
    public partial class Training1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string s = "Visual    C#";
            Response.Write(s.Replace(" ", ""));
        }
    }
結果:
VisualC#

8.將字串分割成字串陣列  Split()


  • 字串EmpName裡放一堆英文名字,每個英文名字間都以逗號分隔。透過字串變數本身的Split(),將字串EmpName依逗號(',')進行切割。

範例:

    
    public partial class Training1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string EmpName = "Adams,Richard,Jerry,Sophie";
            string[] EmpArray = EmpName.Split(new char[]{','});
            Response.Write("陣列長度:"+EmpArray.Length.ToString());
            Response.Write(" ; ");
            Response.Write("第一個名字是:" + EmpArray[0]);
        }
    }
結果:
陣列長度:4 ; 第一個名字是:Adams

  • 取出最後一個單字,須先判斷最後一個索引值為多少
範例:

    
        protected void Page_Load(object sender, EventArgs e)
        {
            string EmpName = "Adams,Richard,Jerry,Sophie";
            string[] EmpArray = EmpName.Split(new char[]{','});
            Response.Write("陣列長度:"+EmpArray.Length.ToString());
            Response.Write(" ; ");
            Response.Write("最後一個名字是:" + EmpArray[EmpArray.Length-1]);
        }
結果:
陣列長度:4 ; 第一個名字是:Sophie

  • 將陣列轉換成字串則使用String.Join()方法
範例:

    
        protected void Page_Load(object sender, EventArgs e)
        {
            string[] EmpNames = {"Adams","Richard","Jerry","Sophie"};
            string Employees = string.Join(";", EmpNames);
            Response.Write(Employees);
        }
結果:
Adams;Richard;Jerry;Sophie

9.字串的格式化  

  • 以下面這段程式為例,最後若要將 i , j , total變數與一些字串混合顯示,Response.Write()時要穿插許多+運算子:
範例:

    
    public partial class Training1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int i = 10;
            int j = 20;
            int total = i + j;
            Response.Write(i.ToString() + "+" + j.ToString() + "運算結果為:" + total.ToString());
        }
    }
結果:
10+20運算結果為:30 

  • 這樣寫並沒有錯,但是Response.Write納行很亂,要變更顯示方法時得想一下才知道如何改。
  • String本身有提供一個Format()方法,他可以將多個變數格式化成指定的方法呈現。
範例:

    
        protected void Page_Load(object sender, EventArgs e)
        {
            int i = 10;
            int j = 20;
            int total = i + j;
            String s = String.Format("{0}+{1}運算結果為:{2}", i, j, total);
            Response.Write(s);
        }
結果:
10+20運算結果為:30 

  • 除了固定的字元外,還可看到用一些大括號括起來的數字,像是{0} , {1} , {2},這些就代表變數要出現的位置,例:{0}就是變數 i 。

沒有留言:

張貼留言