Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

// request.cf · coarse context

A page that knows where it met you.

Only coarse request metadata is shown. This demo does not display or persist visitor IP addresses.

Country
US
Cloudflare location
CMH
Connection
HTTP/2
Language
Not provided

Ray ID: a23b2491fdd9c125

Jump to content

Template talk:Str find

Page contents not supported in other languages.
Add topic
From Wikipedia, the free encyclopedia
Latest comment: 4 months ago by SeaDragon1 in topic Suggestion

Whitespace

[edit]

These examples:

{{str find|test test| }}
{{str find|test test| }}
{{str find|test test|2= }}
{{str find|test test|2= }}

results in: 5, -1, 1, -1.

Is there a way to make it work with whitespace? Fulcanelli (talk) 08:13, 30 April 2009 (UTC)Reply

I've fixed it to work for case 1. I just knew when they changed str_len to ignore whitespace it would mess other things up. Dragons flight (talk) 13:43, 30 April 2009 (UTC)Reply
Thanks a lot! Fulcanelli (talk) 02:44, 2 May 2009 (UTC)Reply

Usage

[edit]

"Only searches first 50 characters of text. Returns -1 if sub_string not found in first 80 characters."; that does not seem correct... - Erik Baas (talk) 15:52, 24 June 2010 (UTC)Reply

Rewrite to avoid 16 levels of 41 depth limit

[edit]

I have written a simplified variation, as Template:Strfind_short, to use far less in wiki-server resources to find the same strings. The new variation uses only 6 levels of the expansion depth limit, while the older template {str_find} uses (an almost unbelievable) 22 levels, explained due to {str_find} invoking {{str_len}} which uses 9 levels alone. For example the string "45" is located at position 4 in "123456789", however, {str_find} cannot process that case when nested 23 deep of the total 41 expansion levels, so it reports "-1" instead of the correct position as 4:

: Nest 23 + {str_find|123456789|45|2}: <!--
-->{{1x|{{1x|{{1x|{{1x|{{1x|{{1x|{{1x|{{1x|{{1x|{{1x
|{{1x|{{1x|{{1x|{{1x|{{1x|{{1x|{{1x|{{1x|{{1x|{{1x
|{{1x|{{1x|{{1x
|{{str_find|123456789|45|2}}
}}}} }}}} }}}} }}}} }}}}  }}}} }}}} }}}} }}}} }}}}
}}}} }}
Nest 23 + {str_find|123456789|45|2}: -1
Nest 18 + {str_find|123456789|45|2}: 4

When nested only 18 deep, then {str_find} can finally match "45" at position 4, without exceeding the expansion-depth limit. However, new template {{strfind_short}} can process the same search for "45" when nested inside 34 other templates, at 16 levels deeper than {str_find} requires:

Nest 34 + {strfind_short|123456789|45|2}: 4

This improved efficiency requires the length of the 2nd string to be passed into the search, or alternatively, a page can use quick template {{strlen_short}} to get the string length to pass.

{{strfind_short|abcddeabcde-abcdeabcde|cde|3}} 9
{{strfind_short|abCDdeabeRTEert|e|1}} 6

I am still testing the new {{strfind_short}} to verify it can handle other strings correctly. -Wikid77 (talk) 18:58, 28 December 2010, revised to fit as 18-nested {str_find} 02:30, 19 November 2012 (UTC)Reply

Test cases

[edit]

Here are some test cases:

  • 1 = 1
  • -1 = -1
  • -1 = -1
  • 1 = 1
  • 2 = 2
  • 2 = 2

All correct?--Lashuto (talk) 02:31, 10 March 2011 (UTC)Reply

Testing

[edit]
  • {{str_find}} = 1
  • {{str_find|}} = 1
  • {{str_find||}} = 1
  • {{str_find|Bob|}} = 1
  • {{str_find||Bob}} = -1
  • {{str_find|Bob|Joe}} = -1
  • {{str_find|BobBob|bB}} = 3
  • {{str_find|Bob|B|}} = 1
  • {{str_find|Bob|o|}} = 2
  • {{str_find|Bob|b|}} = 3
  • {{str_find|o|Bob}} = -1
  • {{str_find| Bob |o}} = 2
  • {{str_find| Bob Jones |b J}} = 3
  • {{str_find|aaa aaa| a}} = 4
  • {{str_find|aaa aaa|a }} = 3
  • {{str_find|높지만 급격히|만}} = 3
  • {{str find|test test| }} = 5
  • {{str find|test test| }} = -1
  • {{str find|test test|2= }} = 1
  • {{str find|test test|2= }} = -1

  • {{str_find/sandbox}} = 1
  • {{str_find/sandbox|}} = 1
  • {{str_find/sandbox||}} = 1
  • {{str_find/sandbox|Bob|}} = 1
  • {{str_find/sandbox||Bob}} = -1
  • {{str_find/sandbox|Bob|Joe}} = -1
  • {{str_find/sandbox|BobBob|bB}} = 3
  • {{str_find/sandbox|Bob|B|}} = 1
  • {{str_find/sandbox|Bob|o|}} = 2
  • {{str_find/sandbox|Bob|b|}} = 3
  • {{str_find/sandbox|o|Bob}} = -1
  • {{str_find/sandbox| Bob |o}} = 2
  • {{str_find/sandbox| Bob Jones |b J}} = 3
  • {{str_find/sandbox|aaa aaa| a}} = 4
  • {{str_find/sandbox|aaa aaa|a }} = 3
  • {{str_find/sandbox|높지만 급격히|만}} = 3
  • {{str find/sandbox|test test| }} = 5
  • {{str find/sandbox|test test| }} = -1
  • {{str find/sandbox|test test|2= }} = 1
  • {{str find/sandbox|test test|2= }} = -1

Bug with this template

[edit]

 You are invited to join the discussion at Wikipedia:Village pump (technical) § String find bug. {{u|Sdkb}}talk 23:10, 1 December 2021 (UTC)Reply

Suggestion

[edit]

Could we have it return 0 if it can't find it? In JavaScript, "test string".indexOf("hello") returns -1. Since the index is 1-based (not 0-based), then we have to do -1 + 1 to find what it would return in a 1-based index. -1 + 1 = 0, so it should return 0 if it doesn't find it.
Why? Because if it returns 1, it could also mean that it found it at the start of the string. If it returned 0 if it didn't find it, people could know that it couldn't find the substring.

Thank you for reading.
And no, I did not use AI to write this, just so you know. SeaDragon1 (talk, contributions) 17:27, 7 March 2026 (UTC)Reply

Also: yes, I am completely aware of how many changes this would require (~282000 pages), but it would allow people to know if it matched the start of the string, or if it just didn't find it.
Hopefully, this won't blow up. SeaDragon1 (talk, contributions) 17:37, 7 March 2026 (UTC)Reply
Ah, never mind. I misread If sub_string is empty, this routine returns 1. as If no match is found, this routine returns 1.. Please disregard the above comments. SeaDragon1 (talk, contributions) 17:39, 7 March 2026 (UTC)Reply