首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >五、对.Net string类进行拓展

五、对.Net string类进行拓展

作者头像
CoderZ
发布2022-08-29 15:33:35
发布2022-08-29 15:33:35
4680
举报

Example:

代码语言:javascript
复制
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:

代码语言:javascript
复制
/// <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
    }
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2020-10-17,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 当代野生程序猿 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档