ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Replacing chars in a string

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Replacing chars in a string

    Hi, a quick question. Is there a way to replace some chars in a string with another chars?

    I want to remove all double spaces in a string. For example, "this__is__a__string" should be processed and become "this_is_a_string"

    I tried to look into %replace, but either i don't understand its use or it doesn't do what i want.

  • #2
    Re: Replacing chars in a string

    Could you provide the spec?

    $replace...

    Comment


    • #3
      Re: Replacing chars in a string

      The string is a composition of 2 strings. I can't modify the program that generated that string, so i must resolve it in my program. The first one is sized 20 chars and the second one is sized 50 chars, for a total of 70.

      Often the first string has blanks at the end. Currently i am resolving this by using this code:

      Code:
      MYSTRING=%SUBST(MYSTRING:1:%SCAN('  ':MYSTRING))+%SUBST(MYSTRING:22:47)
      That way, it copies the first part of the string until it finds two spaces, and then copies from the 22nd char (leaving 1 as space) until the end. It's ugly, but it works.

      However, it would be much cleaner to use something like

      Code:
      mystring=%replace(mystring:'  ':' ')
      Similar like you can do in lots of other programming languages.

      Comment


      • #4
        Re: Replacing chars in a string

        Hi fjleon:

        Try this:

        Code:
        mystring70=%trim(mystring20) + ' ' + mystring50
        Best of Luck
        GLS
        The problem with quotes on the internet is that it is hard to verify their authenticity.....Abraham Lincoln

        Comment


        • #5
          Re: Replacing chars in a string

          Those two strings don't exist in my program, so i can't do that. I can only work with the resulting string. Thanks anyway

          Comment


          • #6
            Re: Replacing chars in a string

            Hi fjleon:

            Code:
            mystring20=%subst(mystring70 : 1 : 20)
            mystring50=%subst(mystring70 : 21 : 50)
            mystring70=%trim(mystring20) + ' ' + mystring50
            or

            Code:
            pos=%scan('  ' : mystring70)
            mystring70=%replace('' : mystring70 : pos :1)
            see this link:


            search for "delete"

            Best of Luck
            GLS
            The problem with quotes on the internet is that it is hard to verify their authenticity.....Abraham Lincoln

            Comment


            • #7
              Re: Replacing chars in a string

              This is what I would do,

              In the d-specs...

              Define your mystring in a data structure and overlay it with a 20 char string and a 50 char string.

              PHP Code:
               D                 DS
               D  mystring               1     70
               D  mystring20             1     20
               D  mystring50            21     70 
              Then as GLS400 said...

              PHP Code:
              mystring = %Trim(mystring20) + ' ' mystring50 
              Ben

              Comment


              • #8
                Re: Replacing chars in a string

                Try this.
                Manual/old way on RPGLE. Please try to use the variable(K01 & K02).

                C IF OUTCHAR <> ' '
                C K01 SCAN OUTCHAR KCPOS 3 0 90
                C DOW *IN90= '1'
                C Eval %Subst(OUTCHAR:KCPOS:1) = K02
                C K01 Scan OUTCHAR KCPOS 90
                C ENDDO

                Comment


                • #9
                  Re: Replacing chars in a string

                  Hello

                  I hope this helps you

                  I wrote this one quickly while waiting for my batch job. It reduces all instances of two, three or however many spaces to a single space.

                  The trick to stop it looping at the end of the string is in the condition on the Dow statement:

                  PHP Code:
                  D TestString      S             30    inz('this  is  a  string')                 
                  D Pos             S              3  0                                            
                  D TwoSpace        S              2    inz
                  (*Blanks)                               
                  C                   Eval      Pos = %Scan(TwoSpace:TestString)                   
                  C                   Dow       Pos and Pos < %Len(%Trim(TestString))          
                  C                   Eval      TestString =                                       
                  C                                %Trim(%SubSt(TestString:1:Pos)) + ' ' +         
                  C                                %Trim(%Subst(TestString:Pos))                   
                  C                   Eval      Pos = %Scan(TwoSpace:TestString)                   
                  C                   Enddo                                                        
                  C                   Seton                                        LR 

                  Comment


                  • #10
                    Re: Replacing chars in a string

                    I know that this is an old thread, but I need to be able to do something like this. I need to search a string for a double quote, and for every double quote that it finds, I want it to replace it with a backslash and a double quote. So, replace every " with a \". I've tried to get %replace and %xlate to work, but I just can't seem to. Is there a way in RPG to do this?

                    Comment


                    • #11
                      Re: Replacing chars in a string

                      Hi,

                      have you tried the following:

                      PHP Code:
                      /Free &#37;
                         
                      NewString = %XLate('"''\': OrigString);
                      /End-Free 
                      Birgitta

                      Comment


                      • #12
                        Re: Replacing chars in a string

                        %Xlate can only replace one character at a time.
                        You have to proceed in two steps : scan + replace
                        Try this.
                        Code:
                        D StartPos        s             10u 0 
                        doU StartPos = 0;                                     
                          StartPos = %Scan('"': SrcString : StartPos + 2 );   
                          if StartPos = 0;                                    
                            Leave;                                            
                          endif;                                              
                          SrcString  = %Replace('\': SrcString : StartPos: [COLOR=red][B]0[/B][/COLOR]);
                        enddo;
                        The fourth parameter of %Replace() represents the number of characters in the source string to be replaced. If zero is specified, then the replacement string is inserted before the specified starting position.
                        Last edited by Mercury; December 10, 2008, 04:18 AM.
                        Philippe

                        Comment


                        • #13
                          Re: Replacing chars in a string

                          %xlate didn't work because I was trying to replace every '"' with a '\"'. Thus, I would be adding 1 to the size of the string. I ended up using %scan and %replace in the following code:

                          Code:
                                   desc = I2DESC;
                          
                                   pos = %scan('"':desc);
                                   DoW pos > 0;
                                     desc = %replace('\"':desc:pos:1);
                                     If (pos+2 <= %len(desc));
                                       pos = %scan('"':desc:pos+2);
                                     Else;
                                       pos=0;
                                     ENDIF;
                                   ENDDO;

                          Comment


                          • #14
                            Re: Replacing chars in a string

                            must use the %replace i posted something on that
                            here on the forum bout 200 years ago

                            I am down to two days before converting the rest of our
                            companies to exchange and dont think i will have time..

                            jamie
                            All my answers were extracted from the "Big Dummy's Guide to the As400"
                            and I take no responsibility for any of them.

                            www.code400.com

                            Comment


                            • #15
                              Re: Replacing chars in a string

                              Apparently you didn't put in practise the loop I suggested in my previous post, seems more elegant though ? Only one %scan, only one %replace, no need to test against the source string length...
                              Last edited by Mercury; December 10, 2008, 09:39 AM.
                              Philippe

                              Comment

                              Working...
                              X