问题
I'd like to write an extension method to the String class so that if the input string to is longer than the provided length N, only the first N characters are to be displayed.
Here's how it looks like:
public static string TruncateLongString(this string str, int maxLength)
{
if (str.Length <= maxLength)
return str;
else
//return the first maxLength characters
}
What String.*() method can I use to get only the first N characters of str?
回答1:
public static string TruncateLongString(this string str, int maxLength)
{
if (string.IsNullOrEmpty(str))
return str;
return str.Substring(0, Math.Min(str.Length, maxLength));
}
回答2:
string truncatedToNLength = new string(s.Take(n).ToArray());
This solution has a tiny bonus in that if n is greater than s.Length, it still does the right thing.
回答3:
You can use LINQ str.Take(n) or str.SubString(0, n), where the latter will throw an ArgumentOutOfRangeException exception for n > str.Length.
Mind that the LINQ version returns a IEnumerable<char>, so you'd have to convert the IEnumerable<char> to string: new string(s.Take(n).ToArray()).
回答4:
Whenever I have to do string manipulations in C#, I miss the good old Left and Right functions from Visual Basic, which are much simpler to use than Substring.
So in most of my C# projects, I create extension methods for them:
public static class StringExtensions
{
public static string Left(this string str, int length)
{
return str.Substring(0, Math.Min(length, str.Length));
}
public static string Right(this string str, int length)
{
return str.Substring(str.Length - Math.Min(length, str.Length));
}
}
Note:
The Math.Min part is there because Substring throws an ArgumentOutOfRangeException when the input string's length is smaller than the requested length, as already mentioned in some comments under previous answers.
Usage:
string longString = "Long String";
// returns "Long";
string left1 = longString.Left(4);
// returns "Long String";
string left2 = longString.Left(100);
回答5:
Simply:
public static String Truncate(String input,int maxLength)
{
if(input.Length > maxLength)
return input.Substring(0,maxLength);
return input;
}
回答6:
public static string TruncateLongString(this string str, int maxLength)
{
return str.Length <= maxLength ? str : str.Remove(maxLength);
}
回答7:
if we are talking about validations also why we have not checked for null string entries. Any specific reasons?
I think below way help since IsNullOrEmpty is a system defined method and ternary operators have cyclomatic complexity = 1 while if() {} else {} has value 2.
public static string Truncate(string input, int truncLength)
{
return (!String.IsNullOrEmpty(input) && input.Length >= truncLength)
? input.Substring(0, truncLength)
: input;
}
回答8:
I added this in my project just because where I'm using it is a high chance of it being used in loops, in a project hosted online hence I didn't want any crashes if I could manage it. The length fits a column I have. It's C#7
Just a one line:
public static string SubStringN(this string Message, int Len = 499) => !String.IsNullOrEmpty(Message) ? (Message.Length >= Len ? Message.Substring(0, Len) : Message) : "";
回答9:
substring(int startpos, int lenght);
回答10:
string.Substring(0,n); // 0 - start index and n - number of characters
来源:https://stackoverflow.com/questions/3566830/what-method-in-the-string-class-returns-only-the-first-n-characters