This question is an extension to the question Is it OK to repost deleted answers as new answers? The conclusion was that it is not OK for an "answer that has basically the same content." What about if I want to start with a "clean slate" after correcting a mistake? I'm not trying to abuse the system.
Original post
I posted the following answer to this question:
This is gives all the indexes of a word in a text string but is a little more readable than @polygenelubricants' answer (and thus better for maintainability).
String text = "0123hello9012hello8901hello7890"; String word = "hello"; int index = word.indexOf(word); while (index >= 0) { // indexOf returns -1 if no match found System.out.println(index); index = word.indexOf(word, index + 1); }
(Source)
Edit to original post
The original post was correctly downvoted because I never referenced text
. The person who downvoted also left a comment (which I appreciated). I edited my answer with the corrected code:
This is gives all the indexes of a word in a text string but is a little more readable than @polygenelubricants' answer (and thus better for maintainability--see @StephenC's comment).
String text = "0123hello9012hello8901hello7890"; String word = "hello"; int index = text.indexOf(word); while (index >= 0) { // indexOf returns -1 if no match found System.out.println(index); index = text.indexOf(word, index + 1); }
(Source)
Delete and Repost
However, editing the original post of course did not reverse the downvote. There were also two comments (the comment about the original mistake and my reply) that would not be useful to other people. I wanted to start with a "clean slate" so I deleted the original post and added this one:
Because of @StephenC's comment about code maintainability and my own difficulty in understanding @polygenelubricants' answer, I wanted to find another way to get all the indexes of a word in a text string. The following code (which is modified from this answer) does so:
String text = "0123hello9012hello8901hello7890"; String word = "hello"; int index = text.indexOf(word); while (index >= 0) { // indexOf returns -1 if no match found System.out.println(index); index = text.indexOf(word, index + 1); }
This new post is essentially the same as the edited version of my original post. However, it doesn't contain the original mistake, the downvote, and the old comments.