minimize length of string in C#
I want to reduce the length of a string. Like...
This str开发者_开发知识库ing...
string foo ="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean in vehicula nulla. Phasellus libero dui, luctus quis bibendum sit amet";
Becomes this string...
Lorem ipsum dolor sit amet, consectetur adipiscing...
How do I do this? Thanks!
string foo ="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean in vehicula nulla. Phasellus libero dui, luctus quis bibendum sit amet";
string small_foo = foo.SubString(0, 50);
Using Substring to get the first 50 characters of the string. http://msdn.microsoft.com/en-us/library/aka44szs.aspx
Code
public string Truncate(string input, int length)
{
if (input.Length < length) return input;
int index = input.IndexOf(' ', length);
return input.Substring(0, index) + "...";
}
...
string foo ="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean in vehicula nulla. Phasellus libero dui, luctus quis bibendum sit amet";
string bar = Truncate(foo, 50);
Console.WriteLine(bar);
Output
Lorem ipsum dolor sit amet, consectetur adipiscing...
Instead of
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean in vehicula nulla. Phasellus libero dui, luctus quis bibendum sit amet
Try this
string myString = ".....";
int maxLength = 30;
if( myString.Length > maxLength ){
myString = myString.SubString(0, maxLength);
}
myString
is now the maximum of maxLength
's size.
Here you go:
class Program
{
static void Main(string[] args)
{
string foo = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean in vehicula nulla. Phasellus libero dui, luctus quis bibendum sit amet";
int maxlength = 50;
string shortFoo = StringShortener(foo, maxlength);
Console.WriteLine("{0} \n becomes: \n {1}", foo, shortFoo);
Console.ReadKey();
}
private static string StringShortener(string foo, int maxlength)
{
if (foo.Length <= maxlength)
return foo;
else
return foo.Substring(0, maxlength - 4) + "...";
}
}
public static string TruncatString(string input, int maxLength)
{
if (input.Length < maxLength) return input;
return input.Substring(0, maxLength - 3) + "...";
}
Given String -
var string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean in vehicula nulla. Phasellus libero dui, luctus quis bibendum sit amet";
1. First Scenario
var maxlenght = 15;
var shortString =(string .Length > maxlenght ) ? string.Substring(0,maxlenght ) + "..." : string;
Output : "Lorem ipsum dol...";
2. Second Scenario
var shortString =(string .Length > maxlenght ) ? string.Substring(0, string.IndexOf(" ", 15)) + "..." : string;
Output: "Lorem ipsum dolor...";
if u want last word full then second senario is best...
this works me fine now...
public static string GetShortTitleForDisplay(string title, int noOfCharacter)
{
var shortTitle = title;
if (title.Length <= noOfCharacter) return shortTitle;
var pos = title.IndexOf(" ", noOfCharacter, StringComparison.Ordinal);
if (pos > 0)
shortTitle = title.Substring(0, pos) + " ...";
return shortTitle;
}
string foo ="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean in vehiculanulla. Phasellus libero dui, luctus quis bibendum sit amet";
string small_foo = foo.Substring(new Random().Next(0, urlsafe.Length), new Random().Next(2, 6));
Code :
string longString = "hi, am Ramy, a backend and a frontend developer, am here to
help people in Programming with C#";
int maxLength = 17;
if (longString.Length < maxLength)
{
longString = longString.Substring(0, 17) + "..."; //Those 3 dots are additional (if you want)
}
The result will be : // hi, am Ramy, a ba...
精彩评论