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 "?" ""}}

I’m expecting this to return the following string:

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


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 "" "?" }}

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 "" }}
{{ 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.