Some of the book titles include metadata — a real bugbear of mine. For example, The Ashes of London (James Marwood & Cat Lovett, Book 1) or Faithful Place (Dublin Murder Squad, Book 3). In each case the words in brackets are not part of the title and I’d like to be able to omit them. Is there a function in Hugo or Go that will tell me the position of a particular character in a string — in this case “(“? Then I could use substr to include only the characters before that position.
I gave up on the idea of using regular expressions: the implementation in Hugo seems odd, to say the least. Instead, I tried using split to turn my book title string into an array of substrings, delimited by the open round bracket character, “(”. I thought this might work, but it turns every title (whether it contains “(” or not) into "s%!”. I have no idea why it’s doing that, or what it means. Here is the relevant snippet from my template:
Any suggestions gratefully received. Incidentally, the poetry critic John Leonard calls round brackets “lunulae”, “little moons”. I’d call them that but I’m afraid people wouldn’t know what I mean.
OK, this is utterly bizarre. Instead of using 0 as the index, I tried 3 (blank result), then 2 (I got the words in brackets that I was trying to remove), then 1 (I got the title but preceded by “EXTRA string=” and followed by a closing lunula). I have absolutely zero idea what’s going on or why, but it should be straightforward to replace the extra text with nothing, so I think I’m nearly there.
I used two replaces in a row rather than substr because not all the titles had a final closing lunula — only those which didn’t have anything between lunulae to start with, oddly enough. I’m sure there’s a more elegant way to do it but this works, which is all I wanted. Thanks to all who read and to Jason for responding.