title: Word wrap a Delphi string to a certain length using commas not spaces?
body: <p>I am generating a comma separated list of names in a string eg</p>
  
  <pre><code>Mr John Blue, Miss A Green, Mr Posh Hyphenated-Surname, Mr Fred Green, Miss Helen Red, Ms Jean Yellow
  </code></pre>
  
  <p>I now want to display them in a memo box that will hold 50 characters on each line so that as many names as possible (and their trailing comma) appear on each line.
  so the above should look like</p>
  
  <pre><code>Mr John Blue, Miss A Green,
  Mr Posh Hyphenated-Surname, Mr Fred Green,
  Miss Helen Red, Ms Jean Yellow
  </code></pre>
  
  <p>I've played with </p>
  
  <pre><code>Memo1.text := WrapText(Mystring,50)
  </code></pre>
  
  <p>but it broke lines at spaces between forename and surnames and I tried </p>
  
  <pre><code>Memo1.text := WrapText(MyString, slinebreak, ',' ,50) 
  </code></pre>
  
  <p>to force it to break only after a comma but that broke at spaces as well as commas. Both also tended to break at a hyphen and I note from Rob Kennedy's reply to a similar question that embedded quotes cause problems with Wrap() so a name like <strong>Mr John O'Donald</strong> would cause problems.</p>
  
  <p>I even tried rolling my own function by counting characters and looking for commas but got bogged down in multiple nested IFs  (Too embarassed to show the dreadful code for that!)</p>
  
  <p>Can anyone offer any help or code showing how this can be done? </p>
  
  <p>PS 
  I have looked at </p>
  
  <ul>
  <li>'Word wrap in TMemo at a plus (+) char' </li>
  <li>'How do I split a long string into “wrapped” strings?'</li>
  <li>'Find a certain word in a string, and then wrap around it'</li>
  </ul>
  
  <p>and other similar posts but none seem to match what I am looking for.</p>