0

領収書のレイアウトを使用して、製品の説明テキストが 24 文字を超える場合は 2 行に分割しようとしています。

私の最初の解決策は次のようなものでした:

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

しかし、それはこの結果をもたらします。

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

説明を分割して、通常どおりに表示する関数を作成する方法がわかりません。

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

私が自分自身を明確にしたことを願っています /:

4

3 に答える 3

1

24 より大きい場合は、ポイント 23 から減少するようにスペース文字を探します。見つかったら、その位置で文字列を分割します。あなたが持っているその「列」システムはかなり厄介に見えます-この出力はどこに行くのですか、画面?

于 2009-09-29T12:13:32.953 に答える
0

このようなものが動作するはずです:

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
于 2009-09-29T12:23:17.903 に答える
0

私の最初のショット、

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

など :

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

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

最適化できると思います。

于 2009-09-29T12:34:21.450 に答える