Is this not how TrimRight is supposed to work?

I’m trying to remove a portion of text after a specific character in a string using TrimRight but it doesn’t seem to work or maybe I’m not understanding how TrimRight is supposed to work.

Here is my sample code:

{{strings.TrimRight "?" "https://img.youtube.com/vi/hyUIbgT2huM?si=824Q8a-GvDNdNAj8"}}

I’m expecting this to return the following string:

https://img.youtube.com/vi/hyUIbgT2huM

For some reason it does not do this and I have no clue as to why, any help is greatly appreciated.

Thanks.

I managed to achieve what I wanted using this method but I still want to understand why it doesn’t work using TrimRight or TrimSuffix.

Here is my solution:

{{ $poster := split "https://img.youtube.com/vi/hyUIbgT2huM?si=824Q8a-GvDNdNAj8" "?" }}

Then I used this to render the image:

{{index $poster 0}}

strings.TrimRight takes a cutset argument, and removes those characters (in any order) from the end.

strings.TrimSuffix takes a string argument, and removes that string from the end.

You can also work with the URL components by parsing it:

{{ $u := urls.Parse "https://img.youtube.com/vi/hyUIbgT2huM?si=824Q8a-GvDNdNAj8" }}
{{ printf "%s%s" (strings.TrimSuffix $u.RequestURI $u.String) $u.Path }}
1 Like

@jmooring thanks for the explanation and for showing me another way to accomplish this using urls.Parse :heart:

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.