Metamorphing Machine I rather be this walking metamorphosis
than having that old formed opinion about everything!

Searching inside strings and its leaking abstractions

From time to time, I write code that I have no use for.
That regex to validate dates is one of them.
Usually, I do that just to prove something, to understand some concept, or to learn something new.

Also from time to time, I port a personal project from one programing language to another.
My most recent comics collection program's incarnation, for example, is in Javascript.

The other day I was thinking about how search string functions differ from one programing language to another:
Madness.

So it bugs me when I port my code and need to mess with these indexes. Actually, I think the whole index thing is a leaked abstraction.
Having that in mind, I tried to come up with something that would not require indexes, nor would it return one.
I did it in VB6, VB.NET, C#, and Javascript.
Here it goes an usage example in Javascript:

let s = "The brown fox jumped over the lazy dog";
let cut = StringFinder.Find(s, "FOX", /*caseInsensitive:*/ true);

if (cut.Found) {
console.log("->" + cut.Before() + "<-"); //Returns ->The brown <-
console.log("->" + cut.After() + "<-"); //Returns -> jumped over the lazy dog<-
console.log("->" + cut.TakeBackward(/*count:*/ 5, /*skip:*/ 1) + "<-"); //Returns ->brown<-
console.log("->" + cut.TakeForward(/*count:*/ 6, /*skip:*/ 1) + "<-"); //Returns ->jumped<-
console.log("->" + cut.What + "<-"); //Returns ->fox<-
} else {
console.log("Not found!");
}

cut = StringFinder.Between(s, "brown", "jumped");
console.log("->" + cut.Before() + "<-"); //Returns ->The <-
console.log("->" + cut.After() + "<-"); //Returns -> over the lazy dog<-
console.log("->" + cut.What + "<-"); //Returns ->brown fox jumped<-

As I said, right now I have no use to it, but maybe someone would find it useful.
Regarding the code, the gist today seems to require to slap it in Github for the world to see.
I have my reasons to not use Github, so I'll go old school and let it available as a zipped set of files to download instead: Strings.js, Program.bas.txt, Program.cs.txt, Program.vb.txt, StringFinder.bas.txt, StringIndex.cls.txt (you will need to remove the .txt extensions)

Andrej Biasic
2020-03-25
Update

Now you can download these files in a .ZIP file. Just click the button:

Andrej Biasic
2021-09-22