Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session
SOLVED

Select text between two symbols

Avatar

Level 2

I have a table that gets populated with different strings of text. Sometimes there will be lines of text that are surrounded by a "|" (pipe) symbol within them.

For example, This | is a example | line of text

I currently have some coding that will remove the pipe symbols from the text,

DATA: i_TABLE LIKE TABLE.
LOOP AT TABLE INTO i_TABLE.
REPLACE ALL OCCURRENCES OF '|'  IN i_TABLE-FIELD1
      WITH ''.
MODIFY TABLE FROM i_TABLE.

RESULT: This is a example line of text

What I need to do now is make an exception for a certain category that will only select the text from within the pipes (and remove the pipes)

DESIRED RESULT: is a example

I am not sure how to structure that code. can anyone give me some suggestions?

Best Regards,

Andrew

1 Accepted Solution

Avatar

Correct answer by
Level 10

Hi Andrew,

You can use the split method to break the string into an array and then grab the second occurrence.

var textArray = "This | is a example | line of text".split("|");

var secondText = textArray[1];

If you need the remove the leading and trailing spaces, and all your users have Reader DC then you can use;

seondText.trim()

Regards

Bruce

View solution in original post

1 Reply

Avatar

Correct answer by
Level 10

Hi Andrew,

You can use the split method to break the string into an array and then grab the second occurrence.

var textArray = "This | is a example | line of text".split("|");

var secondText = textArray[1];

If you need the remove the leading and trailing spaces, and all your users have Reader DC then you can use;

seondText.trim()

Regards

Bruce