'String.IndexOf() returns unexpected value - cannot extract substring between two search strings

Script to manipulate some proper names in a web story to help my reading tool pronounce them correctly.

I get the content of a webpage via

$webpage = (Invoke-WebRequest -URI 'https://wanderinginn.com/2018/03/20/4-20-e/').Content

This $webpage should be of type String.

Now

$webpage.IndexOf('<div class="entry-content">')

returns correct value, yet

$webpage.IndexOf("Previous Chapter")

returns unexpected value and I need some explanation why or how I can find the error myself.

In theory it should cut the "body" of the page run it through a list of proper nouns I want to Replace and push this into a htm-file. It all works, but the value of IndexOf("Prev...") does not.

Edit: After invoke-webrequest I can

Set-Clipboard $webrequest

and post this in notepad++, there I can find both 'div class="entry-content"' and 'Previous Chapter'. If I do something like

Set-Clipboard $webpage.substring(
     $webpage.IndexOf('<div class="entry-content">'),
     $webpage.IndexOf('PreviousChapter')
   )

I would expect Powershell to correctly determine both first instances of those strings and cut between. Therefore my clipboard should now have my desired content, yet the string goes further than the first occurrence.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source