Skip to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Replace function, mutiple replaces like the substitute?

Featured Replies

Hi Guys,

I've got a script which I need to replace multiple items. I can't use the substitute command as I need to be precise in what I am replacing, so I have a let function which determines the position and length of items and want to use the replace function. This all seems to work well, however I have come at a cross road where I need to replace 3 items in my $variable, so I have placed 3 replace functions in my script. The problem is my output is also tripling, just want to output 1 set of data.

 

Let([
    
   I declare all my variables which obtain the start and length...


];

Case(
    // If its the first one in the loop keep the start date but replace the enddate
    $j = 0 ; Replace ( $variable ; pos_endtime ; length_endtime ; "18:00:00" ) ;

 

/* THIS IS WHERE I AM HAVING TROUBLE I WANT TO REPLACE 3 ITEMS in my $variable*/ 
    // If its greater than the first day and meets the last day replace start date
    // Start Date needs to be incremented
    $j > 0 and $j = $day_count ;
    
    Replace ( $variable ; pos_endtime ; length_endtime ; $endtime ) &
    Replace ( $variable ; pos_starttime ; length_starttime ; "8:00:00" ) &
    Replace ( $variable ; pos_startdate ; length_startdate ; start_date_new ) ;


    $j <> 0 and $j <> $day_count ;
    Replace ( $variable ; pos_startdate ; length_startdate ; new_date ) and
    Replace ( $variable ; pos_starttime ; length_starttime ; "8:00:00" ) &
    Replace ( $variable ; pos_endtime ; length_endtime ; "18:00:00" ) ; 

)

 


)

 

Thanks in advance

This is very confusing and impossible to follow.

In general, if you want to replace multiple strings in a given text, you need to nest the Replace() function calls - for example:

Replace ( Replace ( "hh:mm" ; 1 ; 2 ; "12" ) ; 4 ; 2 ; "45" )

returns "12:45".

By using the Let() function, you can do the replacements in series, which makes for a clearer code. Here's an example of the same thing as before:

Let ( [
text = "hh:mm" ;
result = Replace ( text ; 1 ; 2 ; "12" ) ;
result = Replace ( result ; 4 ; 2 ; "45" )
] ;
result
)

 

Important:
Each Replace() operates on the result of the preceding Replace()) calls. You must take this into consideration when providing the start and numberOfCharacters parameters, since the replacementText can easily have different length than the text being replaced. For example:

Let ( [
text = "hh:mm" ;
result = Replace ( text ; 1 ; 2 ; "9" ) ;
result = Replace ( result ; 4 ; 2 ; "45" )
] ;
result
)

returns "9:m45" instead of the naively expected "9:45".

 

1 hour ago, Jalz said:

I can't use the substitute command as I need to be precise in what I am replacing

I believe that's worth reconsidering.

 

 

2 hours ago, Jalz said:


    Replace ( $variable ; pos_endtime ; length_endtime ; $endtime ) &
    Replace ( $variable ; pos_starttime ; length_starttime ; "8:00:00" ) &
    Replace ( $variable ; pos_startdate ; length_startdate ; start_date_new ) ;

 

Just to pick one thing out:  the use of "&" is to concatenate parts of a string, not to execute commands in succession.  The commands will get executed but the result of the first will be concatenated with the result of the 2nd and 3rd.

Maybe it would be easier to declare multiple variables, with simpler chunks of data, which you could then manipulate as needed and assemble at the end. That's the beauty of using a script, you don't have to cram all your logic into a single god object.

Create an account or sign in to comment

Important Information

By using this site, you agree to our Terms of Use.

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.