Do any of you know of an easy/clean way to find a substring within a string while ignoring some specified characters to find it. I think an example would explain things better:
- string: "Hello, -this- is a string"
- substring to find: "Hello this"
- chars to ignore: "," and "-"
- found the substring, result: "Hello, -this"
Using Regex is not a requirement for me, but I added the tag because it feels related.
Update:
To make the requirement clearer: I need the resulting substring with the ignored chars, not just an indication that the given substring exists.
Update 2: Some of you are reading too much into the example, sorry, i'll give another scenario that should work:
- string: "?A&3/3/C)412&"
- substring to find: "A41"
- chars to ignore: "&", "/", "3", "C", ")"
- found the substring, result: "A&3/3/C)41"
And as a bonus (not required per se), it will be great if it's also not safe to assume that the substring to find will not have the ignored chars on it, e.g.: given the last example we should be able to do:
- substring to find: "A3C412&"
- chars to ignore: "&", "/", "3", "C", ")"
- found the substring, result: "A&3/3/C)412&"
Sorry if I wasn't clear before, or still I'm not :).
Update 3:
Thanks to everyone who helped!, this is the implementation I'm working with for now:
An here are some tests:
I'm using some custom extension methods I'm not including but I believe they should be self-explainatory (I will add them if you like) I've taken a lot of your ideas for the implementation and the tests but I'm giving the answer to @PierrOz because he was one of the firsts, and pointed me in the right direction. Feel free to keep giving suggestions as alternative solutions or comments on the current state of the impl. if you like.