您当前的位置: 首页 >  unity

CoderZ1010

暂无认证

  • 3浏览

    0关注

    168博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Unity 使用this关键字进行函数拓展 - string

CoderZ1010 发布时间:2020-10-13 17:48:36 ,浏览量:3

Example:

private string str = "Test";

private void Start()
{
    bool isNullOrEmpty = str.IsNullOrEmpty();
    bool isNullOrWhiteSpace = str.IsNullOrWhiteSpace();
    bool containChinese = str.ContainChinese();
    bool isValidEmail = str.IsValidEmail();
    bool isValidMobilePhoneNumber = str.IsValidMobilePhoneNumber();
    string uppercaseFirst = str.UppercaseFirst();
    string lowercaseFirst = str.LowercaseFirst();

    string path = Application.streamingAssetsPath.PathCombine("readme.txt");
    bool existsFile = path.ExistsFile();
    bool deleteFileCarefully = path.DeleteFileCarefully();
}

Extension:

    /// 
    /// The extension of string.
    /// 
    public static class StringExtension
    {
        /// 
        /// The string value is null/empty or not.
        /// 
        /// 
        /// return true if the string value is null or empty, otherwise retrun false.
        public static bool IsNullOrEmpty(this string self)
        {
            return string.IsNullOrEmpty(self);
        }
        /// 
        /// The string value is null/white space or not.
        /// 
        /// 
        /// return true if the string value is null or white space, otherwise return false.
        public static bool IsNullOrWhiteSpace(this string self)
        {
            return string.IsNullOrWhiteSpace(self);
        }        
        /// 
        /// The string value contains chinese or not.
        /// 
        /// 
        /// return true if the string value contains chinese, otherwise return false.
        public static bool ContainChinese(this string self)
        {
            return Regex.IsMatch(self, @"[\u4e00-\u9fa5]");
        }
        /// 
        /// The string value is valid Email.
        /// 
        /// 
        /// return true if the string value is valid email, otherwise return false.
        public static bool IsValidEmail(this string self)
        {
            return Regex.IsMatch(self, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
        }
        /// 
        /// The string value is valid mobile phone number or not.
        /// 
        /// 
        /// return true if the string value is valid mobile phone number, otherwise return false.
        public static bool IsValidMobilePhoneNumber(this string self)
        {
            return Regex.IsMatch(self, @"^0{0,1}(13[4-9]|15[7-9]|15[0-2]|18[7-8])[0-9]{8}$");
        }
        /// 
        /// Uppercase the first char.
        /// 
        /// 
        /// return an string which uppercase the first char by the string.
        public static string UppercaseFirst(this string self)
        {
            return char.ToUpper(self[0]) + self.Substring(1);
        }
        /// 
        /// Lowercase the first char.
        /// 
        /// 
        /// return an string which lowercase the first char by the string.
        public static string LowercaseFirst(this string self)
        {
            return char.ToLower(self[0]) + self.Substring(1);
        }
        #region Path
        /// 
        /// Combine path.
        /// 
        /// 
        /// 
        /// 
        public static string PathCombine(this string self, string toCombine)
        {
            return Path.Combine(self, toCombine);
        }
        /// 
        /// Get the sub files path in the directory.
        /// 
        /// 
        /// 
        /// 
        /// return the list contains sub files path in the directory.
        public static List GetDirSubFilePathList(this string self, bool recursive = false, string suffix = "")
        {
            List retList = new List();
            var di = new DirectoryInfo(self);
            if (!di.Exists)
            {
                return retList;
            }
            var files = di.GetFiles();
            foreach (var file in files)
            {
                if (!string.IsNullOrEmpty(suffix))
                {
                    if(!file.FullName.EndsWith(suffix, StringComparison.CurrentCultureIgnoreCase))
                    {
                        continue;
                    }
                }
                retList.Add(file.FullName);
            }
            if (recursive)
            {
                var dirs = di.GetDirectories();
                foreach (var dir in dirs)
                {
                    retList.AddRange(GetDirSubFilePathList(dir.FullName, recursive, suffix));
                }
            }
            return retList;
        }
        #endregion

        #region File
        /// 
        /// Get the file is exists or not.
        /// 
        /// 
        /// return true if the file is exists, otherwise return false.
        public static bool ExistsFile(this string self)
        {
            return File.Exists(self);
        }
        /// 
        /// Delete the file if it is exists.
        /// 
        /// 
        /// return true if the file is exists and delete it, otherwise return false.
        public static bool DeleteFileCarefully(this string self)
        {
            if (File.Exists(self))
            {
                File.Delete(self);
                return true;
            }
            return false;
        }
        /// 
        /// Encrypt the file if it is exists.
        /// 
        /// 
        /// return true if the file is exists and encrypt it, otherwise return false.
        public static bool EncryptFileCarefully(this string self)
        {
            if (File.Exists(self))
            {
                File.Encrypt(self);
                return true;
            }
            return false;
        }
        /// 
        /// Decrypt the file if it is exists.
        /// 
        /// 
        /// return true if the file is exists and decrypt it, otherwise return false.
        public static bool DecryptFileCarefully(this string self)
        {
            if (File.Exists(self))
            {
                File.Decrypt(self);
                return true;
            }
            return false;
        }
        #endregion

        #region Directory
        /// 
        /// Create the directory if it is not exists.
        /// 
        /// 
        /// 
        public static string CreateDirectoryCarefully(this string self)
        {
            if (!Directory.Exists(self))
            {
                Directory.CreateDirectory(self);
            }
            return self;
        }
        /// 
        /// Delete the directory if it is exists.
        /// 
        /// 
        public static void DeleteDirectoryCarefully(this string self)
        {
            if (Directory.Exists(self))
            {
                Directory.Delete(self);
            }
        }
        /// 
        /// Delete the directory if it is exists.
        /// 
        /// 
        /// 
        public static void DeleteDirectoryCarefully(this string self, bool recursive)
        {
            if (Directory.Exists(self))
            {
                Directory.Delete(self, recursive);
            }
        }
        /// 
        /// Clear the directory if it is exists.
        /// 
        /// 
        public static void ClearDirectoryCarefully(this string self)
        {
            if (Directory.Exists(self))
            {
                Directory.Delete(self, true);
            }
            Directory.CreateDirectory(self);
        }
        #endregion
    }
关注
打赏
1653184800
查看更多评论
立即登录/注册

微信扫码登录

0.0494s