嘿伙计们。
我想减少字符串的长度。像...
这个字符串......
string foo ="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean in vehicula nulla. Phasellus libero dui, luctus quis bibendum sit amet";
成为这个字符串......
Lorem ipsum dolor坐下来,精神上的adipiscing ...
我该怎么做?谢谢!
答案 0 :(得分:10)
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);
使用Substring获取字符串的前50个字符。 http://msdn.microsoft.com/en-us/library/aka44szs.aspx
答案 1 :(得分:3)
代码
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);
输出
Lorem ipsum dolor坐下来,精神上的adipiscing ...
而不是
Lorem ipsum dolor坐下来,精神上的精神。 Aenean in vehicula nulla。 Phasellus libero dui,luctus quis bibendum sit amet
答案 2 :(得分:2)
试试这个
string myString = ".....";
int maxLength = 30;
if( myString.Length > maxLength ){
myString = myString.SubString(0, maxLength);
}
myString
现在是maxLength
的最大值。
答案 3 :(得分:1)
你走了:
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) + "...";
}
}
答案 4 :(得分:1)
public static string TruncatString(string input, int maxLength)
{
if (input.Length < maxLength) return input;
return input.Substring(0, maxLength - 3) + "...";
}
答案 5 :(得分:0)
给定字符串 -
var string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean in vehicula nulla. Phasellus libero dui, luctus quis bibendum sit amet";
<强> 1。第一个场景
var maxlenght = 15;
var shortString =(string .Length > maxlenght ) ? string.Substring(0,maxlenght ) + "..." : string;
输出:&#34; Lorem ipsum dol ...&#34;;
<强> 2。第二种情景
var shortString =(string .Length > maxlenght ) ? string.Substring(0, string.IndexOf(" ", 15)) + "..." : string;
输出:&#34; Lorem ipsum dolor ...&#34;;
如果你想要最后一个字,那么第二个最好的话......
现在我的工作很好......
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;
}
答案 6 :(得分:0)
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));