0 votes
Anyone have a way to do a string comparison for substrings of any length?

e.g. I want to search a string containing an email to see if it has "@gmail" in it, then again to see if it's got @hotmail etc and iterate through to find whether they're using common freemail providers.
by (5.8k points)

1 Answer

0 votes

A general solution for determining whether string contains any substring from a list using .find
https://www.guidedtrack.com/programs/10828/edit

>> substringsToFind = ["a", "b", "c"]
>> string = "ghjk"

>> i = 1
*label: search
*while: i < substringsToFind.size
	>> found = string.find(substringsToFind[i])
	*if: not found
		>> i = i + 1	
		*goto: search
	*goto: success

none of [{substringsToFind}] is in {string}

*label: success
*if: found
	"{substringsToFind[i]}" is in "{string}" at position "{found}"

In addition to .find, there is also .count, .combine, .shuffle, .sort, .mean which all are very useful for collections (arrays)

A more specific solution for the email problem, maybe useful if you want to do more low-level work with strings.

https://www.guidedtrack.com/programs/10819

Checking if mail provider in a valid email string belongs to list of common freemail providers is easier than searching random substring in random string because we can expect provider string to be in between '@' and '.'

Then we can check *if: provider in [common providers]

by (5.8k points)
Welcome to Guidedtrack Q&A, where you can ask questions and receive answers from other members of the community.
134 questions
144 answers
55 comments
40 users