Minimize string length in C #

Hey guys.

I want to reduce the length of the string. How...

This line ...

string foo ="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean in vehicula nulla. Phasellus libero dui, luctus quis bibendum sit amet"; 

Becomes this line ...

Lorem ipsum dolor sit amet, consectetur adipiscing ...

How can I do it? Thanks!

+4
source share
6 answers
 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); 

Use a substring to get the first 50 characters of a string. http://msdn.microsoft.com/en-us/library/aka44szs.aspx

+10
source

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

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean in a nulla car. Phasellus libero dui, luctus quis bibendum sit amet

+3
source

try it

 string myString = "....."; int maxLength = 30; if( myString.Length > maxLength ){ myString = myString.SubString(0, maxLength); } 

myString now the maximum size of maxLength .

+2
source

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) + "..."; } } 
+1
source
  public static string TruncatString(string input, int maxLength) { if (input.Length < maxLength) return input; return input.Substring(0, maxLength - 3) + "..."; } 
+1
source

Given the line -

 var string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean in vehicula nulla. Phasellus libero dui, luctus quis bibendum sit amet"; 

1. The first scenario

 var maxlenght = 15; var shortString =(string .Length > maxlenght ) ? string.Substring(0,maxlenght ) + "..." : string; 

Exit: "Lorem ipsum dol ...";

2. The second scenario

 var shortString =(string .Length > maxlenght ) ? string.Substring(0, string.IndexOf(" ", 15)) + "..." : string; 

Exit: "Lorem ipsum dolor ...";

if u wants the last word to be complete, then the best second senario ...

now it works fine ...

  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; } 
0
source

Source: https://habr.com/ru/post/1344104/


All Articles