ZYW.STRHelper
1.17.5
dotnet add package ZYW.STRHelper --version 1.17.5
NuGet\Install-Package ZYW.STRHelper -Version 1.17.5
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ZYW.STRHelper" Version="1.17.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ZYW.STRHelper" Version="1.17.5" />
<PackageReference Include="ZYW.STRHelper" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ZYW.STRHelper --version 1.17.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: ZYW.STRHelper, 1.17.5"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ZYW.STRHelper@1.17.5
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ZYW.STRHelper&version=1.17.5
#tool nuget:?package=ZYW.STRHelper&version=1.17.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
STRHelper - 字符串与通用辅助工具库
介绍
STRHelper 是一个轻量级的 .NET 辅助类库,专注于提供实用的字符串处理、随机数/随机字符串生成、日志写入、文件保存等通用功能,简化日常开发中的重复编码工作。
功能特性
1. 随机数据生成
- 生成指定长度的随机字符串(数字+大小写字母,排除易混淆字符)
- 生成指定长度的随机数字字符串
- 生成无分隔符的 GUID 字符串
- 基于加密安全随机数生成器的随机字节获取,提升随机数安全性
2. 日志与文件操作
- 便捷的日志写入扩展方法(按日期分目录/文件存储,自动去重,线程安全)
- 字符串直接保存为 TXT 文件的扩展方法
3. 字符串拼接
- 生成枚举类型的sql用的Case语句
- 获取枚举值的字符值或描述值
安装
通过 NuGet 包管理器安装:
Install-Package STRHelper
或通过 .NET CLI 安装:
dotnet add package STRHelper
快速开始
命名空间
首先引入命名空间:
using Helper;
1. 生成随机数据
生成随机字符串(长度8)
string randomStr = STRHelper.GetRandomString(8);
// 示例输出:9s7B8k2P
生成随机数字字符串(长度6)
string randomNum = STRHelper.GetRandomNumString(6);
// 示例输出:879201
生成无分隔符GUID
string guid = STRHelper.GetGuidString();
// 示例输出:c7b9f8e7a6d543b2a1c0d9e8f7a6b5c4
2. 日志写入
基础日志写入
// 写入日志(自动按 年/月/日 分文件存储)
"用户登录成功:admin".WriteLog();
// 带时间分割线的日志写入
"系统启动完成".WriteLog(tagDateTime: true);
// 关闭日志去重/记录
"测试日志(不记录)".WriteLog(RecordLog: false);
3. 保存字符串到TXT文件
// 直接将字符串保存为TXT文件
string content = "Hello, STRHelper!";
content.SaveToTxt(@"D:\test.txt");
详细API说明
随机数据生成
| 方法 | 说明 | 参数 |
|---|---|---|
GetRandomString(int len) |
生成指定长度的随机字符串(包含数字、大小写字母,排除0/O/l等易混淆字符) | len:字符串长度 |
GetRandomNumString(int len) |
生成指定长度的随机数字字符串 | len:字符串长度 |
GetGuidString() |
生成无分隔符的GUID字符串 | 无 |
日志与文件操作
| 方法 | 说明 | 参数 |
|---|---|---|
WriteLog(this string content, bool tagDateTime = false, bool RecordLog=true) |
字符串扩展方法,写入日志到程序目录下的Log文件夹(按年月/日分文件) | content:日志内容;tagDateTime:是否添加时间分割线;RecordLog:是否记录日志(默认true) |
SaveToTxt(this String str, String TxtFileName) |
字符串扩展方法,将字符串保存为指定路径的TXT文件 | str:要保存的字符串;TxtFileName:文件完整路径(含文件名) |
兼容性
- .NET Framework 4.0+ / .NET Core 2.0+ / .NET 5+ / .NET 6+ / .NET 7+
- Windows 系统(依赖文件系统路径,Linux/Mac需适配路径分隔符)
注意事项
- 日志写入功能默认使用程序运行目录下的
Log文件夹,需确保程序有该目录的读写权限; WriteLog方法内置了日志内容去重(LastLog),避免短时间内重复写入相同内容;- 随机数生成基于
RNGCryptoServiceProvider,保证加密级别安全性,适合生成验证码、临时密码等场景。
问题反馈
如有bug、需求或建议,可通过以下方式反馈:
- GitHub Issues:https://github.com/your-username/STRHelper/issues
- 邮箱:your-email@example.com
许可证
本项目采用 MIT 许可证开源 - 详见 LICENSE 文件。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- System.Configuration.ConfigurationManager (>= 10.0.8)
- System.Data.DataSetExtensions (>= 4.5.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.