2012-07-17 156 views
1

我在查找指定子串出現的字符串中的位置。在COBOL中查找子串的索引

例如,尋找在串子串「環保」,「綠色雞蛋和火腿」應該返回我1,但是從「綠色雞蛋和火腿綠色」將返回我1和14

我應該如何做這個?

編輯1:改變措辭如此位置1開始,不是0 編輯2:我可以在下面的代碼片段的第一個實例爲WS-指針:

MOVE 1 TO WS-POINTER 

UNSTRING WS-STRING(1:WS-STRING-LEN) 
    DELIMITED BY LT-MY-DELIMITER 
    INTO WS-STRING-GARBAGE        
    WITH POINTER WS-POINTER 
END-UNSTRING     
+0

如果你正在尋找一個COBOL動詞(陳述)來做到這一點,我認爲你可能會運氣不好。原生COBOL不會很好地執行子串:-( – NealB 2012-07-17 21:46:34

回答

3

AFAIK COBOL沒有一條語句來查找字符串中字符串的位置,以便需要手動完成。然而,COBOL確實有一個統計字符串中的字符串出現一種說法: INSPECT串計數器清點所有搜索字符串

下面是一個例子程序,在OpenCOBOL工作(見OpenCobol.org):

IDENTIFICATION DIVISION. 
    PROGRAM-ID. OCCURRENCES. 

    ENVIRONMENT DIVISION. 
    INPUT-OUTPUT SECTION. 
    FILE-CONTROL. 

    DATA DIVISION. 
    FILE SECTION. 

    WORKING-STORAGE SECTION. 
    01 TEST-STRING-1     PIC X(30) 
     VALUE 'green eggs and ham'. 
    01 TEST-STRING-2     PIC X(30) 
     VALUE 'green eggs and green ham'. 
    01 TEST-STRING      PIC X(30). 
    01 SEARCH-STRING     PIC X(05) 
     VALUE 'green'. 
    01 MATCH-COUNT      PIC 9. 
    01 SEARCH-INDEX      PIC 99. 
    01 MATCH-POSITIONS. 
     05 MATCH-POS     PIC 99 OCCURS 9 TIMES. 

    PROCEDURE DIVISION. 
    MAIN. 
     MOVE TEST-STRING-1 TO TEST-STRING 
     PERFORM FIND-MATCHES 

     MOVE TEST-STRING-2 TO TEST-STRING 
     PERFORM FIND-MATCHES 

     STOP RUN 
     . 

    FIND-MATCHES. 
     MOVE ZERO TO MATCH-COUNT 
     INSPECT TEST-STRING TALLYING MATCH-COUNT 
      FOR ALL SEARCH-STRING. 
     DISPLAY 'FOUND ' MATCH-COUNT ' OCCURRENCE(S) OF ' 
      SEARCH-STRING ' IN:' 
     DISPLAY TEST-STRING 
     DISPLAY 'MATCHES FOUND AT POSITIONS: ' WITH NO ADVANCING 
     PERFORM VARYING SEARCH-INDEX FROM 1 BY 1 
      UNTIL SEARCH-INDEX = 30 
      IF TEST-STRING (SEARCH-INDEX:5) = SEARCH-STRING 
       DISPLAY SEARCH-INDEX ' ' WITH NO ADVANCING 
     END-PERFORM 
     DISPLAY ' ' 
     DISPLAY ' ' 
     . 
+0

「UNTIL SEARCH-INDEX = 30」是不好的,你有一個30字節的字段,一旦你達到27,你就會看到你的表的「外部」知道OpenCobol如何「對齊」01,但如果IBM Enterprise Cobol和字段長度爲32,那麼如果第一個字符的第32個字符是「g」,而第二個字符的第四個字符是「reen」,則會得到「命中」。 – 2013-01-21 01:34:33

0

您可以在IBM使用QCLSCAN我

77 QCLSCAN-SRCHLEN   PIC S9(3)  COMP-3.   
77 QCLSCAN-STARTPOS   PIC S9(3)  COMP-3.   
77 QCLSCAN-PATLEN   PIC S9(3)  COMP-3.   
77 QCLSCAN-XLATE   PIC X(01)  VALUE "0".   
77 QCLSCAN-TRIM    PIC X(01)  VALUE "0".   
77 QCLSCAN-WILDCARD   PIC X(01)  VALUE LOW-VALUES. 
77 QCLSCAN-FOUNDPOS   PIC S9(3)  COMP-3.   
... 
... 
    MOVE LENGTH OF WRK-ACCT-NBR TO QCLSCAN-SRCHLEN 
    MOVE  1     TO QCLSCAN-STARTPOS 
    MOVE  9     TO QCLSCAN-PATLEN 
    MOVE "0"     TO QCLSCAN-XLATE 
    MOVE "0"     TO QCLSCAN-TRIM  
    MOVE "?"     TO QCLSCAN-WILDCARD 
    CALL "QCLSCAN" USING WRK-ACCT-NBR    
          QCLSCAN-SRCHLEN   
          QCLSCAN-STARTPOS   
          EMPLOYEE-SSN-9X   
          QCLSCAN-PATLEN   
          QCLSCAN-XLATE    
          QCLSCAN-TRIM    
          QCLSCAN-WILDCARD   
          QCLSCAN-FOUNDPOS   
    IF QCLSCAN-FOUNDPOS > ZERO      
* Found data in position QCLSCAN-FOUNDPOS 
    ELSE 
* Found no match 
    END-IF 
0
MOVE 1 TO WS-POINTER 

UNSTRING WS-STRING(1:WS-STRING-LEN) 
DELIMITED BY LT-MY-DELIMITER 
INTO WS-STRING-GARBAGE        
WITH POINTER WS-POINTER 
END-UNSTRING 

你問有關如何使用上述後續字符串。

可以通過兩種方式使用UNSTRING來獲得你想要的計數。要麼有多個接收字段和COUNT-IN,要麼通過每次使用先前UNSTRING的POINTER值執行多次UNSTRING執行。

您需要考慮分隔符的長度。然而,你最終會得到「非直觀」的代碼,每次有人拿起程序時都必須「理解」代碼。

相反,它是一個簡單的任務,其中「substring」處理取決於或引用修改(接受的答案中的方法)。

必須當count + length-of-delimiter = max-length-of-string-to-search時,通過結束搜索,確保您不會「超出字段的末端」。