Mastering Strings in Small Basic with the Extended Text LibrarySmall Basic is a gentle introduction to programming that lets beginners create graphics, games, and utilities quickly. While its built-in Text and String operations are simple and useful for many tasks, more advanced projects often require richer string-processing capabilities. The Extended Text Library (ETL) fills that gap by providing a set of powerful, easy-to-use string utilities tailored to Small Basic’s syntax and learning style. This article walks through why ETL is useful, what it offers, and how to apply it in practical examples.
Why extend Small Basic’s string handling?
Small Basic intentionally keeps its standard library minimal to avoid overwhelming new learners. However, this minimalism also means common tasks—like advanced pattern matching, multi-line trimming, or robust CSV parsing—require verbose code or awkward workarounds. ETL:
- Adds functionality missing from the core language while preserving Small Basic’s simple API style.
- Helps learners perform real-world tasks (data cleaning, text parsing, templating) earlier in their learning path.
- Keeps code readable and maintainable by encapsulating common string operations into well-named functions.
What the Extended Text Library provides
ETL is a collection of focused routines that complement Small Basic’s built-in Text and String operations. Typical features included in an ETL distribution:
- Substring utilities (safe slicing, negative indices)
- Trimming and padding (trim both ends, trim to length, pad left/right)
- Case conversion beyond ToUpper/ToLower (title case, sentence case)
- Search and replace with options (first/all, case-sensitive/insensitive)
- Splitting and joining with advanced rules (limit count, preserve empties)
- CSV parsing and serialization (handles quotes, escaped delimiters)
- Pattern matching helpers (simple wildcards, basic regex-like behaviors)
- Tokenization utilities (split by multiple separators, keep delimiters)
- Unicode-aware helpers (normalize, length in characters vs bytes)
- Formatting and templating (placeholder replacement, conditional inserts)
Not every ETL package contains every item above; authors typically pick a pragmatic subset that best matches their students’ needs.
Basic usage patterns
ETL is written to feel familiar to Small Basic programmers. Functions are typically exposed as methods on a single namespace (for example, ExtendedText), with descriptive names and straightforward parameters.
Example — safe slice:
text = "Hello, world!" result = ExtendedText.SafeSlice(text, 7) ' "world!" result2 = ExtendedText.SafeSlice(text, 7, 11) ' "world"
Example — CSV parsing:
line = "Alice, "Bob, Jr.", 42" fields = ExtendedText.ParseCsv(line) ' fields[1] = "Alice" ' fields[2] = "Bob, Jr." ' fields[3] = "42"
Example — replace all case-insensitive:
src = "Foo bar foo" out = ExtendedText.Replace(src, "foo", "baz", "IgnoreCase", "All") ' out = "baz bar baz"
Practical examples and recipes
Below are several concrete examples showing how ETL simplifies common tasks.
-
Cleaning user input (trim, normalize spaces, title case)
raw = " alice in wonderland " clean = ExtendedText.Trim(raw) clean = ExtendedText.NormalizeSpaces(clean) clean = ExtendedText.ToTitleCase(clean) ' clean = "Alice In Wonderland"
-
Extracting tokens with multiple delimiters
line = "value1|value2, value3;value4" tokens = ExtendedText.Tokenize(line, "|,;") ' tokens = ["value1", "value2", "value3", "value4"]
-
Templating for simple text generation
template = "Dear {name}, Your score is {score}." data = Array.Create() data["name"] = "Sam" data["score"] = "88" message = ExtendedText.FillTemplate(template, data) ' message = "Dear Sam, Your score is 88."
-
Robust CSV reading and writing “`smallbasic csvLine = “1,2,“three, three”,4” row = ExtendedText.ParseCsv(csvLine) ‘ row[3] = “three, three”
newLine = ExtendedText.JoinCsv(row) ’ newLine == csvLine (or equivalent normalized form)
--- ### Implementation notes and pedagogy If you’re teaching or maintaining ETL for Small Basic students, consider these design principles: - Keep the API small and descriptive. Beginners learn names and intent faster than many parameters. - Provide safe defaults (e.g., Replace defaults to all occurrences, case-sensitive unless asked otherwise). - Avoid exposing complex regex unless introducing patterns explicitly—prefer simpler wildcard or token helpers. - Include examples and unit-like tests; short demo programs help learners grasp behavior quickly. - Document edge cases: empty strings, nulls (if relevant in the host environment), extremely long inputs. --- ### Performance and limitations Small Basic is not designed for heavy text-processing workloads. Use ETL for improving clarity and convenience in small-to-moderate tasks (config files, CSVs, simple templating). For very large files or CPU-heavy transformations: - Consider pre-processing data in a more performant environment (Python, Node.js) before using Small Basic. - Cache repeated computations (e.g., compiled patterns) where the ETL supports it. - Be mindful of memory when tokenizing huge strings; prefer streaming approaches when possible. --- ### Extending ETL yourself Creating or expanding an ETL module is a great learning project. Start small: - Implement SafeSlice, Trim, NormalizeSpaces, and a simple ReplaceAll. - Add Tokenize supporting multiple delimiters. - Publish examples and let students contribute new helpers. Example stub for ReplaceAll: ```smallbasic ' Pseudocode outline — adapt to Small Basic syntax/environment Function ReplaceAll(text, oldValue, newValue) While Text.Contains(text, oldValue) text = Text.Replace(text, oldValue, newValue) EndWhile Return text EndFunction
Example project: CSV-based contact manager
A practical project combining ETL features:
- Read a CSV of contacts (Name, Email, Notes).
- Use ParseCsv to load lines, NormalizeSpaces and ToTitleCase to clean names.
- Provide search by substring (case-insensitive) using ETL’s search helpers.
- Export filtered results using JoinCsv.
This project teaches file I/O, string handling, arrays, and simple UI in Small Basic.
Conclusion
The Extended Text Library transforms Small Basic from a toy-string environment into a practical toolkit for many real-world text tasks while keeping the language approachable. By wrapping common patterns into clear functions, ETL helps learners focus on problem-solving and application logic instead of low-level string plumbing. Whether you’re teaching, learning, or building small utilities, ETL is an excellent bridge from beginner-friendly syntax to useful, production-minded text processing.
Leave a Reply