[ASP.NET] 在C#中模擬VB幾個常用的函數

摘要:[ASP.NET] 在C#中模擬VB幾個常用的函數

前言


在VB中有提供幾個常用的方法,例如 Left, Right, Mid等函數,但是在C#中並無直接提供這些函數

如C#要使用的話一種方法是自己寫一個,另一種是 using Microsoft.VisualBasic 命名空間

在下面提供幾個在C#中模擬出來的函數。

 

範例


Left(string, int)


        // 取得由左數來第N位字串
        public static string Left(string sSource, int iLength)
        {
            if (sSource.Trim().Length > 0)
                return sSource.Substring(0, iLength > sSource.Length ? sSource.Length : iLength);
            else
                return "";
                
        }

Right(string, int)


        // 取得由右數來第N位字串
        public static string Right(string sSource, int iLength)
        {
            if (sSource.Trim().Length > 0)
                return sSource.Substring(iLength > sSource.Length ? 0 : sSource.Length - iLength);
            else
                return "";
        }

Mid(string, int, int)


        // 取得起始位置至結束之字串
        public static string Mid(string sSource, int iStart, int iLength)
        {
            if (sSource.Trim().Length > 0)
            {
                int iStartPoint = iStart > sSource.Length ? sSource.Length : iStart;
                return sSource.Substring(iStartPoint, iStartPoint + iLength > sSource.Length ? sSource.Length - iStartPoint : iLength);
            }
            else
                return "";
        }

IsNumber(string)


        // 判斷是否為數字型態
        public static Boolean IsNumber(string sSource)
        {
            int flag = 0;
            sSource = sSource.Trim();
            if (sSource != "")
            {
                char[] chars = sSource.ToCharArray();
                for (int i = 0; i < chars.Length; i++)
                {
                    if (!char.IsNumber(chars[i]))
                    {
                        flag++;
                    }
                }
            }
            else
                flag = 1;
            if (flag > 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

IsDate(string)


        // 檢查是否為日期格式,驗證格式(YYYY/MM/DD,YYYYMMDD,YYY/MM/DD,YYYMMDD)
        public static Boolean IsDate(string myDate)
        {
            Boolean flag = true;
            //檢查是否空字串
            myDate = myDate.Trim();
            if (myDate == "")
                return false;
            //檢查是否為數字格式
            if (IsNumber(myDate.Replace("/","")) == true)
            {
                int Dlg = myDate.Length;
                switch (Dlg)
                {
                    case 7:
                        //民國年 YYYMMDD
                        try
                        {
                            int tmpY = Convert.ToInt32(Left(myDate, 3)) + 1911;
                            string tmpDate = tmpY + Right(myDate, 4);
                            DateTime dt = DateTime.ParseExact(tmpDate, "yyyyMMdd", null, System.Globalization.DateTimeStyles.AllowWhiteSpaces);
                        }
                        catch { flag = false; }
                        break;
                    case 8:
                        //西元年 YYYYMMDD
                        try
                        {
                            DateTime dt = DateTime.ParseExact(myDate, "yyyyMMdd", null, System.Globalization.DateTimeStyles.AllowWhiteSpaces);
                        }
                        catch { flag = false; }
                        break;
                    case 9:
                        //民國年 YYY/MM/DD
                        try
                        {
                            string tmpStr = myDate.Replace("/", "");
                            int tmpY = Convert.ToInt32(Left(tmpStr, 3)) + 1911;
                            string tmpDate = tmpY + Right(tmpStr, 4);
                            DateTime dt = DateTime.ParseExact(tmpDate, "yyyyMMdd", null, System.Globalization.DateTimeStyles.AllowWhiteSpaces);
                        }
                        catch { flag = false; }
                        break;
                    case 10:
                        //西元年 YYYY/MM/DD
                        if (myDate.IndexOf("/") == 4 || myDate.LastIndexOf("/") == 7)
                        {
                            try
                            {
                                DateTime dt = DateTime.Parse(myDate);
                            }
                            catch { flag = false; }
                        }
                        else { flag = false; }
                        break;
                    default:
                        flag = false;
                        break;
                }
            }
            else
            {
                flag = false;
            }
            return flag;
        }

Asc(string)


        // 取得字元對應的ASCII碼
        public static int Asc(string character)
        {
            if (character.Trim().Length == 1)
            {
                System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
                int intAsciiCode = (int)asciiEncoding.GetBytes(character)[0];
                return (intAsciiCode);
            }
            else
            {
                throw new ApplicationException("Character is not valid.");
            }
        }

Chr(int)


       // 取得ASCII碼對應字元
       public static string Chr(int asciiCode)
        {
            if (asciiCode >= 0 && asciiCode <= 255)
            {
                System.Text.ASCIIEncoding asciiEncoding = new System.Text.ASCIIEncoding();
                byte[] byteArray = new byte[] { (byte)asciiCode };
                string strCharacter = asciiEncoding.GetString(byteArray);
                return (strCharacter);
            }
            else
            {
                throw new ApplicationException("ASCII Code is not valid.");
            }
        }

 

 

 


以上文章敘述如有錯誤及觀念不正確,請不吝嗇指教
如有侵權內容也請您與我反應~謝謝您 :)