Skip to content

Iterators Sub-Module Documentation⚓︎


Overview⚓︎

The StringIterators Sub-Library provides a set of functions for iterating through various components of a string.

These functions return an iterator function that can be used to iterate through each character, word, sentence, or line in a string.

Functions⚓︎

ICharacters⚓︎

Purpose⚓︎

Returns an iterator function that returns each character in the string Str one at a time.

Syntax⚓︎

ICharacters(Str: string): ((string, number) -> (number, string), string, number)

Parameters⚓︎

  • Str: string
    The input string to iterate over its characters.

Returns⚓︎

  • function
    An iterator function that returns the next character in the string on each call.
  • number
    The index of the character in the input string.
  • string
    The character.

Examples⚓︎

1
2
3
4
local Str = "Hello!"
for Index, Character in String.ICharacters(Str) do
    print(Index .. (":"), Character)
end
1
2
3
4
5
6
1: H
2: e
3: l
4: l
5: o
6: !

IWords⚓︎

Purpose⚓︎

This function returns an iterator function that returns each word in the string Str one at a time.
Words are defined as sequences of non-whitespace characters separated by one or more whitespace characters.

Syntax⚓︎

IWords(Str: string): ((string, number): ((string, number) -> (number, number, string), string, number)

Parameters⚓︎

  • Str: string
    The input string to iterate over its words.

Returns⚓︎

  • function
    An iterator function that returns the next word in the string on each call.
  • number
    The start index of the word in the input string.
  • number
    The end index of the word in the input string.
  • string
    The word itself.

Examples⚓︎

1
2
3
4
local Str = "This is a test string."
for Start, End, Word in String.IWords(Str) do
    print(string.format("[%d-%d] - \"%s\"", Start, End, Word))
end
1
2
3
4
5
[1-4] - "This"
[6-7] - "is"
[9-9] - "a"
[11-14] - "test"
[16-21] - "string"

ISentences⚓︎

Purpose⚓︎

Returns an iterator function that returns each sentence in the input string one at a time.
Sentences are defined as sequences of characters separated by one or more sentence-ending punctuation characters (e.g. ., ?, !).

Syntax⚓︎

ISentences(Str: string): ((string) -> (number, number, string, string), string, number)

Parameters⚓︎

  • Str: string
    The input string to iterate over its words.

Returns⚓︎

  • function
    An iterator function that returns the next sentence in the string on each call.
  • number
    The start index of the sentence in the input string.
  • number
    The end index of the sentence in the input string.
  • string
    The sentence.
  • string
    The ending punctuation of the sentence (e.g. ., ?, !).

Examples⚓︎

1
2
3
4
5
6
7
8
local Sentences = {}
local Str = "This is the first sentence. This is the second sentence! This is the third sentence?"

for Start, End, Sentence, Ending in String.ISentences(Str) do
    table.insert(Sentences, {Start = Start, End = End, Sentence = Sentence, Ending = Ending})
end

print(table.unpack(Sentences))
1
2
3
{ Start = 1, End = 26, Sentence = "This is the first sentence", Ending = "." },
{ Start = 29, End = 55, Sentence = "This is the second sentence", Ending = "!" },
{ Start = 58, End = 83, Sentence = "This is the third sentence", Ending = "?" }