我在精度为 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)
有没有办法不包括这个“没有时间”?
我正在使用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时寻找一种更整洁的格式化方法。
我有一个像下面这样的字符串:
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)