Now Reading
What’s the index of an empty string in an empty string?

What’s the index of an empty string in an empty string?

2023-12-13 11:37:46

This appears like a query a programmer may ask after one medicinal cigarette too many. The pc science equal of “what’s the sounds of 1 hand clapping?”. However it’s a query I’ve to resolve the reply to.

I’m including indexOf() and lastIndexOf() operations to the Calculate rework of my information wrangling (ETL) software program (Easy Data Transform). This can permit customers to seek out the offset of 1 string inside one other, counting from the beginning or the tip of the string. Straightforward Knowledge Remodel is written in C++ and makes use of the Qt QString class for strings. There are indexOf() and lastIndexOf() strategies for QString, so I believed this could be a simple job to wrap that performance. Perhaps quarter-hour to program it, write a take a look at case and doc it.

Clearly it wasn’t that straightforward, in any other case I couldn’t be penning this weblog submit.

Initially, what’s the index of “a” in “abc”? 0, clearly. QString( “abc” ).indexOf( “a” ) returns 0. Duh. Nicely solely in case you are a (non-Fortran) programmer. Ask a non-programmer (corresponding to my spouse) and they’re going to say: 1, clearly. It’s the first character. Duh. Excel FIND( “a”, “abc” ) returns 1.

Okay, most of my prospects, aren’t programmers. I can use 1 primarily based indexing.

However then issues get extra difficult.

What’s the index of an empty string in “abc”? 1 perhaps, utilizing 1-based indexing or perhaps empty shouldn’t be a sound worth to go.

What’s the index of an empty string in an empty string? Hmm. I suppose the empty string does comprise an empty string, however at what index? 1 perhaps, utilizing 1-based indexing, besides there isn’t a primary place within the string. Once more, perhaps empty shouldn’t be a sound worth to go.

I regarded on the Qt C++ QString, Javascript string and Excel FIND() operate for solutions. However they every give completely different solutions and a few of them aren’t even internally constant. This can be a easy comparability of the primary index or final index of textual content v1 in textual content v2 in every (Excel doesn’t have an equal of lastIndexOf() that I’m conscious of):

Altering these to make the all of the legitimate outcomes 1-based and setting invalid outcomes to -1, for straightforward comparability:

See Also

So:

  • Javascript disagrees with C++ QString and Excel on whether or not the primary index of an empty string in an empty string is legitimate.
  • Javascript disagrees with C++ QString on whether or not the final index of an empty string in a non-empty string is the index of the final character or 1 after the final character.
  • C++ QString thinks the primary index of an empty string in an empty string is the primary character, however the final index of an empty string in an empty string is invalid.

It appears surprisingly troublesome to provide you with one thing intuitive and constant! I believe I’m in all probability going to return an error message if both or each values are empty. This appears to me to be the one unambiguous and constant method.

I may return a 0 for a non-match or when one or each values are empty, however I believe you will need to return completely different leads to these 2 completely different instances. Additionally, not discovered and invalid really feel qualitatively completely different to a calculated index to me, so shouldn’t be simply one other quantity. What do you assume?



Source Link

What's Your Reaction?
Excited
0
Happy
0
In Love
0
Not Sure
0
Silly
0
View Comments (0)

Leave a Reply

Your email address will not be published.

2022 Blinking Robots.
WordPress by Doejo

Scroll To Top