by TrashHalo
String manipulation functions

BYOND has several libraries for handling text functions, with some different abilities in each. If you do a lot of text handling in your games, you may want to check out bstrlib by TrashHalo.

The library basically has two parts: a Tokenizer datum you can use to break up text into smaller chunks, and a set of procs for general text manipulation.

The Tokenizer is very easy to use. If you have a bunch of values separated by commas or pipes or what have you, it's easy to go through the text a token at a time.

var/Tokenizer/T = new(mytext, "/")
world <<

If you try that snippet with mytext set to "apple/orange/banana", you'll get this output:

apple orange banana

This can be a clever way of storing info in your savefiles.

The rest of the library is made up of procs you can use for all kinds of text:

  • startsWith(haystack, needle) and endsWith(haystack, needle) will tell you if your text begins with a specific pattern.
  • replace(text, find, replace) gives you a copy of your text with the search pattern replaced everywhere by something else. This is case-sensitive only.
  • trim(text) will give back a copy of your text with whitespace removed from the beginning and end.
  • chomp(text) removes a \n from the end of a string.
  • chop(text) removes the last character from the end of a string.
  • charAt(text, n) gives you the character at index n.
  • toCharArray(text) creates a list, and fills it with one character at a time from your text string.
  • join(token, text[]) joins a list of text strings by the token you want, so you can join a list of names with a comma for instance with join(",", mylist).
  • split(token, text) does the opposite of join(), taking a piece of text and returning a list of everything that was separated by the token. This works just like the Tokenizer datum but it gives you the results all at once.
  • wordWrap(text, numChars) splits a long bunch of text into separate lines, where numChars is the maximum number of characters you want per line. This is really useful if you want to create a game interface like the old NES style.
  • count(haystack, needle) counts the number of times a pattern appears in your text.
  • reverse(text) will reverse any block of text, although the author has warned that this can be slow if the text is very large.

This library is useful for a lot of text-handling needs and does some things that other libraries don't. It's also pretty easy to see how it does what it does, so if ultimately you don't want to use this library but want something like it, it can be very useful for reference.

There are a couple of drawbacks, but not fatal ones. All of the routines are case-sensitive, so if you need case-insensitive matching then this might not be the best library for you. When using it as a library, some of the proc names are common enough words (like "replace" and "count") that they could conflict with existing code, so that's something you'll want to take into consideration.