string - Need to break the line at some index and without breaking the word using c# -


i have string below

innerstring s="clicking log out clear our cookies , log out of stack overflow on devices";

i need break string , form in lines format,each line should contains 37 characters

condition :

1) should not break word

e.g: clicking log out clear our coo
ies , log out of stack overflow on al
l devices

i tried using below code

                int appendcount = linkcount / 38;                     if (innerstring.length > 37)                     {                         int startvalue = 38;              if(innerstring[startvalue]==" ")             {                 innerstring = innerstring.insert(startvalue, system.environment.newline).trimend();                     startvalue = startvalue + 38;               }              else             {             int i=innerstring.lastindexof(" ",startvalue);             startvalue=i++;                             innerstring = innerstring.insert(startvalue, system.environment.newline).trimend();             startvalue = startvalue + 38;             } 

output should without breaking words in middle :

clicking log out clear our cookies
, log out of stack overflow on
devices

added whole thing in while loop , works:

string innerstring = "clicking log out clear our cookies , log out of stack overflow on devices"; if (innerstring.length > 37) {     int startvalue = 38;     while (startvalue < innerstring.length)     {         if (innerstring[startvalue] == ' ')         {             innerstring = innerstring.insert(startvalue, system.environment.newline).trimend();             startvalue = startvalue + 38;         }         else         {             int = innerstring.lastindexof(" ", startvalue);             startvalue = i++;             innerstring = innerstring.insert(startvalue, system.environment.newline).trimend();             startvalue = startvalue + 38;         }     } } 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -