Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Split a string by second character

0 Likes
3,264

Hey guys,

first of all forgive my English, but I need your help with this.

I need to split a string by the second repetition of an hyphen, I know, I explain myself badly, but I give an example:

my string is like this:

CO-CODA (CODE) TA - 8 hours (500)

and I need it to be like this:

CO-CODA (CODE) TA

8 hours (500)

thanks

1 ACCEPTED SOLUTION
Read only

kaleem_ahmed
Explorer
0 Likes
2,468

As mentioned by Sandra you can do it by many ways. Here is how you will do it by Split keyword.

First split the string into three strings at the hyphen. (Str1, Str2, Str3)

Then concatenate the first two part (Str1, Str2) into result string separated by hyphen.

DATA text TYPE string.

text = `CO-CODA (CODE) TA - 8 hours (500)`. "Example Text

SPLIT text AT '-' INTO: DATA(str1) DATA(str2) DATA(str3).

CONCATENATE str1 str2 INTO DATA(result) SEPARATED BY '-'.

WRITE:/ result.

5 REPLIES 5
Read only

Sandra_Rossi
Active Contributor
2,468

You can do it in many ways, for example with SPLIT, segment, substring_before, match, and so on.

Read only

0 Likes
2,468

and if I don't know how many hyphens are in the string, how can I separate them in a way to have all the string until the last hyphen?

Read only

2,468

You can still do it with the statements above.

Read only

RaymondGiuseppi
Active Contributor
2,468

Just read the online (or F1) help on statement SPLIT.

Read only

kaleem_ahmed
Explorer
0 Likes
2,469

As mentioned by Sandra you can do it by many ways. Here is how you will do it by Split keyword.

First split the string into three strings at the hyphen. (Str1, Str2, Str3)

Then concatenate the first two part (Str1, Str2) into result string separated by hyphen.

DATA text TYPE string.

text = `CO-CODA (CODE) TA - 8 hours (500)`. "Example Text

SPLIT text AT '-' INTO: DATA(str1) DATA(str2) DATA(str3).

CONCATENATE str1 str2 INTO DATA(result) SEPARATED BY '-'.

WRITE:/ result.