Loading [MathJax]/jax/output/CommonHTML/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >五、对.Net string类进行拓展

五、对.Net string类进行拓展

作者头像
CoderZ
发布于 2022-08-29 07:33:35
发布于 2022-08-29 07:33:35
38600
代码可运行
举报
运行总次数:0
代码可运行

Example:

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
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
代码运行次数:0
运行
AI代码解释
复制
/// <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 删除。

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

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
C#文件安全管理解析
彭泽0902
2018/01/04
1.6K0
C#常用的IO操作方法
public class IoHelper { /// <summary> /// 判断文件是否存在 /// </summary>
彭泽0902
2018/01/04
9380
C#目录文件复制、创建操作
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.IO; usi
跟着阿笨一起玩NET
2018/09/19
2.3K0
C# WinForm实现自动更新程序的案例分享
string runPath = Process.GetCurrentProcess().MainModule.FileName;
用户7718188
2022/11/06
8900
C#文件帮助类FoderHelper
using System; using System.Data; using System.Configuration; using System.Linq; using System.Web; using System.IO; using System.Collections; using System.Collections.Generic; using System.Security.AccessControl;
用户7108768
2021/11/03
3580
[C#] 常用工具类——文件操作类
/// <para> FilesUpload:工具方法:ASP.NET上传文件的方法</para> /// <para> FileExists:返回文件是否存在</para> /// <para> IsImgFilename:判断文件名是否为浏览器可以直接显示的图片文件名</para> /// <para> CopyFiles:复制指定目录的所有文件</para> /// <para> MoveFiles:移动指定目录的所有文件</para> /// <para> D
跟着阿笨一起玩NET
2018/09/19
2.7K0
C#利用SharpZipLib生成压缩包
SharpZipLib是一个开源的C#压缩解压库,应用非常广泛。就像用ADO.NET操作数据库要打开连接、执行命令、关闭连接等多个步骤一样,用SharpZipLib进行压缩和解压也需要多个步骤。SharpZipLib功能比较强大,在很多C#的应用中,都有它的身影,我们可以通过引入SharpZipLib类库文件,在程序中实现自动压缩文件以及解压缩文件的功能,例如一个常见的情景就是用户客户端程序下载更新包,下载完成之后,在本地自动解压文件。
郑子铭
2023/02/12
8960
C#利用SharpZipLib生成压缩包
.NET Core 3.0之深入源码理解Configuration(二)
上一篇文章讨论了Configuration的几个核心对象,本文继续讨论Configuration中关于文件型配置的相关内容。相比较而言,文件型配置的使用场景更加广泛,用户自定义配置扩展也可以基于文件型配置进行扩展。如果需要查看上一篇文章,可以点击移步。
AI.NET 极客圈
2019/07/19
7440
.NET Core 3.0之深入源码理解Configuration(二)
最好的.NET开源免费ZIP库DotNetZip(.NET组件介绍之三)
彭泽0902
2018/01/04
3.3K0
C#常用操作类库四(File操作类)
View Code    public class FileHelper : IDisposable     {         private bool _alreadyDispose = false;         #region 构造函数         public FileHelper()         {             //             // TODO: 在此处添加构造函数逻辑             //         }         ~FileHelpe
跟着阿笨一起玩NET
2018/09/18
9810
.NET代码错误日志
1.首先创建一个类Files using System.IO; using System.Security.AccessControl; namespace 命名空间 {     public class Files     {         /// <summary>         /// 给指定的操作系统用户赋操作权限         /// </summary>         /// <param name="pathname">文件的路径</param>         /// <param
DougWang
2020/02/18
6510
C#常用目录文件操作封装类代码
这个c#类封装了常用的目录操作,包括列出目录下的文件、检测目录是否存在、得到目录下的文件列表、检测目录是否为空、查找目录下的文件等等功能
用户7108768
2021/11/03
1.1K0
Unity网络交互☀️压缩包zip下载与解压
在Unity搜索 I18N ,将这几个文件复制到Unity:Asset/DLL文件夹即可。
星河造梦坊官方
2024/08/15
1610
Unity网络交互☀️压缩包zip下载与解压
C# IO操作
明志德道
2023/10/21
2120
OxyPlot 导出图片及 WPF 元素导出为图片的方法
最近有个需求,就是将 OxyPlot 图形导出图片。经过尝试,本文记录三种方法:1、OxyPlot 自带导出方法;2、网上找的导出 WPF 界面元素的方法;3、基于方法 2 的附加属性调用方式。下面将逐一介绍。
独立观察员
2022/12/06
1.2K0
OxyPlot 导出图片及 WPF 元素导出为图片的方法
Visual Studio Package 插件开发
  这段时间公司新做了一个支付系统,里面有N个后台服务,每次有更新修改,拷贝打包发布包“不亦乐乎”。。。于是我想要不要自己定制个打包插件。
陈珙
2018/09/12
8700
Visual Studio Package 插件开发
c# 自动更新程序
1、我这里用到了json,那么不能直接饮用json的dll文件,会出现更新时候占用的问题,可以使用fastjson的开源代码,放进来解决,你可以直接使用xml格式的返回内容,这样就不需要json了,这样更方便
冰封一夏
2020/11/24
1.1K0
c# 自动更新程序
WPF项目从.Net Framework迁移到.Net6
Nuget 安装 Microsoft.Extensions.Configuration
码客说
2022/09/28
9530
WPF项目从.Net Framework迁移到.Net6
【游戏开发】Excel表格批量转换成lua的转表工具
  在上篇博客《【游戏开发】Excel表格批量转换成CSV的小工具》 中,我们介绍了如何将策划提供的Excel表格转换为轻便的CSV文件供开发人员使用。实际在Unity开发中,很多游戏都是使用Lua语言进行开发的。如果要用Lua直接读取CSV文件的话,又要写个对应的CSV解析类,不方便的同时还会影响一些加载速度,牺牲游戏性能。因此我们可以直接将Excel表格转换为lua文件,这样就可以高效、方便地在Lua中使用策划配置的数据了。在本篇博客中,马三将会和大家一起,用C#语言实现一个Excel表格转lua的转表工具——Xls2Lua,并搭配一个通用的ConfigMgr来读取lua配置文件。
马三小伙儿
2018/09/12
5.6K0
【游戏开发】Excel表格批量转换成lua的转表工具
C#封装的常用文件操作代码类
这个C#类封装了我们经常能用到的文件操作方法,包括读写文件、获取文件扩展名、复制文件、追加内容到文件、删除文件、移动文件、创建目录、递归删除文件及目录、列目录、列文件等,不可多得。
用户7108768
2021/11/02
9180
相关推荐
C#文件安全管理解析
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验