Ste*_*idi 379 .net c# string truncate
我想截断一个字符串,使其长度不超过给定值.我正在写一个数据库表,并希望确保我写的值符合列的数据类型的约束.
例如,如果我可以编写以下内容会很好:
string NormalizeLength(string value, int maxLength)
{
return value.Substring(0, maxLength);
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,这会引发异常,因为maxLength
通常会超出字符串的边界value
.当然,我可以编写如下的函数,但我希望这样的东西已经存在.
string NormalizeLength(string value, int maxLength)
{
return value.Length <= maxLength ? value : value.Substring(0, maxLength);
}
Run Code Online (Sandbox Code Playgroud)
执行此任务的难以捉摸的API在哪里?有吗?
LBu*_*kin 579
Truncate()
遗憾的是,字符串上没有方法.你必须自己写这种逻辑.但是,你可以做的是将它包装在扩展方法中,这样你就不必在任何地方复制它:
public static class StringExt
{
public static string Truncate(this string value, int maxLength)
{
if (string.IsNullOrEmpty(value)) return value;
return value.Length <= maxLength ? value : value.Substring(0, maxLength);
}
}
Run Code Online (Sandbox Code Playgroud)
现在我们可以写:
var someString = "...";
someString = someString.Truncate(2);
Run Code Online (Sandbox Code Playgroud)
Caf*_*eek 118
或者代替三元运算符,您可以使用Math.min
public static class StringExt
{
public static string Truncate( this string value, int maxLength )
{
if (string.IsNullOrEmpty(value)) { return value; }
return value.Substring(0, Math.Min(value.Length, maxLength));
}
}
Run Code Online (Sandbox Code Playgroud)
jpi*_*son 39
我想我会抛弃我的实现,因为我相信它涵盖了其他人已经触及的所有案例,并且以简洁的方式这样做仍然是可读的.
public static string Truncate(this string value, int maxLength)
{
if (!string.IsNullOrEmpty(value) && value.Length > maxLength)
{
return value.Substring(0, maxLength);
}
return value;
}
Run Code Online (Sandbox Code Playgroud)
这个解决方案主要建立在Ray的解决方案之上,并通过使用this关键字打开了用作扩展方法的方法,正如LBushkin在他的解决方案中所做的那样.
Dyl*_*son 35
在.NET 4.0中,您可以使用以下Take
方法:
string.Concat(myString.Take(maxLength));
Run Code Online (Sandbox Code Playgroud)
没有测试效率!
drz*_*aus 35
因为性能测试很有趣:(使用linqpad扩展方法)
var val = string.Concat(Enumerable.Range(0, 50).Select(i => i % 10));
foreach(var limit in new[] { 10, 25, 44, 64 })
new Perf<string> {
{ "newstring" + limit, n => new string(val.Take(limit).ToArray()) },
{ "concat" + limit, n => string.Concat(val.Take(limit)) },
{ "truncate" + limit, n => val.Substring(0, Math.Min(val.Length, limit)) },
{ "smart-trunc" + limit, n => val.Length <= limit ? val : val.Substring(0, limit) },
{ "stringbuilder" + limit, n => new StringBuilder(val, 0, Math.Min(val.Length, limit), limit).ToString() },
}.Vs();
Run Code Online (Sandbox Code Playgroud)
该truncate
方法"显着"更快.#microoptimization
早
- truncate10 5788 ticks过去了(0.5788 ms)[10K reps,5.788E-05 ms per]
- smart-trunc10 8206 ticks已过去(0.8206 ms)[在10K reps中,每个8.206E-05 ms]
- stringbuilder10 10557 ticks elapsed(1.0557 ms)[10K reps,0.00010557 ms per]
- concat10 45495 ticks已过去(4.5495 ms)[以10K reps计,每秒0.00045495 ms]
- newstring10 72535 ticks已过去(7.2535 ms)[以10K代表为单位,每个0.00072535 ms]
晚了
- truncate44 8835 ticks过去了(0.8835 ms)[10K reps,8.835E-05 ms per]
- stringbuilder44 13106 ticks过去了(1.3106 ms)[10K reps,0.00013106 ms per]
- smart-trunc44 14821 ticks已过去(1.4821 ms)[在10K reps中,每个0.00014821 ms]
- newstring44 144324 ticks逝去(14.4324 ms)[在10K reps中,每个0.00144324 ms]
- concat44 174610经过时间(17.461毫秒)[10K次,每次0.0017461毫秒]
太长
- smart-trunc64 6944 ticks已过去(0.6944 ms)[在10K reps中,每个6.944E-05 ms]
- truncate64 7686 ticks过去了(0.7686 ms)[10K reps,7.686E-05 ms per]
- stringbuilder64 13314 ticks已过去(1.3314 ms)[以10K reps计,每个0.00013314 ms]
- newstring64 177481 ticks逝去(17.7481 ms)[10K reps,0.00177481 ms per]
- concat64 241601经过时间(24.1601 ms)[以10K代表计,每次0.00241601 ms]
tam*_*mes 27
您可以使用LINQ ...它消除了检查字符串长度的需要.不可否认,也许不是最有效的,但它很有趣.
string result = string.Join("", value.Take(maxLength)); // .NET 4 Join
Run Code Online (Sandbox Code Playgroud)
要么
string result = new string(value.Take(maxLength).ToArray());
Run Code Online (Sandbox Code Playgroud)
Dar*_*ren 13
似乎还没有人发布这个:
public static class StringExt
{
public static string Truncate(this string s, int maxLength)
{
return s != null && s.Length > maxLength ? s.Substring(0, maxLength) : s;
}
}
Run Code Online (Sandbox Code Playgroud)
使用&&运算符使其略微优于接受的答案.
Sea*_*nMC 13
我在一条线上做了这样的事情
value = value.Length > 1000 ? value.Substring(0, 1000) : value;
Run Code Online (Sandbox Code Playgroud)
Joe*_*Joe 11
.NET Framework有一个API来截断这样的字符串:
Microsoft.VisualBasic.Strings.Left(string, int);
Run Code Online (Sandbox Code Playgroud)
但是在C#应用程序中,你可能更喜欢自己动手而不是依赖于Microsoft.VisualBasic.dll,它的主要存在是向后兼容性.
小智 8
另一个解决方案:
return input.Substring(0, Math.Min(input.Length, maxLength));
Run Code Online (Sandbox Code Playgroud)
最近 C# 中最简单的方法是:
string Trunc(string s, int len) => s?.Length > len ? s.Substring(0, len) : s;
Run Code Online (Sandbox Code Playgroud)
它返回较长字符串的截断值和其他情况下的原始字符串 - 包括空输入 - 这是由 ? 处理的 一元运算符。
采用 @CaffGeek 并简化它:
public static string Truncate(this string value, int maxLength)
{
return string.IsNullOrEmpty(value) ? value : value.Substring(0, Math.Min(value.Length, maxLength));
}
Run Code Online (Sandbox Code Playgroud)
小智 5
为什么不:
string NormalizeLength(string value, int maxLength)
{
//check String.IsNullOrEmpty(value) and act on it.
return value.PadRight(maxLength).Substring(0, maxLength);
}
Run Code Online (Sandbox Code Playgroud)
即在事件value.Length < maxLength
垫空间到最后或截断多余的部分。
我知道这是一个老问题,但这是一个不错的解决方案:
public static string Truncate(this string text, int maxLength, string suffix = "...")
{
string str = text;
if (maxLength > 0)
{
int length = maxLength - suffix.Length;
if (length <= 0)
{
return str;
}
if ((text != null) && (text.Length > maxLength))
{
return (text.Substring(0, length).TrimEnd(new char[0]) + suffix);
}
}
return str;
}
var myString = "hello world"
var myTruncatedString = myString.Truncate(4);
Run Code Online (Sandbox Code Playgroud)
返回:你好...
与C#6的Null传播算子类似的变体
public static string Truncate(this string value, int maxLength)
{
return value?.Length <= maxLength ? value : value?.Substring(0, maxLength);
}
Run Code Online (Sandbox Code Playgroud)
请注意,我们基本上检查value
这里是否为空两次.
对于C#字符串,2016年仍然没有Truncate方法。但是-使用C#6.0语法:
public static class StringExtension
{
public static string Truncate(this string s, int max)
{
return s?.Length > max ? s.Substring(0, max) : s ?? throw new ArgumentNullException(s);
}
}
Run Code Online (Sandbox Code Playgroud)
它就像一个魅力:
"Truncate me".Truncate(8);
Result: "Truncate"
Run Code Online (Sandbox Code Playgroud)
我的两分钱示例长度为 30 :
var truncatedInput = string.IsNullOrEmpty(input) ?
string.Empty :
input.Substring(0, Math.Min(input.Length, 30));
Run Code Online (Sandbox Code Playgroud)
在 C# 8 中,可以使用新的 Ranges 功能...
value = value[..Math.Min(30, value.Length)];
Run Code Online (Sandbox Code Playgroud)
这是C# 9 的一行代码:
public static string? Truncate(this string? value, int maxLength) => value is null or "" || value.Length <= maxLength ? value : value[..maxLength];
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
278680 次 |
最近记录: |