C#入門教程筆記

來源:互聯網
上載者:User

標籤:

1.C# string api

Substring(),//截取字串,接受兩個參數,第一個是位移量,第二個是截取長度
replace(),//替換字串,接受兩個參數,第一個指定被替換的字串,第二個指定被替換的字串
IndexOf(),//返回指定字元在字串中的位置,從0開始
//一個重載ToString()方法案例//Format將double轉換為stringstruct Location{    private double x;    private double y;    public Location(double x,double y)    {        this.x = x;        this.y = y;    }    public override string ToString()    {        return System.String.Format("Location:{0}degree,latitude:{1}degree", x, y);    }}
//一個存取控制字元readonly //常用在定義常量,與const 的區別在於,readonly在程式運行時被賦值,賦值之後程式運行中不能更改值

 

比較兩個未本地化字串的最好方法是使用具有 StringComparison.Ordinal(是否全等) 和 StringComparison.OrdinalIgnoreCase(忽略大小寫) 的 Equals 方法。

例如:

bool result = root.Equals(root2, StringComparison.Ordinal);            System.Console.WriteLine("{0} and {1},is{2}", root, root2, result ? ",equel." : ",not equel.");            result = root.Equals(root2, StringComparison.OrdinalIgnoreCase);//忽略大小寫            System.Console.WriteLine("{0}and{1},is{2}", root, root2, result ? ",equel." : ",not equel.");            //靜態方法也可用            bool isEquel = String.Equals(root, root2, StringComparison.Ordinal);            System.Console.WriteLine(isEquel.ToString());
CompareTo() //用於根據一個字串是小於 (<) 還是大於 (>) 另一個字串來返回一個整數值。比較字串時使用的是 Unicode 值,並且小寫值小於大寫的值。
Split(),//Split方法將字串拆分成帶分隔字元的char數組

例如:

string numString = "1287543"; //"1287543.0" will return false for a longlong number1 = 0;bool canConvert = long.TryParse(numString, out number1);if (canConvert == true)  Console.WriteLine("number1 now = {0}", number1);else  Console.WriteLine("numString is not a valid long");byte number2 = 0;numString = "255"; // A value of 256 will return falsecanConvert = byte.TryParse(numString, out number2);if (canConvert == true)  Console.WriteLine("number2 now = {0}", number2);else  Console.WriteLine("numString is not a valid byte");decimal number3 = 0;numString = "27.3"; //"27" is also a valid decimalcanConvert = decimal.TryParse(numString, out number3);if (canConvert == true)  Console.WriteLine("number3 now = {0}", number3);else  Console.WriteLine("number3 is not a valid decimal");

long的tryParse()方法:將數字轉換為64位有符號整型,返迴轉換是否成功;參數:

含有一個數字轉換字串。
        //
        //結果:
        //此方法返回時,包含了64位有符號整數等效
        //零,如果載於S,如果轉換成功的數量,或
        //轉換失敗。如果s參數為空白或System.String.Empty轉換失敗,
        //不是正確格式的,或者表示比System.Int64.MinValue以下的數
        //或大於System.Int64.MaxValue更大。這個參數傳遞初始化。

C#入門教程筆記

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.