Skip to main content

string

Null/Empty

note

字符串判空

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

note

拼接字符串

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"

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"
tip

可通过委托指定 Join 拼接的内容

intJoin.Join("@", item => -item); // "-1@-2@-3"

CanPing

note

测试网址字符串是否可以被ping通

string url = "https://www.bilibili.com/";
url.CanPing(); // 如果联网了,应该为 True

PadLeftAndRight

note

字符串左右填充

int iValue = 233;
iValue.PadLeft(6, '-'); // "---233"
iValue.PadRight(6); // "233 "
iValue.PadRight(6, item => item + 1, '-'); // "234---"

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

note

自动为字符串添加遮罩

var origin = "CollapseNav.Net.Tool";
var masked = origin.AutoMask(); // Col*ool

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

note

为字符串集合批量添加前缀后缀

string[] strs = new[] { "2", "3" };
strs.AddBegin("999"); // ["9992", "9993"]
strs.AddEnd("111"); // ["9992111", "9993111"]

SplitAs

note

将字符串按照指定分隔符分割并且通过指定的规则转换

var temp = "123,456";
var nums = temp.SplitAs(str => int.Parse(str), ',');
// nums[0] = 123, nums[1] = 456