$String.split introduces extraneous "\"

Hi,

I have a TBX file for vocabulary (Welsh, but I don’t think that matters here…).

The $Name of each vocabulary note contains the Welsh headword, an en-dash, then the English definition. E.g.

am yr ail – alternatively

The prototype has the following code to split the name into two attributes $LangWelsh and $LangEnglish, based on the en-dash (which is only ever used once in a $Name):

	$MyList=$Name.split("–");
	$LangWelsh = $MyList.at(0);
	$LangEnglish = $MyList.at(1);

This works very well, unless there is an apostrophe in the $Name, when it becomes escaped with \:

(Note that $Names have dumb apostrophes, ' not .)

I can work around this with an additional .replace("\","") of course, but I wondered whether this is simply an inevitable artefact of the way split works, or perhaps a bug?

Or more likely, am I doing something wrong?

(BTW, $FirstLetter and $MySet are to deal with the fact that Welsh treats the letters ‘ll’, ‘dd’ etc as single letters, so I need to do some manipulation to get them to sort in the right order…)

Many thanks,

David

[edit: code correction is response to next post. Mea culpa!]

Yes, I see the issue.I don’t think it is .split() so much as the List creation ‘escaping’ a an unclosed single-quote delimited string. Not what you intended, but seemingly the code’s understanding of what it gets. For instance:

$MyList=$Name.split(" – ").replace("\\","");

Still generates a \r in the target List.

Either way, this still is not the desired result. A workaround for now (tested):

var:list vList=$Name.split(" – ");
$LangWelsh = vList.split(" – ").at(0).replace("\\","");
$LangEnglish = vList.split(" – ").at(1).replace("\\","");

Notes:

  • The spit, you actually want '- ’ , i.e. with the padding spaces, not ‘-’ to avoid stray white space (seen in $MyList in your code).
  • New the code works, we don’t want to use an attribute (that we don’t reset) so store an interim state. A ‘list’ type variable suffices.
  • To replace a backslash—Tinderbox’s own escape character_we need to escape it, so \\ not \.

That makes sense – thanks very much for the explanation and the smoothing out of my rough code!

Apologies, copied code direct from different tests. My earlier example above is now correct, as per you observation. :slight_smile:

1 Like

I’ll get this fixed. In the interim, consider typographic apostrophe ’ (cmd-shift-] on US keyboards) as a better apostrophe here.

and on UK keyboards a closing single typographic (‘curly’) quote is Opt+Shift+] ( ⌥+⇧+]).

Which is also true for US keyboards. This was a glitch in the prior response.