by Deadron
Various text-related functions you might find useful, including reading files into lists and replacing words in strings.
A BYONDscape Classic! I won't try to describe it because the comments in the source code do a fine job already -- see below. -- Gughunter


dd_file2list(file_path, separator = "\n")
Splits the text from the specified file into a list.
file_path is the path to the file.
separator is an optional delimiter between items in the file;
it defaults to "\n", which makes each line of the file an item in the list.


// Read in the list of possible NPC names.
var/list/names = dd_file2list("NPCs.txt")

dd_replacetext(text, search_string, replacement_string)
Returns a new string replacing all occurrences of search_string in text
with replacement_string. This is not case-sensitive.


verb/say(msg as text)
// Don't let the player fake people out using line breaks when they say things.
// Replace any instances of
or /n with a space.
var/search_string = "
var/replacement = " "
var/sanitized_text = dd_replacetext(msg, search_string, replacement)

search_string = "/n"
sanitized_text = dd_replacetext(sanitized_text, search_string, replacement)

view(src) << sanitized_text

dd_replaceText(text, search_string, replacement_string)
The case-sensitive version of dd_replacetext().

dd_hasprefix(text, prefix)
Returns 1 if the text has the specified prefix, 0 otherwise. This version is not case sensitive.


// Does the player's name have GM as the prefix?
if (dd_hasprefix(name, "GM"))
// Give them GM abilities.

dd_hasPrefix(text, prefix)
The case-sensitive version of dd_hasprefix.

dd_hassuffix(text, suffix)
Returns 1 if the text has the specified prefix, 0 otherwise.
This version is not case sensitive.

dd_hasSuffix(text, suffix)
Returns 1 if the text has the specified prefix, 0 otherwise.
This version is case sensitive.

dd_text2list(text, separator)
Split the text into a list, where separator is the delimiter between items.
Returns the list. This is not case-sensitive.

If the myText string is "a = b = c", and you call dd_text2list(myText, " = "), you get a list back with these items:


// Get a list containing the names in this string.
var/mytext = "George; Bernard; Shaw"
var/separator = "; "
var/list/names = dd_text2list(mytext, separator)

dd_text2List(text, separator)
The case-sensitive version of dd_text2list().

dd_list2text(list/the_list, separator)
Create a string by combining each element of the list,
inserting the separator between each item.


// Turn this list of names into one string separated by semi-colons.
var/list/names = list("George", "Bernard", "Shaw")
var/separator = "; "
var/mytext = dd_list2text(names, separator)

dd_centertext(message, length)
Returns a new text string, centered based on the length.

If the string is not as long as the length, spaces are added
on both sides of the message.

If the string is longer than the specified length, the message
is truncated to fit to the length.

This function is useful when laying out text on the map, where you
might want to center a title, for example.

dd_limittext(message, length)
If the message is longer than length, truncates the message to fit
length. This is useful for text on the map, where you might want
to display a player name, for example, but have to make sure it's
not too long to fit.
This library is incredibly valuable!
I suggest that anyone who hasn't, should download it and look at its source.