标签: humanizer

如果精度更大,如何使 Humanizer 不显示“无时间”?

我在精度为 4的 TimeSpan 上使用Humanizer:

(dateEnd - dateStart).Humanize(4)
Run Code Online (Sandbox Code Playgroud)

它会生成如下内容:

2 hours, 17 minutes, 20 seconds, 141 milliseconds
Run Code Online (Sandbox Code Playgroud)

但是当剩余时间只有几分钟(还剩不到一个小时)时,它会生成:

17 minutes, 20 seconds, 141 milliseconds, no time
Run Code Online (Sandbox Code Playgroud)

有没有办法不包括这个“没有时间”?

c# humanizer

5
推荐指数
1
解决办法
544
查看次数

在TimeSpan对象上使用Humanize时,如何自定义返回的格式化字符串?

我正在使用Humanizer格式化TimeSpan对象。这非常方便,但是我也希望将字符串的格式设置为:“ xd yh zm as”,而不是“ x天,y小时,z分钟,秒”。目前,我只是在Humanize完成其工作后替换这些子字符串,但是感觉很hacky。

TimeSpan difference = DateTime.Now - myTime;

_myString = difference.Humanize(3, maxUnit:Humanizer.Localisation.TimeUnit.Day, minUnit:Humanizer.Localisation.TimeUnit.Second);
_myString = _myString.Replace(" hours", "h").Replace(" hour", "h").Replace(" minutes", "m").Replace(" minute", "m").Replace(" days", "d").Replace(" day", "d").Replace(" seconds", "s").Replace(" second", "s").Replace(",", "");
Run Code Online (Sandbox Code Playgroud)

有更好的方法吗?我知道一些有关如何格式化TimeSpans的问题,但是我特别想在使用Humanizer时寻找一种更整洁的格式化方法。

.net c# humanizer

5
推荐指数
0
解决办法
206
查看次数

使用Humanizer或Regex在每个/周围添加空间

我有一个像下面这样的字符串:

var text = @"Some text/othertext/ yet more text /last of the text";
Run Code Online (Sandbox Code Playgroud)

我想规范化每个斜杠周围的空间,以便它匹配以下内容:

var text = @"Some text / othertext / yet more text / last of the text";
Run Code Online (Sandbox Code Playgroud)

也就是说,每个斜线前面有一个空格,之后是一个空格.我如何使用Humanizer来实现这一点,或者除此之外,使用单个正则表达式? Humanizer是首选解决方案.

我可以使用以下一对正则表达式执行此操作:

var regexLeft = new Regex(@"\S/");    // \S matches non-whitespace
var regexRight = new Regex(@"/\S");
var newVal = regexLeft.Replace(text, m => m.Value[0] + " /");
newVal = regexRight.Replace(newVal, m => "/ " + m.Value[1]);
Run Code Online (Sandbox Code Playgroud)

c# regex humanizer

4
推荐指数
1
解决办法
96
查看次数

标签 统计

c# ×3

humanizer ×3

.net ×1

regex ×1