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

举报
CoderZ1010 发表于 2022/09/25 05:14:00 2022/09/25
【摘要】 Example: private string str = "Test"; private void Start() { bool isNullOrEmpty = str.IsNullOrEmpty(); bool isNullOrWhiteSpace = str.IsNullOrWhiteSpace(); ...

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:

    /// <summary>
    /// The extension of string.
    /// </summary>
    public static class StringExtension
    {
        /// <summary>
        /// The string value is null/empty or not.
        /// </summary>
        /// <param name="self"></param>
        /// <returns>return true if the string value is null or empty, otherwise retrun false.</returns>
        public static bool IsNullOrEmpty(this string self)
        {
            return string.IsNullOrEmpty(self);
        }
        /// <summary>
        /// The string value is null/white space or not.
        /// </summary>
        /// <param name="self"></param>
        /// <returns>return true if the string value is null or white space, otherwise return false.</returns>
        public static bool IsNullOrWhiteSpace(this string self)
        {
            return string.IsNullOrWhiteSpace(self);
        }        
        /// <summary>
        /// The string value contains chinese or not.
        /// </summary>
        /// <param name="self"></param>
        /// <returns>return true if the string value contains chinese, otherwise return false.</returns>
        public static bool ContainChinese(this string self)
        {
            return Regex.IsMatch(self, @"[\u4e00-\u9fa5]");
        }
        /// <summary>
        /// The string value is valid Email.
        /// </summary>
        /// <param name="self"></param>
        /// <returns>return true if the string value is valid email, otherwise return false.</returns>
        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})(\]?)$");
        }
        /// <summary>
        /// The string value is valid mobile phone number or not.
        /// </summary>
        /// <param name="self"></param>
        /// <returns>return true if the string value is valid mobile phone number, otherwise return false.</returns>
        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}$");
        }
        /// <summary>
        /// Uppercase the first char.
        /// </summary>
        /// <param name="self"></param>
        /// <returns>return an string which uppercase the first char by the string.</returns>
        public static string UppercaseFirst(this string self)
        {
            return char.ToUpper(self[0]) + self.Substring(1);
        }
        /// <summary>
        /// Lowercase the first char.
        /// </summary>
        /// <param name="self"></param>
        /// <returns>return an string which lowercase the first char by the string.</returns>
        public static string LowercaseFirst(this string self)
        {
            return char.ToLower(self[0]) + self.Substring(1);
        }
        #region Path
        /// <summary>
        /// Combine path.
        /// </summary>
        /// <param name="self"></param>
        /// <param name="toCombine"></param>
        /// <returns></returns>
        public static string PathCombine(this string self, string toCombine)
        {
            return Path.Combine(self, toCombine);
        }
        /// <summary>
        /// Get the sub files path in the directory.
        /// </summary>
        /// <param name="self"></param>
        /// <param name="recursive"></param>
        /// <param name="suffix"></param>
        /// <returns>return the list contains sub files path in the directory.</returns>
        public static List<string> GetDirSubFilePathList(this string self, bool recursive = false, string suffix = "")
        {
            List<string> retList = new List<string>();
            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
        /// <summary>
        /// Get the file is exists or not.
        /// </summary>
        /// <param name="self"></param>
        /// <returns>return true if the file is exists, otherwise return false.</returns>
        public static bool ExistsFile(this string self)
        {
            return File.Exists(self);
        }
        /// <summary>
        /// Delete the file if it is exists.
        /// </summary>
        /// <param name="self"></param>
        /// <returns>return true if the file is exists and delete it, otherwise return false.</returns>
        public static bool DeleteFileCarefully(this string self)
        {
            if (File.Exists(self))
            {
                File.Delete(self);
                return true;
            }
            return false;
        }
        /// <summary>
        /// Encrypt the file if it is exists.
        /// </summary>
        /// <param name="self"></param>
        /// <returns>return true if the file is exists and encrypt it, otherwise return false.</returns>
        public static bool EncryptFileCarefully(this string self)
        {
            if (File.Exists(self))
            {
                File.Encrypt(self);
                return true;
            }
            return false;
        }
        /// <summary>
        /// Decrypt the file if it is exists.
        /// </summary>
        /// <param name="self"></param>
        /// <returns>return true if the file is exists and decrypt it, otherwise return false.</returns>
        public static bool DecryptFileCarefully(this string self)
        {
            if (File.Exists(self))
            {
                File.Decrypt(self);
                return true;
            }
            return false;
        }
        #endregion

        #region Directory
        /// <summary>
        /// Create the directory if it is not exists.
        /// </summary>
        /// <param name="self"></param>
        /// <returns></returns>
        public static string CreateDirectoryCarefully(this string self)
        {
            if (!Directory.Exists(self))
            {
                Directory.CreateDirectory(self);
            }
            return self;
        }
        /// <summary>
        /// Delete the directory if it is exists.
        /// </summary>
        /// <param name="self"></param>
        public static void DeleteDirectoryCarefully(this string self)
        {
            if (Directory.Exists(self))
            {
                Directory.Delete(self);
            }
        }
        /// <summary>
        /// Delete the directory if it is exists.
        /// </summary>
        /// <param name="self"></param>
        /// <param name="recursive"></param>
        public static void DeleteDirectoryCarefully(this string self, bool recursive)
        {
            if (Directory.Exists(self))
            {
                Directory.Delete(self, recursive);
            }
        }
        /// <summary>
        /// Clear the directory if it is exists.
        /// </summary>
        /// <param name="self"></param>
        public static void ClearDirectoryCarefully(this string self)
        {
            if (Directory.Exists(self))
            {
                Directory.Delete(self, true);
            }
            Directory.CreateDirectory(self);
        }
        #endregion
    }

文章来源: coderz.blog.csdn.net,作者:CoderZ1010,版权归原作者所有,如需转载,请联系作者。

原文链接:coderz.blog.csdn.net/article/details/109056973

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。