【问题标题】:User entered String splitting用户输入字符串拆分
【发布时间】:2018-05-07 10:02:48
【问题描述】:

我是 Progress opensge 的新手!我想知道如何使用使用过程拆分用户输入的字符串! 字符串的长度最大为 2000,每行最多包含 35 个字符。还必须考虑单词之间的空格。如果单行最大 > 35,则必须以另一行开头,但不是这样: ....hel 洛…………

它必须在 35.Line 切断并弹出下一行之后,但空格让我很困惑,我找不到任何算法 例如:

myfield2 =  "Many districts and landmarks in New York City are well known, and the city received a record 62.8 million tourists in 2017".

if index(myfield2,spacee) = 0 then do:

          do while ii < length(myfield2) :
           line = substring(myfield2,ii,35).
           ii = ii + 35.            
         end.
       end.
     display line.    
   else if index(myfield2,spacee) <> 0 and length(myfield2) < 35  then do: 

.......

这样吗?

谢谢!

【问题讨论】:

    标签: openedge progress-4gl


    【解决方案1】:

    您可以使用 R-INDEX 找到第 35 个字符的空格字符

    R-INDEX (myfield2, " ", ii) .

    【讨论】:

    • 你能举个例子吗?
    【解决方案2】:

    另一种方法:

    define variable t as character no-undo.
    define variable x as character no-undo format "x(36)".
    
    define variable i as integer   no-undo.
    define variable n as integer   no-undo.
    
    t = "Many districts and landmarks in New York City are well known, and the city received a record 62.8 million tourists in 2017".
    n = num-entries( t, " " ).
    
    do i = 1 to n:
    
      if length( x + entry( i, t, " " )) < 35 then
        do:
          x = x + entry( i, t, " " ) + " ".
          next.
        end.
    
      display x with frame a down.
      down 1 with frame a.
    
      x = entry( i, t, " " ) + " ".
    
    end.
    
    display x with frame a down.
    

    或者,如果您更喜欢嵌入换行符的变量:

    define variable t as character no-undo.
    define variable x as character no-undo format "x(36)".
    
    define variable i as integer   no-undo.
    define variable j as integer   no-undo.
    define variable n as integer   no-undo.
    
    t = "Many districts and landmarks in New York City are well known, and the city received a record 62.8 million tourists in 2017".
    n = num-entries( t, " " ).
    
    do i = 1 to n:
    
      if j < 35 and
        (j + length( entry( i, t, " " )) + 1) < 35 then
        do:
          x = x + entry( i, t, " " ) + " ".
          j = j + length( entry( i, t, " " )) + 1.
          next.
        end.
    
      j = length( entry( i, t, " " )) + 1.
      x = x + "~n" + entry( i, t, " " ) + " ".
    
    end.
    
    display x view-as editor size 40 by 10.
    

    【讨论】:

    • 嘿,汤姆,首先感谢您的帮助!它必须是一个 pdf 文件,看起来像这样 -->ibb.co/hCSEES 文本部分会随着文本大小而增加..(更多的单词和区域必须更大)我认为 textlenght / 35 ! 35.word之后如果没有空格就必须剪掉!我已经写了区域线绘图程序,但我没有停留在文本区域:(我的意思是它必须是最佳的:)
    • 我的答案中的代码在超过 35 个字符之前在最后一个空格处拆分文本,然后开始新行并根据需要再次执行多次。对我来说,这听起来像是你在问题和上面的评论中所要求的,但如果我不明白你的要求,我很抱歉。
    • 如果没有“空格/空白”怎么办。它必须完全切断 35.line 但为此我必须使用子字符串或条目?如果没有空格,您能否编辑一下?
    • 此外,如果下一行包含小文本,它不会显示整个文本。例如:run ABC(“纽约市的许多地区和地标都广为人知,该市在 2017 年接待了创纪录的 6280 万游客”)。它返回元素直到“游客”,下一行不以“2017 年游客”开头。谢谢。
    • 一行包含 35 个字符。您的代码不会将其从 35 中删除。例如:“斯德哥尔摩的许多地区和地标都很有名,该市在 2017 年接待了创纪录的 6280 万游客”。 1.line是:斯德哥尔摩的许多地区和地标,它包含超过35个字符。这意味着“斯德哥尔摩”必须从下一行开始,否则文本会从字段溢出。如果没有空白,则直接从36中删除。如果下一行也没有空白,则从 72.line 中将其切断,如果 3.row 包含空白(“”),则必须考虑空白。一定是这样的!我希望我很清楚。谢谢
    【解决方案3】:

    类似这样的:

    DEFINE VARIABLE cInput  AS CHARACTER   NO-UNDO INITIAL 
    "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et ~
    dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet ~
    clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, ~
    consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, ~
    sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea ~
    takimata sanctus est Lorem ipsum dolor sit amet.".
    
    DEFINE VARIABLE cOutput AS CHARACTER   NO-UNDO.
    DEFINE VARIABLE iPos    AS INTEGER     NO-UNDO.
    DEFINE VARIABLE iPrev   AS INTEGER     NO-UNDO INITIAL 1 . 
    DEFINE VARIABLE iLength AS INTEGER     NO-UNDO.
    
    ASSIGN iLength = LENGTH (cInput, "CHARACTER").
    
    repeatLoop:
    REPEAT:
        iPos = R-INDEX (cInput, " ", iPrev + 35) . 
    
        IF iPos = 0 THEN 
            ASSIGN iPos = iLength .  
    
        ASSIGN cOutput = cOutput + TRIM (SUBSTRING (cInput, iPrev, iPos - iPrev, "CHARACTER")) + "|~n".
    
        IF iPos + 35 >= iLength THEN DO: 
            ASSIGN cOutput = cOutput + TRIM (SUBSTRING (cInput, iPos, -1, "CHARACTER")) .
    
            LEAVE repeatLoop. 
        END.
    
        ASSIGN iPrev = iPos . 
    END.
    
    MESSAGE cOutput
        VIEW-AS ALERT-BOX INFORMATION BUTTONS OK.
    

    【讨论】:

    • 它不会像这样切断文本:modtemLoremipsumdolorsitametconsetetursadipscingelitrseddiamnonumyeirporinviduntutlaboreet.Commentsusedtoasforclarificationoro。我的意思是没有空格的文本。
    • 如果文本贯穿始终,没有空格,那么它也必须直接从 36.line 剪切,但它可能是这样的:abcdeabcdeabcdeabcdeabcdeabcdeabcdea --> 36.line 必须从下一行开始,如果有在 36.line 之后是空白,那么您的代码必须被执行。如果它们不是空白,则必须在 36.line 之后再次剪切,但这一次如果存在则被视为空白.. 它必须像这样。
    【解决方案4】:

    试试这样的。应该为你工作。它将文本分解为临时表。只需调整过程调用中的第二个参数,您就可以将文本拆分为所需的任何文本长度。

    def temp-table tt-editor no-undo
        field linha      as integer
        field conteudo   as character format 'x(80)':U
        index editor-id is primary unique 
              linha.
    
    procedure pi-print-editor:
        def input param c-editor    as char    no-undo.
        def input param i-len       as integer no-undo.
    
        def var i-linha  as integer no-undo.
        def var i-aux    as integer no-undo.
        def var c-aux    as char    no-undo.
        def var c-ret    as char    no-undo.
    
        IF c-editor = ? or i-len = ? THEN
            RETURN.
    
        for each tt-editor:
            delete tt-editor.
        end.
    
        assign c-ret = chr(255) + chr(255).
    
        do  while c-editor <> "":
            if  c-editor <> "" then do:
                assign i-aux = index(c-editor, chr(10)).
    
                IF i-aux = 0 THEN DO:
                    assign i-aux = index(c-editor, chr(13)).
                END.
                if  i-aux > i-len or (i-aux = 0 and length(c-editor) > i-len) then
                    assign i-aux = r-index(c-editor, " ", i-len + 1).
                if  i-aux = 0 then
                    assign c-aux = substr(c-editor, 1, i-len)
                           c-editor = substr(c-editor, i-len + 1).
                else
                    assign c-aux = substr(c-editor, 1, i-aux - 1)
                           c-editor = substr(c-editor, i-aux + 1).
                if  i-len = 0 then
                    assign entry(1, c-ret, chr(255)) = c-aux.
                else do:
                    assign i-linha = i-linha + 1.
                    create tt-editor.
                    assign tt-editor.linha    = i-linha
                           tt-editor.conteudo = c-aux.
                end.
            end.
            if  i-len = 0 then
                return c-ret.
        end.
        return c-ret.
    end procedure.
    
    RUN pi-print-editor ('modtemLoremipsumdolorsitametconsetetursadipscingelitrseddiamnonumyeirporinviduntutlaboreet.Commentsusedtoasforclarificationoro. i mean the text without spaces.',35).
    
    FOR EACH tt-editor:
        DISP tt-editor.conteudo FORMAT 'x(40)' LENGTH(conteudo) WITH WIDTH 333.
    END.
    

    希望对你有帮助。

    【讨论】:

    • 感谢 bruno :) 但我是初学者,这个临时表或其他东西对我来说有点复杂!我只是需要更简单的东西:)我实际上写了一些代码,但如果没有空白,它不会在 35.line 之后切断文本!不知道如何向您展示我的代码 :) 它不适合评论区 :)