Substring filter?

291 views
Skip to first unread message

Hubert

unread,
Nov 15, 2019, 6:43:00 AM11/15/19
to TiddlyWiki
Hello,

Does a substring filter exist in the core? I couldn't find anything in the list of operators that would extract a string of text beginning and ending at a specific location within the string (expressed in integers).

I'm aware that this can be solved with Evan's excellent formula plugin (and I'm using it, among others, for this purpose) but I wanted to know where we stand in case of core capabilities regarding substrings.

Many thanks,
Hubert

Jed Carty

unread,
Nov 15, 2019, 7:38:01 AM11/15/19
to TiddlyWiki
I don't think that we have a core substring filter, the only way that I know of to do it is by some creative usage of the split, butfirst, first and join operators.

Which is probably not a practical usage, but it is possible.

\define subStr(input, start, end)
<$set name=len filter='[<__end__>subtract<__start__>]'>
<$list filter='[<__input__>split[]butfirst<__start__>first<len>join[]]'>

</$list>
</$set>
\end

<<subStr """hello""" 1 4>>

it contains the character at the index given for start and not the character given for end. And uses 0-based indexing.

Probably not useful, but I believe that is the current state of substrings in the core.

Jed Carty

unread,
Nov 15, 2019, 7:47:46 AM11/15/19
to TiddlyWiki
It just occurred to me that the subfilter operator may make this usable like this:

\define substr() [split[]butfirst<start>first<len>join[]]

<$vars start=2 len=4>
<$list filter='[[some string thing]subfilter<substr>]'>

</$list>
</
$vars>

you would have to calculate len (end-start) outside the main filter like in the previous example, or know what it is ahead of time, but this may be usable.

Hubert

unread,
Nov 15, 2019, 7:51:42 AM11/15/19
to TiddlyWiki
Wow! This is great. Thank you Jed for these solutions.

Regards,
Hubert

TonyM

unread,
Nov 15, 2019, 4:56:25 PM11/15/19
to TiddlyWiki
Hubert

Using add/remove prefix and suffix addresses some cases and those commonly faced manipulating titles.

Regex is also a friend in string manipulation.

Regards
Tony

Mohammad

unread,
Mar 28, 2020, 2:54:00 PM3/28/20
to tiddl...@googlegroups.com
This is a modified version of substr proposed by Jed and it works amazing!

\define substr(string, start, length)
<$list filter="[<__string__>!is[blank]]+[<__start__>!is[blank]]" variable=ignore>
<$vars Length={{{ [[$length$]!is[blank]then[$length$]]~[[$string$]length[]] }}}  >
<$text text={{{ [<__string__>split[]butfirst<__start__>first<Length>join[]] }}} />
</$vars>
</$list>
\end

<<substr "This_is_a_Test" 1 3>>

<<substr "This_is_a_Test" 5>>

<<substr "This_is_a_Test" -5>>

<<substr "This_is_a_Test" -10 6>>

The two inputs string and start are required and the last input, length is optional.
Macro checks if the first two parameters are missing!

It gives amazing but correct results! Give a try in tiddlywiki.com.
Note to the minus value for start and It takes characters from the end!

--Mohammad


Mohammad

unread,
Mar 28, 2020, 3:22:43 PM3/28/20
to TiddlyWiki
In previous post the first $list seems incorrect! So, for now ignore it and use the revised substr code as below:

\define substr(string, start, length)

<$vars Length={{{ [[$length$]!is[blank]then[$length$]]~[[$string$]length[]] }}}  >
<$text text={{{ [<__string__>split[]butfirst<__start__>first<Length>join[]] }}} />
</$vars>
\end


I am thinking not check the input at all or find a better solution!

--Mohammad

Mohammad

unread,
Mar 28, 2020, 3:29:29 PM3/28/20
to TiddlyWiki
This seems the fail-safe one

\define substr(string, start:0, length)
<$vars Length={{{ [[$length$]!is[blank]then[$length$]]~[[$string$]length[]] }}}  >
<$text text={{{ [<__string__>split[]butfirst<__start__>first<Length>join[]] }}} />
</$vars>
\end


See these examples:

# <<substr>>
# <<substr string:"Test">>
# <<substr start:1>>
# <<substr length:2>>
# <<substr string:"Test" length:2>>
# <<substr string:"Test" start:1 length:2>>
# <<substr string:"Test" start:-4 length:2>>



Mohammad

unread,
Mar 28, 2020, 3:32:03 PM3/28/20
to TiddlyWiki
This works very similar (almost exactly) to PHP substr function

--Mohammad

TonyM

unread,
Mar 28, 2020, 5:46:21 PM3/28/20
to TiddlyWiki
Thanks Mohammad

Tony

Wolfgang Scherer

unread,
May 9, 2022, 11:52:23 AM5/9/22
to TiddlyWiki
It took me a while to find out, but filter operators are basically Javascript only. so for future reference, I wrote a fancy substring operator myself. It is available at
https://sw-amt.ws/tiddly/tw-substring.html

On Saturday, March 28, 2020 at 10:46:21 PM UTC+1 TW Tones wrote:
Thanks Mohammad

Tony

Jeremy Ruston

unread,
May 9, 2022, 1:07:25 PM5/9/22
to TiddlyWiki Group
It took me a while to find out, but filter operators are basically Javascript only. so for future reference, I wrote a fancy substring operator myself. It is available at
https://sw-amt.ws/tiddly/tw-substring.html

Funnily enough, we are working on a new PR which allows custom filter operators to be defined in wikitext. There are more details here:


Best wishes

Jeremy

James

unread,
Jun 3, 2022, 9:11:30 AM6/3/22
to TiddlyWiki
Hi all,

Does anyone know how to extract portion of the tiddler?  Like to result show in Google.

I have numerous tiddler with few short paragraphs.  I wanted to show only the first 100 words of the text on the listing.

I am glad if someone could share a simple script on how to do it.  Thanks.
Reply all
Reply to author
Forward
0 new messages