A client reported a problem where our software was "hanging". I eventually managed to find the piece of code responsible:
While strShortenedText.IndexOf(" ") > -1 'if there are two contiguous spaces
'anywhere in this string ...
strShortenedText = strShortenedText.Replace(" ", " ") '... replace each
'occurrence with a
'single string ...
End While '... and do this in a loop in order to handle situations with 3 or more
'contiguous spaces
I'm afraid I won't be able to include the actual string here as it contains data covered by GDPR legislation, but I am just looking for an explanation as to how this could possibly have gone into an infinite loop?
What I find is that:
strShortenedText.IndexOf(" ")
returns a value greater than -1, so somewhere in this string there are two contiguous spaces (I made sure that these spaces are both 0x20 i.e. char(32))- So if strShortenedText contains
" "
(2 contiguous spaces) anywhere, and I replace" "
(2 contiguous spaces) with" "
(a single space) then, surely (??????),strShortenedText
after the.Replace
will be shorter than it was before the replace?
Instead, I find that the REPLACE leaves the string unchanged, and no matter how often the code loops through the statement, the variable's length never changes.
How might this be possible? And what could I possibly do to handle this scenario?