Tinderbox Forum

Substring: I don't understand this behavior

Hi there,

I want to create a value for a KA from the $Name of the notes. The rule is:

$Anfrage = $Name.substr($Name.find("ANF-").toNumber,14);

So I’d expect to get a string, that starts with “ANF-”. But I get something like this:

So some of the strings start correctly with the “ANF-”, and others with the “F-” of the “ANF-”. What could be wrong here?

Suggestion: store the result of

$Name.find(“ANF-”)

in a handy numeric attribute. See whether it’s what you expect it to be.

No… it is not.
I took another screenshot, this time with a monospace font, and you can see the “ANF-” perfectly aligned in the 4th and 5th row, while the attribute says “23” for the 4. row and “25” for the 5. row…
That is also the point, where the “ANF-” becomes “F-”

The rule is:

$Anfrage = $Name.substr($Name.find("ANF-").toNumber,14);
$MyNumber = $Name.find("ANF-");

Edit: When I delete one of the spaces after the AW_ at the beginning and add again one space, then the 25 becomes a 23 and the result is correct. But only with the second one (of two). BTW, those $Names are file names of a watched folder, so those spaces are part of the file name. Perhaps that helps…
Also cutting the complete name and re-pasting does the trick.
But it should somehow work automatically :slight_smile:

There seems to be some whitespace left of the “IngSoft”, which is not a normal space. I could get rid of it with a stamp like:

$Name = $Name.replace("\sIngSoft", "IngSoft");

and taking the watch folder offline.
But a little question still remains…

Ah – I bet I know the problem. As I recall, find() and substr() don’t know about (fairly unusual) unicode code points that require 2 words in UTF-16 and 4 in UTF-8.

This is a case for using regular expressions. If think something like

$Name.contains(".(ANF.)_")

would leave your code in $1, ready to be stored where you want it.

Interesting as I thought that when titling a note (manually), Tinderbox discarded leading spaces. I guess the leading spaces in the names here were due to some other means of creation, such as file import?

Yes… these were imported files, and the leading spaces as well as those preceding the word “IngSoft” were the culprits.