C#常用lib(內建)

C#知識系列

最近有一些內建的lib老是會忘記,乾脆把它整理起來

在Math 類別中我們將介紹以下常用的成員:
欄位
 PI:圓周率
 方法
      Max:兩數取大值
      Min:兩數取小值
      Pow:次方運算
      Round:銀行家捨入法
      Sqrt:開根號
      Truncate:無條件捨去小數位



        public class MathLib
        {
            public static void GetArea() => Console.WriteLine($"area={Math.PI * 5 * 5}");
            public static void GetMax() => Console.WriteLine($"max={ Math.Max(10, 25)}");
            public static void GetMin() => Console.WriteLine(Math.Min(10.28, 10.65));

            public static void GetRound()
            {
                Console.WriteLine($"進行銀行家捨入的最後一位數為小數第3位" + 10.674 + "->" + Math.Round(10.674, 2));
                Console.WriteLine($"進行銀行家捨入的最後一位數為小數第3位" + 10.675 + "->" + Math.Round(10.675, 2));
                Console.WriteLine($"進行銀行家捨入的最後一位數為小數第3位" + 10.676 + "->" + Math.Round(10.676, 2));
                Console.WriteLine($"進行銀行家捨入的最後一位數為小數第2位" + 10.674 + "->" + Math.Round(10.674, 1));
                Console.WriteLine($"進行銀行家捨入的最後一位數為小數第2位" + 10.675 + "->" + Math.Round(10.675, 1));
                Console.WriteLine($"進行銀行家捨入的最後一位數為小數第2位" + 10.676 + "->" + Math.Round(10.676, 1));
            }

            public static void GetSqrt() => Console.WriteLine(Math.Sqrt(10));

            public static void GetTruncate() => Console.WriteLine(Math.PI + "->" + Math.Truncate(Math.PI));

            public static void GetRandom()
            {
                Random rnd = new Random(); //實體化Random類別.
                string result = ""; //宣告Result變數準備儲存結果.
                for (int i = 0; i <= 9; i++) //儲存10個介於5~14的整數亂數.
                    result = result + rnd.Next(5, 15) + ",";
                Console.WriteLine(result);
            }
        }

Random類別裡最重要的就是產生亂數的Next 方法。

DateTime 本身是表示時間的瞬間,在建構方法中可以指定時間的值,而在DateTime 類別中我
們將介紹以下常用的成員:
建構方法
DateTime
屬性
      Date:取出日期
      Day:取出天數
      DayOfWeek:取出星期幾
      DayOfYear:取出在本年的第幾天
      Hour:取出小時部份
      Millisecond:取出毫秒部份
      Minute:取出分鐘部份
      Now:取出目前電腦系統的時間
      Second:取出秒部份
      Today:取出目前電腦系統的日期
      Year:取出年份部份
方法
      Add:增加一段時間間隔
      AddDays:增加天數
      AddHours:增加小時數
      AddMinutes:增加分鐘數
      AddMonths:增加月數
      AddSeconds:增加秒數
      AddYears:增加年數
      DaysInMonth:指定日期的天數
      IsLeapYear:判斷是否為閏年
      Parse:將字串轉型成DateTime 型別
      Subtract:減去一段時間間隔
      ToLongDateString:轉成完整日期格式
      ToLongTimeString:轉成完整時間格式
      ToShortDateString:轉成簡短時間格式
      ToShortTimeString:轉成簡短時間格式

    class DateTimeLib
    {
        public static void GetDayOfYear()
        {
            //取得DateTime 物件所在一年中的天數。
            //實體化DateTime時,指定2006年10月21日下午6時33分52秒.
            DateTime dt = new DateTime(2006, 10, 21, 18, 33, 52);
            Console.WriteLine($"原本DateTime:" + dt.ToString() + "\n" + "DayOfYear:" + dt.DayOfYear.ToString());
        }

        public static void GetHour()
        {
            //取得DateTime 物件所表示日期的小時部分。
            //實體化DateTime時,指定2006年10月21日下午6時33分52秒.
            DateTime dt = new DateTime(2006, 10, 21, 18, 33, 52);
            Console.WriteLine("原本的DateTime: " + dt.ToString()
                                              + "\n" + "Hour: " + dt.Hour.ToString()); //顯示Hour 結果
        }
        public static void GetMillisecond()
        {
            //取得DateTime 物件所表示日期的毫秒部分。
            DateTime dt = new DateTime();
            dt = DateTime.Now; //取得系統目前時間
            Console.WriteLine("目前電腦系統時間: " + dt.ToString()
                                           + "\n" + "Millisecond: " + dt.Millisecond.ToString());
            //顯示Millisecond 結果
        }

        public static void GetDatetime()
        {
            // 建構方法
            // DateTime()
            // DateTime(A, B, C, D, E, F)
            // DateTime(A, B, C, D, E, F, G)
            // 說明
            // 在實體化DateTime 物件時,可以:
            // 1.不指定日期
            // 2.指定A 年B 月C 日D 時E 分F 秒。
            // 3.指定A 年B 月C 日D 時E 分F 秒G 毫秒。
            //實體化DateTime時,指定2006年10月21日下午6時33分52秒300毫秒.
            DateTime dt = new DateTime(2006, 10, 21, 18, 33, 52, 300);
            Console.WriteLine(dt.ToString());
            //顯示dt,毫秒不會顯示.
        }

        public static void GetDate()
        {
            //取得DateTime 物件的日期部份,並將時間部分的值設定為上午12:00:00。
            //實體化DateTime時,指定2006年10月21日下午6時33分52秒.
            DateTime dt = new DateTime(2006, 10, 21, 18, 33, 52);
            Console.WriteLine(
                "原本的DateTime: " + dt.ToString()
                                + "\n" + "Date: " + dt.Date.ToString()); //顯示Date 結果
        }

        public static void GetDay()
        {
            //取得DateTime 物件所在月份的天數。
            //實體化DateTime時,指定2006年10月21日下午6時33分52秒
            DateTime dt = new DateTime(2006, 10, 21, 18, 33, 52);
            Console.WriteLine("原本的DateTime: " + dt.ToString()
                                              + "\n" + "Day: " + dt.Day.ToString());
        }
        public static void GetDayOfWeek()
        {
            DateTime dt = new DateTime(2006, 10, 21, 18, 33, 52);
            Console.WriteLine(
                "原本的DateTime: " + dt.ToString()
                                + "\n" + "DayOfWeek: " + dt.DayOfWeek.ToString());
        }
        public static void GetMinute()
        {
            //取得DateTime 物件所表示日期的分鐘部分。
            //實體化DateTime時,指定2006年10月21日下午6時33分52秒.
            DateTime dt = new DateTime(2006, 10, 21, 18, 33, 52);
            Console.WriteLine("原本的DateTime: " + dt.ToString()
                                              + "\n" + "Minute: " + dt.Minute.ToString()); //顯示Minute 結果
        }
        public static void GetNow()
        {
            //取得電腦上目前的日期和時間。
            DateTime dt = new DateTime();
            dt = DateTime.Now; //設定dt時間為目前電腦的時間
            Console.WriteLine("目前電腦系統時間: " + dt.ToString());
            //顯示目前電腦的時間
        }

        public static void GetSecond()
        {
            //取得DateTime 物件所表示日期的秒鐘部分。
            //實體化DateTime時,指定2006年10月21日下午6時33分52秒.
            DateTime dt = new DateTime(2006, 10, 21, 18, 33, 52);
            Console.WriteLine("原本的DateTime: " + dt.ToString()
                                              + "\n" + "Second: " + dt.Second.ToString()); //顯示Second 結果
        }

        public static void GetToday()
        {
            //取得電腦上目前的日期,並將時間部分的值設定為上午12: 00:00。
            Console.WriteLine("電腦上目前的日期:" + DateTime.Today.ToString());
        }


        public static void Method_Add()
        {
            //將指定的A 值加入至DateTime 物件的值,A 的資料型態為TimeSpan。
            //實體化DateTime時,指定2006年10月21日下午6時33分52秒.
            DateTime dt = new DateTime(2006, 10, 21, 18, 33, 52);
            //實體化TimeSpan時,指定時間間隔為1天2小時3分4秒.
            TimeSpan ts = new TimeSpan(1, 2, 3, 4); //
            Console.WriteLine(dt.ToString() + " + " + ts.ToString() +
                              " = " + dt.Add(ts).ToString()); //顯示dt 增加ts 的結果.
        }
        public static void Method_AddDay()
        {
            //將指定的天數A 加入至DateTime 物件的值,A 的資料型態必須為Double。
            //實體化DateTime時,指定2006年10月21日下午6時33分52秒.
            DateTime dt = new DateTime(2006, 10, 21, 18, 33, 52);
            Double A = 3.0; //宣告Double數值為3.0
            Console.WriteLine(dt.ToString() + " + " + A +
                              "天 = " + dt.AddDays(A).ToString()); //顯示dt 增加A 的結果.
        }
        public static void Method_AddHours()
        {
            //將指定的時數A 加入至DateTime 物件的值,A 的資料型態必須為Double。
            //實體化DateTime時,指定2006年10月21日下午6時33分52秒.
            DateTime dt = new DateTime(2006, 10, 21, 18, 33, 52);
            Double A = 4.0; //宣告DoubleA值為4.0
            Console.WriteLine(dt.ToString() +
                              " + " + A + "小時 = " + dt.AddHours(A).ToString());
            //顯示dt.AddHours(A)的結果.
        }

        public static void Method_AddMinutes()
        {
            //將指定的分鐘數A 加入至DateTime 物件的值,A 的資料型態必須為Double。
            //實體化DateTime時,指定2006年10月21日下午6時33分52秒.
            DateTime dt = new DateTime(2006, 10, 21, 18, 33, 52);
            Double A = 5.0; //宣告Double值為5.0
            Console.WriteLine(dt.ToString() + " + "
                                            + A + "分鐘 = " + dt.AddMinutes(A).ToString());
            //顯示dt.AddMinutes(A)的結果.
        }
        public static void Method_AddMonths()
        {
            //將指定的月份數A 加入至DateTime 物件的值,A 的資料型態必須為Int。
            //實體化DateTime時,指定2006年10月21日下午6時33分52秒.
            DateTime dt = new DateTime(2006, 10, 21, 18, 33, 52);
            int A = 3; //宣告整數A值為3.0
            //顯示dt.AddMonths(A)的結果.
            Console.WriteLine(dt.AddMonths(A).ToString());
        }

        public static void Method_AddSeconds()
        {
            //將指定的秒數A 加入至DateTime 物件的值,A 的資料型態必須為Double。
            //實體化DateTime時,指定2006年10月21日下午6時33分52秒.
            DateTime dt = new DateTime(2006, 10, 21, 18, 33, 52);
            Double A = 6.0; //宣告被精浮點數A值為6.0
            //顯示dt.AddSeconds(A)的結果.
            Console.WriteLine(dt.AddSeconds(A).ToString());
        }
        public static void Method_AddYears()
        {
            //將指定的年份數A 加入至DateTime 物件的值,A 的資料型態必須為Int。
            //實體化DateTime時,指定2006年10月21日下午6時33分52秒.
            DateTime dt = new DateTime(2006, 10, 21, 18, 33, 52);
            int A = 4; //宣告整數A值為4
            //顯示dt.AddHours(A)的結果.
            Console.WriteLine(dt.AddHours(A).ToString());
        }
        public static void Method_DaysInMonth()
        {
            //傳回指定之A 年B 月的天數,通常使用於求該月份有幾天。
            //顯示2006年2月有幾天的結果.
            Console.WriteLine(DateTime.DaysInMonth(2006, 2).ToString());
        }


        public static void Method_IsLeapYear()
        {
            //傳回指定年份A 是否為閏年,通常用於判斷該年是否是閏年。
            //顯示判斷2006年是否為閏年的結果.
            Console.WriteLine(DateTime.IsLeapYear(2006).ToString());
        }


        public static void Method_Parse()
        {
            //將指定字串A 轉換為DateTime 資料型別。
            DateTime dt = new DateTime(); //實體化DateTime資料型別.
            //利用DateTime.Parse將字串值"2006/10/21 下午06:33:52"轉成DateTime值.
            dt = DateTime.Parse("2006/10/21 下午06:33:52");
            Console.WriteLine(dt.ToString()); //顯示dt 的結果
        }

        public static void Method_Subtract()
        {
            //將DateTime 物件減去指定的時間或持續期間,A 的資料型態必須為DateTime 或TimeSpan。
            //實體化DateTime時,指定2006年10月21日下午6時33分52秒.
            DateTime dt1 = new DateTime(2006, 10, 21, 18, 33, 52);
            //實體化DateTime時,指定2006年10月18日下午1時23分42秒.
            DateTime dt2 = new DateTime(2006, 10, 18, 13, 23, 42);
            //顯示dt1減去dt2的結果.
            Console.WriteLine(dt1.Subtract(dt2).ToString());
        }

        public static void Method_ToLongDateString()
        {
            //將DateTime 物件的值轉換為完整日期字串表示。
            //實體化DateTime時,指定2006年10月21日下午6時33分52秒.
            DateTime dt = new DateTime(2006, 10, 21, 18, 33, 52);
            //顯示dt.ToString()與dt.ToLongDateString()的結果.
            Console.WriteLine(dt.ToString() + "\n" +
                            dt.ToLongDateString().ToString());
        }
  
        
        public static void Method_ToLongTimeString()
        {
            //將DateTime 物件的值轉換為完整時間字串表示。
            //實體化DateTime時,指定2006年10月21日下午6時33分52秒.
            DateTime dt = new DateTime(2006, 10, 21, 18, 33, 52);
            //顯示dt.ToString()與dt.ToLongTimeString()的結果.
            Console.WriteLine(dt.ToString() + "\n" +
                            dt.ToLongTimeString().ToString());
        }
  
        public static void Method_ToShortDateString()
        {
            //將DateTime 物件的值轉換為簡短日期字串表示
            //實體化DateTime時,指定2006年10月21日下午6時33分52秒.
            DateTime dt = new DateTime(2006, 10, 21, 18, 33, 52);
            //顯示dt.ToString()與dt.ToShortDateString()的結果.
            Console.WriteLine(dt.ToString() + "\n" +
                            dt.ToShortDateString().ToString());
        }
  
        public static void Method_ToShortTimeString()
        {
            //將DateTime 物件的值轉換為簡短時間字串表示。
            //實體化DateTime時,指定2006年10月21日下午6時33分52秒.
            DateTime dt = new DateTime(2006, 10, 21, 18, 33, 52);
            //顯示dt.ToString()與dt.ToShortTimeString()的結果.
            Console.WriteLine(dt.ToString() + "\n" +
                            dt.ToShortTimeString().ToString());
        }
  
        
    }

TimeSpan 是代表一段時間的間隔,TimeSpan 可以在建構方法中指定時間間隔,在本區中我們
將介紹以下TimeSpan的部分
建構方法
TimeSpan
屬性
    Days:取得天數
    Hours:取得小時數
    Milliseconds:取得毫秒數
    Minutes:取得分鐘數
    Seconds:取得秒數 
    TotalDays:將時間間隔轉換成總天數來表示
    TotalHours:將時間間隔轉換成總小時數來表示
    TotalMinutes:將時間間隔轉換成總分鐘數來表示
    TotalSeconds:將時間間隔轉換成總秒數來表示
方法
    Add:增加一段時間間隔
    Parse:轉換字串為TimeSpan 型別
    Subtract:減去一段時間間隔

        public class TimeSpanLib
        {
            public static void Method_Days()
            {
                //取得目前TimeSpan 物件所表示的天數。
                //實體化TimeSpan,值為10天9小時8分7秒6毫秒
                TimeSpan ts = new TimeSpan(10, 9, 8, 7, 6);
                //顯示ts與ts.Days的結果.
                Console.WriteLine(ts.ToString() +
                                "\n" + ts.Days.ToString());
            }
            public static void Method_Hours()
            {
                //取得目前TimeSpan 物件所表示的時數。
                //實體化TimeSpan,值為10天9小時8分7秒6毫秒
                TimeSpan ts = new TimeSpan(10, 9, 8, 7, 6);
                //顯示ts與ts.Hours的結果.
                Console.WriteLine(ts.ToString() +
                                "\n" + ts.Hours.ToString());
            }

            public static void Method_Milliseconds()
            {
                //取得目前TimeSpan 物件所表示的毫秒數。
                //實體化TimeSpan,值為10天9小時8分7秒6毫秒
                TimeSpan ts = new TimeSpan(10, 9, 8, 7, 6);
                //顯示ts與ts.Milliseconds的結果.
                Console.WriteLine(ts.ToString() +
                                "\n" + ts.Milliseconds.ToString());
            }

            public static void Method_Minutes()
            {
                //取得目前TimeSpan 物件所表示的分鐘數。
                //實體化TimeSpan,值為10天9小時8分7秒6毫秒
                TimeSpan ts = new TimeSpan(10, 9, 8, 7, 6);
                //顯示ts與ts.Minutes的結果.
                Console.WriteLine(ts.ToString() +
                                "\n" + ts.Minutes.ToString());
            }

            public static void Method_Seconds()
            {
                //取得目前TimeSpan 物件所表示的秒數。
                //實體化TimeSpan,值為10天9小時8分7秒6毫秒
                TimeSpan ts = new TimeSpan(10, 9, 8, 7, 6);
                //顯示ts與ts.Seconds的結果.
                Console.WriteLine(ts.ToString() +
                                "\n" + ts.Seconds.ToString());
            }

            public static void Method_TotalDays()
            {
                //取得目前TimeSpan 物件的值,這個值以整數和小數的天數表示。
                //實體化TimeSpan,值為10天9小時8分7秒6毫秒
                TimeSpan ts = new TimeSpan(10, 9, 8, 7, 6);
                //顯示ts與ts.TotalDays的結果.
                Console.WriteLine(ts.ToString() +
                                "\n" + ts.TotalDays.ToString());
            }

            public static void Method_TotalHours()
            {
                //取得目前TimeSpan 物件的值,這個值以整數和小數的小時數表示。
                //實體化TimeSpan,值為10天9小時8分7秒6毫秒
                TimeSpan ts = new TimeSpan(10, 9, 8, 7, 6);
                //顯示ts與ts.TotalHours的結果.
                Console.WriteLine(ts.ToString() +
                                "\n" + ts.TotalHours.ToString());
            }
            public static void Method_TotalMinutes()
            {
                //取得目前TimeSpan 物件的值,這個值以整數和小數的分鐘數表示。
                //實體化TimeSpan,值為10天9小時8分7秒6毫秒
                TimeSpan ts = new TimeSpan(10, 9, 8, 7, 6);
                //顯示ts與ts.TotalMinutes的結果.
                Console.WriteLine(ts.ToString() +
                                "\n" + ts.TotalMinutes.ToString());
            }

            public static void TotalSeconds()
            {
                //取得目前TimeSpan 物件的值,這個值以整數和小數的秒數表示。
                //實體化TimeSpan,值為10天9小時8分7秒6毫秒
                TimeSpan ts = new TimeSpan(10, 9, 8, 7, 6);
                //顯示ts與ts.TotalSeconds的結果.
                Console.WriteLine(ts.ToString() +
                                "\n" + ts.TotalSeconds.ToString());
            }
            public static void Method_Add()
            {
                //將指定的A 加入原本的TimeSpan 物件,A 的資料型態必須為TimeSpan。
                //宣告名稱為ts1的TimeSpan,值為10天9小時8分7秒6毫秒
                TimeSpan ts1 = new TimeSpan(10, 9, 8, 7, 6);
                //宣告名稱為ts2的TimeSpan,值為2天3小時4分5秒6毫秒
                TimeSpan ts2 = new TimeSpan(2, 3, 4, 5, 6);
                //顯示ts1與ts2與兩者相加的結果.
                Console.WriteLine("ts1: " + ts1.ToString() +
                                "\n" + "ts2: " + ts2.ToString() +
                                "\n" + "ts1+ts2: " + ts1.Add(ts2).ToString());
            }

            public static void Method_Parse()
            {
                //將字串S 的值轉換為TimeSpan 資料型別,字串S 的格式為A.B: C: D.E,
                //意義為A 天B 小時C 分D 秒E 毫秒,其中A 天的值範圍為0 ~10675199,
                //B 小時的範圍為0~23,C 分的範圍為0~59,D 秒的範圍為0~59,E 毫秒
                //位數限制為1~7 位數。
                //實體化TimeSpan,值為10天9小時8分7秒6毫秒
                TimeSpan ts = new TimeSpan();
                //利用TimeSpan.Parse設定ts的值為10天23小時59分59秒999毫秒
                ts = TimeSpan.Parse("10.23:59:59.999");
                //顯示ts的結果.
                Console.WriteLine(ts.ToString());
            }

            public static void Method_Subtract()
            {
                //將原本的TimeSpan 物件減去指定的A,A 的資料型態必須為TimeSpan。
                //宣告名稱為ts1的TimeSpan,值為10天9小時8分7秒6毫秒
                TimeSpan ts1 = new TimeSpan(10, 9, 8, 7, 6);
                //宣告名稱為ts2的TimeSpan,值為2天3小時4分5秒6毫秒
                TimeSpan ts2 = new TimeSpan(2, 3, 4, 5, 6);
                //顯示ts1與ts2與ts1-ts2的結果.
                Console.WriteLine("ts1: " + ts1.ToString() +
                                "\n" + "ts2: " + ts2.ToString() +
                                "\n" + "ts1-ts2: " + ts1.Subtract(ts2).ToString());
            }

        }

String 類別包括最常使用的字串相關函數,而以下將介紹String 類別中最常使用的成員:
方法
    Compare:比較兩字串大小
    Concat:連接字串
    Format:格式化字串
    IndexOf:搜尋子字串第一個符合條件的索引值
    Insert:插入字串
    Join:連接字串
    LastIndexOf:搜尋子字串最後一個符合條件的索引值
    Replace:替換字串
    Split:分隔字串
    Substring:擷取字串
    ToLower:轉換為英文小寫
    ToUpper:轉換為英文大寫
    Trim:去除頭尾空白字元

 

        public class StringLib
        {
          
            public static void Method_Compare()
            {
                //比較S1 與S2 兩個字串的大小,以字彙順序來比較。
                //回傳結果小於零:S1 小於S2。
                //回傳結果等於零:-S1 等於S2。
                //回傳結果大於零:-S1 大於S2。
                string str1 = "Lancy";
                string str2 = "Mai";
                string str3 = "Kevin";
                Console.WriteLine(string.Compare(str1, str2).ToString()
                                + "\n" + string.Compare(str2, str3).ToString());
                //‘ 輸出結果為-1跟1,因為"Lancy"的L小於"Mai"的M,"Mai"的M大於"Kevin"的K
            }
            public static void Method_Concat()
            {
                //回傳字串S1 與S2 的相連字串。
                string str1 = "Kevin";
                string str2 = "Mai";
                Console.WriteLine(string.Concat(str1, str2).ToString());
                //輸出將str1 與str2 相連的結果
            }

            public static void Method_Format()
            {
                //將物件Obj 利用格式字元F 來格式化,並傳回格式化後的字串。詳細內
                //容請參閱本書第5 章:陣列(Array)、字元(Char)和字串(String。
                double db = 5.6253;
                Console.WriteLine(string.Format("{0:F2}", db));
                //將倍精浮點數db 四捨五入至小數第2 位.
            }

            public static void Method_IndexOf()
            {
                //回傳字串中第一個符合指定字串S 的索引位置。
                string str = "Kevin is Mai's brother, and Mai is Andy's brother";
                Console.WriteLine(str.IndexOf("Mai").ToString());
            }

            public static void Method_Insert()
            {
                //用來指定目前字串的索引位置要插入指定的String 物件。
                string str = "Kevin Mai's brother, and Mai is Andy's brother";
                Console.WriteLine(str.Insert(5, " is").ToString());
            }

            public static void Method_Join()
            {
                //回傳將S2 字串陣列中的每個字串利用S1 符號連接起來的字串。
                string[] str = new string[3];
                string sep = "-";
                str[0] = "Kevin";
                str[1] = "Mai";
                str[2] = "Andy";
                Console.WriteLine(string.Join(sep, str));
                //執行結果會把Kevin,Mai,Andy 利用-號連接
            }
            public static void Method_LastIndexOf()
            {
                //回傳字串中最後一個符合指定字串S 的索引位置。
                string str = "Kevin is Mai's brother, and Mai is Andy's brother";
                Console.WriteLine(str.LastIndexOf("Mai").ToString());
                //最後一個符合Mai 的位置是28
            }

            public static void Method_Replace()
            {
                //將字串物件中所有符合S1 的字串都替換成S2 字串。
                string str = "Kevin is Mai's brother, and Mai is Andy's brother";
                str = str.Replace("Mai", "Tacheng");
                Console.WriteLine(str.ToString());
                //結果會將字串內Mai 全部替換成Tacheng
            }

            public static void Method_Split()
            {
                //用來分割目前執行個體中字串,並依據分隔字元C 來切割,需以「字串陣列型態」來接收回傳值。
                string str = "C#-VB-JAVA";
                string[] Split_str = str.Split('-');
                Console.WriteLine(Split_str[0]
                                + "\n" + Split_str[1]
                                + "\n" + Split_str[2]);
            }

            public static void Method_Substring()
            {
                //用來處理目前字串中從指定位置A 擷取指定長度B 的子字串。
                string str1 = "English ability is very important";
                string str2 = str1.Substring(8, 7);
                Console.WriteLine(str2);
                //從str1 的第8 個位置取出7 個長度的子字串
            }

            public static void Method_ToLower()
            {
                //將字串內英文字母全部轉換成小寫英文字母。
                string str = "English Ability Is Very Important";
                Console.WriteLine(str.ToLower());
                //將str 轉為小寫字母
            }

            public static void Method_ToUpper()
            {
                //將字串內英文字母全部轉換成大寫英文字母。
                string str = "English Ability Is Very Important";
                Console.WriteLine(str.ToUpper());
                //將str 轉為大寫字母
            }

            public static void Method_Trim()
            {
                //用來去除字串的開頭和結尾的「空白字元」,回傳扣除空白字元後的字串。
                string str1 = " Very good! ";
                string str2 = str1.Trim();
                Console.WriteLine(
                    "原先的字串:" + "[" + str1 + "]\n" +
                    "後來的字串:" + "[" + str2 + "]");
            }
        }

補充時間

        //取得目前週數
        public int GetWeekOfYear(DateTime dt)
        {
            GregorianCalendar GetWeek= new GregorianCalendar();
            return GetWeek.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, DayOfWeek.Monday);
        }

        //取得該年有多少週
        public int GetWeekAmount(int year)
        {
            DateTime end = new DateTime(year, 12, 31);  //該年最後一天
            System.Globalization.GregorianCalendar gc = new GregorianCalendar();
            return gc.GetWeekOfYear(end, CalendarWeekRule.FirstDay, DayOfWeek.Monday);  //該年的週數
        }
    class Program
    {
        static void Main(string[] args)
        {
            DateTime today = new DateTime(2021, 1, 4);

           
            int weekNo = GetWeekOfYear(today);
            Console.WriteLine("當週:" + weekNo);
            if (weekNo % 100 == 1)
            {
                Console.WriteLine("上週:" + GetWeekAmount(today.Year));
            }
            else {
                Console.WriteLine("上週:" + (weekNo - 1));
            }

            Console.ReadKey();
        }
        static  int GetWeekOfYear(DateTime dt)
        {
            GregorianCalendar gc = new GregorianCalendar();
            return Convert.ToInt32(dt.Year.ToString() +gc.GetWeekOfYear(dt, CalendarWeekRule.FirstDay, DayOfWeek.Monday).ToString().PadLeft(2,'0'));
        }

        static int GetWeekAmount(int year)
        {
            DateTime end = new DateTime(year, 12, 31);  //該年最後一天
            System.Globalization.GregorianCalendar gc = new GregorianCalendar();
            return Convert.ToInt32 (year.ToString() + gc.GetWeekOfYear(end, CalendarWeekRule.FirstDay, DayOfWeek.Monday).ToString().PadLeft(2, '0'));  //該年的週數
        }
    }
// 取小數點2位(無條件捨去)
Decimal result = Math.Floor(123.123456 * 100) / 100;
// output: 123.12

// 小數4位 (無條件捨去)
Decimal result = decimal.Truncate(123.123456 * 10000) / 10000;
// output: 123.1234

 

老E隨手寫