Skip to Content
author's profile photo Former Member
Former Member

String concatenate Issue (space is not adding)

Hello Experts,

I've wrote a custom string function to remove commas in Names or address fields. The following code works fine if there is NO SPACE in the given string as it remove all the commas. (i.e D,J,UICE = DJUICE). However, issue happens if I provide a string like this (D,J JE,FF S,AM ,D,ONE.). Instead of making it (DJ JEFF SAM DONE) it makes it like this (DJJEFFSAMDONE).

Any clue how I can convert it into my desired output, which is DJ JEFF SAM DONE.

Here is my code.

FORM REMOVE_COMMAS CHANGING pgv_change_text TYPE string.

DATA: i type i,
gv_length type i,
stest(100),
sakt(1),
str1(100).

CONSTANTS: c_deli TYPE c value ' '.

"Check length
gv_length = strlen( pgv_change_text ).

WHILE i < gv_length.
sakt = pgv_change_text+i(1).
if sakt = ','.
sakt = ''.
elseif sakt = ' '.
sakt = c_deli.
endif.
"shift stest.
concatenate str1 sakt into str1.

i = i + 1.
ENDWHILE.
pgv_change_text = str1.


ENDFORM.

Many thanks in advance.

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

8 Answers

  • Best Answer
    author's profile photo Former Member
    Former Member
    Posted on May 31, 2013 at 04:49 PM

    Try this:

    DATA: str TYPE string.

    str = 'D,J JE,FF S,AM ,D,ONE'.

    REPLACE ALL OCCURRENCES OF ',' IN str WITH ''.

    WRITE str.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on May 31, 2013 at 04:53 PM

    Use 'REPLACE ALL OCCURRENCES OF ',' <string> with ' '

    Neha

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on May 31, 2013 at 05:03 PM

    This code will give you the desired output.

    FORM REMOVE_COMMAS CHANGING pgv_change_text TYPE string.

    DATA: i TYPE i,

    gv_length TYPE i,

    stest(100),

    sakt(1),

    str1(100),

    lv_text(80) TYPE c.

    CONSTANTS: c_deli TYPE c VALUE '_'.

    i = 0.

    "Check length

    gv_length = strlen( pgv_change_text ).

    BREAK-POINT.

    WHILE i < gv_length.

    sakt = pgv_change_text+i(1).

    IF sakt = ','.

    sakt = ''.

    ELSEIF sakt = ' '.

    sakt = c_deli.

    ENDIF.

    "shift stest.

    CONCATENATE str1 sakt INTO str1.

    i = i + 1.

    ENDWHILE.

    i = 0.

    lv_text = str1.

    WHILE i < gv_length.

    CLEAR sakt.

    sakt = lv_text+i(1).

    IF sakt = c_deli.

    REPLACE '_' WITH ' ' INTO lv_text.

    ENDIF.

    i = i + 1.

    ENDWHILE.

    pgv_change_text = lv_text.

    ENDFORM.


    Cheers!

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on May 31, 2013 at 05:11 PM

    Hi,

    Try this code using REPLACE keyword.

    FORM REMOVE_COMMAS CHANGING pgv_change_text TYPE string.

    DATA: i type i,
    gv_length type i,
    stest(100),
    sakt(1),
    str1(100).

    CONSTANTS: c_deli TYPE c value ' '.

    REPLACE ALL OCCURRENCES OF ',' IN pgv_change_text WITH ''.


    ENDFORM.

    Thats it.

    Regards

    Satish

    Add a comment
    10|10000 characters needed characters exceeded

  • author's profile photo Former Member
    Former Member
    Posted on May 31, 2013 at 04:11 PM

    How about replace , with space. Then split on space. Then concatenate separated by space.

    Neal

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on May 31, 2013 at 04:11 PM

    hello.

    use

    concatenate str1 sakt into str1 separated by space.

    Regards

    Add a comment
    10|10000 characters needed characters exceeded

    • Former Member

      Thanks for both of your inputs. I've already tried SEPERATED BY SPACE but then everything get's wrong and I get D J J E F F S A M D O N E. I don't want this output what I need is this ->DJ JEFF SAM DONE.

  • Posted on May 31, 2013 at 04:25 PM

    Use REPLACE statement, as Neal Wilhite comment.

    REPLACE [{FIRST OCCURRENCE}|{ALL OCCURRENCES} OF] pattern

    IN [section_of] dobj WITH new

    [IN {CHARACTER|BYTE} MODE]

    [replace_options].

    Example:

    REPLACE ',' IN pgv_change_text WITH space.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on May 31, 2013 at 04:42 PM

    Hello,

    Change constant value C_deli to a spl character e.g. '&' then use replace.

    DATA: i type i,

    gv_length type i,
    stest(100),
    sakt(1),
    str1(100).

    CONSTANTS: c_deli TYPE c value ' '.

    "Check length
    gv_length = strlen( pgv_change_text ).

    for code refer to this link http://scn.sap.com/thread/1222123

    Cheers!

    Sree

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.