Processing the content generated by a node? What call would I use to modify a node's content. For example, let's say I have the words "12345678" located in various nodes. If I wanted to replace all occurences of this in Drupal 7 with "abcdefgh", I could easily hack it in a template like this:

$content = str_replace("12345678", "abcdefgh", $content);
print $content;


This is probably very bad practice, but hopefully it will demonstrate that I just want to be able to replace the occurrence of any text with some other piece of text.

What is the best way to do that in Drupal 8?

UPDATE

I had a look at template_preprocess_node. But it looks like this will allow me to add more variables to the theme, but not to actually replace specific pieces of text.
In Drupal 8, you can use hook_ENTITY_TYPE_view_alter() (which in this case would be hook_node_view_alter()) to alter the render array for the entity; otherwise, as the documentation for that hook suggests, you can use hook_preprocess_HOOK() (which in this case would be hook_preprocess_node()).


  If a module wishes to act on the rendered HTML of the entity rather than the structured content array, it may use this hook to add a #post_render callback. Alternatively, it could also implement hook_preprocess_HOOK() for the particular entity type template, if there is one (e.g., node.html.twig).


As for template_preprocess_node() implemented by a theme, that is not different from hook_preprocess_node(): It can either add new variables, or change the content of existing variables. In both the cases, the $variables parameter is passed by reference, so any change to the array is passed back to the function/method invoking those hooks.

The other alternative would be implementing an input filter in a module, and add the input filter to the input format used from the content type. This would have the pro of stopping the replacements simply by removing the input filter from the input format, and it would also have the pro of changing in the site configuration which content types would have the text replaced, and document to users the changes done to nodes.
The con is that it cannot be used in fields that don't use input formats, but it works for the body field of nodes. If you need to alter the text that appears in a link field, for example, an input filter would not help (since the link field would ask for a URL and the text to show as link title).