我们想将单词包装在换行符(\ n)处的字符串中。基本上,我们按ENTER键开始换行。

按下Enter键即可在“我们的消息”中创建新行。

输入数据 :

在注释列中捕获了字符串。
给出的评论栏为:

EDI ORDER-SAVE COMMENTS\nC3 Generic\nLOC 0833\nExpected arrival 01/07/2016\nOTYPE NE\nTRKPC 01 GM/00007643020008361321


我们尝试过:

string s = "EDIORDER-SAVE COMMENTS C3 Generic LOC 0833 Expected arrival 01/07/2016  OTYPE NE TRKPC 01 GM/00007643020008361321"  ;

StringBuilder sb = new StringBuilder(s);

int i = 0;
while ((i = sb.indexOf(" ", i + 20)) != -1) {
    sb.replace(i, i + 1, "\n");
}


转换后的预期数据:
换行符是换行符

Long  String of Comment field  should Splits in 6 different lines

EDI ORDER-SAVE COMMENTS

C3 Generic

LOC 0833

Expected arrival 01/07/2016

OTYPE NE

TRKPC 01 GM/00007643020008361321


输出后的确切数据如下所示:

SalesOrder    NComment                            SalesOrderLine                    StockCode
183590  EDI ORDER-SAVE COMMENTS                         1
183590                                                  2                         abc-defg-13OZ-24
183590  C3    Generic                                   37
183590  LOC 0833                                        38
183590  Expected arrival               01/07/2016       39
183590  OTYPE NE                                        40
183590  TRKPC 01 GM/00007643020008361321                51


任何帮助,将不胜感激!

最佳答案

抱歉,我还没有真正想要实现的目标。您要在通过第20个字符的单词之前插入中断吗?

    StringBuilder sb = new StringBuilder(s);
    int currentSpaceFound = sb.indexOf(" ");
    int i = 1;
    while (currentSpaceFound != -1) {
        int lastOccurence = currentSpaceFound;
        currentSpaceFound = sb.indexOf(" ", currentSpaceFound + 1);
        if (currentSpaceFound > (20 * i)) {
            sb.setCharAt(lastOccurence, '\n');
            i++;
        }
    }
    System.out.println(sb);

10-06 12:45
查看更多