五、对.Net string类进行拓展

2022-08-29 15:33:35 浏览数 (1)

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
    }

0 人点赞