views:

75

answers:

3

Hey, im working with a receipt layout and trying to divide up a products descriptiontext into 2 lines if its longer then 24 characters.

my first solution was something like this:

If Row.Description.Length >= 24 Then
TextToPrint &= Row.Description.Substring(0, 24) & "      $100"
TextToPrint &= Row.Description.Substring(24) & vbNewLine
else
 TextToPrint &= Row.Description & filloutFunction(Row.Description.length) &"      $100" & vbNewLine
end if

but that gives this result.

A product with a long na   $100
me that doesn't fit

I cant figure out how to make a function to divide the description to look like we normaly see it.

A product with a long      $100
name that doesn't fit

hope i made myself clear /:

would really appreciate any help.

+1  A: 

If its greater than 24 then look for a space character from point 23 decrementally. Once you find it, split the string on that position. That 'column' system you have looks pretty nasty though - where is this output going, screen?

UpTheCreek
to a receipt printer. one row on the paper has 42 characters in width. i need the remaning 18 characters for a "amount-column to have a "right-alignment"
Alexander
ah, understandable then.
UpTheCreek
A: 

Something like this should work:

Dim yourString = "This is a pretty ugly test which should be long enough"
Dim inserted As Boolean = False
For pos As Integer = 24 To 0 Step -1
    If Not inserted AndAlso yourString(pos) = " "c Then
        yourString = yourString.Substring(0, pos + 1) & Environment.NewLine & yourString.Substring(pos + 1)
        inserted = True
    End If
Next

Bobby

Bobby
Thankyou! that worked perfectly!
Alexander
A: 

Hi,

My first shot,

private static List<string> SplitSentence(string sentence, int count)
{
    if(sentence.Length <= count)
    {
        return new List<string>()
               {
                   sentence
               };
    }

    string extract = sentence.Substring(0, sentence.Substring(0, count).LastIndexOfAny(new[]
                                                                                      {
                                                                                          ' '
                                                                                      }));

    List<string> list = SplitSentence(sentence.Remove(0, extract.Length), count);

    list.Insert(0, extract.Trim());

    return list;
}

and so :

string sentence = "A product with a long name that doesn't fit";

List<string> sentences = SplitSentence(sentence, 24);
sentences[0] = sentences[0] + "      $100";

I think it's possible to optimize.

Best Regards,

Vincent BOUZON

Vincent BOUZON
thankyou! I found this usefull aswell!
Alexander