string
Null/Empty(判空)
string empty = "";
string notEmpty = "NotEmpty";
string whiteSpace = " ";
empty.IsNull() && empty.IsEmpty(); //True
notEmpty.NotEmpty() && notEmpty.NotNull(); //True
notEmpty.IsNull(); //False
whiteSpace.IsEmpty(); //True
empty.NotEmpty(); //False
whiteSpace.NotNull(); //False
tip
若字符串为空, 则返回参数中传入的值, 可能不是很常用
empty.IsNull("233"); // "233"
notEmpty.IsEmpty("233"); // "NotEmpty"
Add/AddIf(拼接)
string str = string.Empty;
str = str.Add("1"); // "1"
str = str.AddIf(true,"2") // "12"
str = str.AddIf(false,"3"); // "12"
str = string.Empty;
str = str
.Add("1")
.AddIf(true,"2")
.AddIf(false,"3"); // "12"
tip
同时 StringBuilder 也有相似扩展
StringBuilder sb = new();
sb
.AddIf("1")
.AddIf("Not Empty","2")
.AddIf(false,"3")
.AddIf("","4"); // "12"
ToChinese
note
将数字字符串转为中文
string num = "10510001010";
num.ToChinese(); // "一百零五亿一千万零一千零一十"
num = "1010";
num.ToChinese(); // "一千零一十"
num = "99999";
num.ToChinese(); // "九万九千九百九十九"
num = "203300010001";
num.ToChinese(); // "二千零三十三亿零一万零一"
num = "9999999999999999";
num.ToChinese(); // "九千九百九十九万九千九百九十九亿九千九百九十九万九千九百九十九"
caution
不是很成熟,很可能出现各种奇奇怪怪的问题
ToXXX
note
字符串转基本数据格式
// 日期
string timeString = "2021-08-28 23:23:23";
timeString.ToDateTime();
// 整数Int32
string intString = "2333";
intString.ToInt();
// 浮点数
string doubleString = "233.333";
doubleString.ToDouble();
// Guid
string guidString = "00000000-0000-0000-0000-000000000000";
guidString.ToGuid();
// 长整型
string longString = "233333333333333";
longString.ToLong();
Join(连接)
note
可以直接将任意集合类型拼接为字符串
string strJoin = "233.233.233.233";
strJoin.Join("@"); // "2@3@3@.@2@3@3@.@2@3@3@.@2@3@3"
int[] intJoin = { 1, 2, 3 };
intJoin.Join("@"); // "1@2@3"
intJoin.Join("@", item => -item); // "-1@-2@-3"
tip
可通过委托指定 Join 拼接的内容
CanPing(ping测试)
string url = "https://www.bilibili.com/";
url.CanPing(); // 如果联网了,应该为 True
PadLeftAndRight(填充)
int iValue = 233;
double dValue = 2.33;
long lValue = 23333;
iValue.PadLeft(6); // " 233"
dValue.PadLeft(6); // " 2.33"
lValue.PadLeft(6); // " 23333"
iValue.PadLeft(6, '-'); // "---233"
dValue.PadLeft(6, '-'); // "--2.33"
lValue.PadLeft(6, '-'); // "-23333"
iValue.PadRight(6); // "233 "
dValue.PadRight(6); // "2.33 "
lValue.PadRight(6); // "23333 "
iValue.PadRight(6, '-'); // "233---"
dValue.PadRight(6, '-'); // "2.33--"
lValue.PadRight(6, '-'); // "23333-"
iValue.PadLeft(6, item => item + 1, '-'); // "---234"
dValue.PadLeft(6, item => item + 1, '-'); // "--3.33"
lValue.PadLeft(6, item => item + 1, '-'); // "-23334"
iValue.PadRight(6, item => item + 1, '-'); // "234---"
dValue.PadRight(6, item => item + 1, '-'); // "3.33--"
lValue.PadRight(6, item => item + 1, '-'); // "23334-"
StartsWith&&EndsWith
note
对字符串集合的 StartsWit&&EndsWith 判断
All** 全符合则为 true
Has** 有一个符合则为 true
string[] strs = new[] { "2333", "233333333333333", "2332" };
strs.HasStartsWith("233"); // True
strs.HasEndsWith("33"); // True
strs.AllStartsWith("2333"); // False
strs.AllEndsWith("33"); // False
// 可忽略大小写(默认大小写敏感)
strs = new[] { "ABCD", "AbcD", "abcd" };
strs.HasStartsWith("aBcd", true)
strs.AllStartsWith("aBcd", true)
strs.HasEndsWith("aBcd", true)
strs.AllEndsWith("aBcd", true)
FirstLast
note
取字符串 前几个 后几个
string str = "12345678987654321";
str.First(5); // "12345"
str.Last(5); // "54321"
AutoMask(自动遮罩)
var origin = "CollapseNav.Net.Tool";
var masked = origin.AutoMask(); // Col*ool
caution
可用性可能不大高
Base64
var imagePath = "./xxx.png";
// 图片转为 base64
var base64String = imagePath.ImageToBase64();
// base64 保存为流
var stream = base64String.Base64ImageToStream();
// base64 保存到文件
base64String.Base64ImageSaveTo(savePath);
tip
如果想要将普通文件转为 base64 也是可以的
var filePath = "./xxxx.txt";
using var fs = filePath.OpenReadShareStream();
var base64String = fs.StreamToBase64();
Contain(是否包含)
note
字符串是否包含指定的字符串
All** 全符合则为 true
Has** 有一个符合则为 true
string temp = "123456789";
temp.AllContain("789", "567"); // True
temp.HasContain("789", "567"); // True
temp.AllContain("789", "898"); // false
temp.HasContain("789", "898"); // True
tip
也可以直接传入集合, 此时可以选择忽略大小写
string temp = "ABCD";
temp.AllContain(new[] { "ABc", "cD" }, ignoreCase: true); // True
temp.AllContain(new[] { "ABc", "cD" }, ignoreCase: false); // False
temp.HasContain(new[] { "ABc", "CD" }, ignoreCase: true); // True
temp.HasContain(new[] { "ABc", "CD" }, ignoreCase: false); // True
temp.HasContain(new[] { "ABcE", "cD" }, ignoreCase: false); // False
AddBegin&AddEnd(添加前缀后缀)
string[] strs = new[] { "2", "3" };
strs.AddBegin("999"); // ["9992", "9993"]
strs.AddEnd("111"); // ["9992111", "9993111"]