LeonBr
LeonBr

Reputation: 11

SPLIT results with separator

I'm trying to split a string (separated with the HTML break tag), without deleting the break tag. I think it's pretty messy to add a break as string after splitting, so is there any function/possibility to keep the separator while "splitting"?

Example:

<HTML><BODY><p>some text<br/>some more text</p></BODY></HTML>

Expected result:

<HTML><BODY><p>some text<br/>
some more text</p></BODY></HTML>

Upvotes: 1

Views: 1613

Answers (2)

phil soady
phil soady

Reputation: 11348

Just use the replace command. replace <br/> with <br/>CR_LF
The CR_LF refers to the carriage return linefeed character. In more complex cases you can use regex expressions in abap.

class ZTEST_SO definition   public    create public .
public section.
methods t1.
ENDCLASS.
CLASS ZTEST_SO IMPLEMENTATION.
  METHOD T1.
     data: my_break type string,
            my_string type string
                    value '<HTML><BODY><p>some text<br/>some more text</p></BODY></HTML>'.

     my_break = '<br/>' && CL_ABAP_CHAR_UTILITIES=>CR_LF.
     replace all occurrences of '<br/>' in my_string with my_break in character mode.
     "check my_string in the debugger :)

     "<HTML><BODY><p>some text<br/>
     "some more text</p></BODY></HTML>
  ENDMETHOD.
ENDCLASS.

Upvotes: 1

Dirk Trilsbeek
Dirk Trilsbeek

Reputation: 6023

As far as I know SPLIT removes the separator from the results and it doesn't seem like you can change that.

But you could create your own separator by first replacing your <br/> tag with <br/> plus an arbitrary string that is highly unlikely to ever appear in your HTML source, and then split the HTML using this arbitrary string as a separator instead.

types:
  begin of t_result,
    segment(2000) type c,
  end of t_result.

DATA:
  source type string,
  separator type string,
  brtag type string,
  repl type string,
  result_tab type standard table of t_result,
  result_row TYPE t_result.

brtag = '<br/>'.
separator = '|***SEP***|'.
concatenate brtag separator into repl.
source = '<HTML><BODY><p>some text<br/>some more text</p></BODY></HTML>'.

replace all occurrences of brtag in source with repl.
split source at separator into table result_tab.

LOOP AT result_tab INTO result_row.
  WRITE:
  result_row-segment.
ENDLOOP.

Output of that example report:

<HTML><BODY><p>some text<br/>
some more text</p></BODY></HTML>

The caveat of this solution is that your custom separator, if not chosen with some care, might appear in your HTML source on its own. I therefore would choose an arbitrary string with a special character or two that would be encoded in HTML (like umlauts) and therefore not appear in your source.

Upvotes: 3

Related Questions