|
Functions to deal with whitespace-separated lists of values in strings. More...
Functions | |
string | firstWord (string text) |
Return the first word in text. | |
string | getField (string text, int index) |
Extract the field at the given index in the newline and/or tab separated list in text. | |
int | getFieldCount (string text) |
Return the number of newline and/or tab separated fields in text. | |
string | getFields (string text, int startIndex, int endIndex=-1) |
Extract a range of fields from the given startIndex onwards thru endIndex. | |
string | getRecord (string text, int index) |
Extract the record at the given index in the newline-separated list in text. | |
int | getRecordCount (string text) |
Return the number of newline-separated records in text. | |
string | getRecords (string text, int startIndex, int endIndex=-1) |
Extract a range of records from the given startIndex onwards thru endIndex. | |
string | getWord (string text, int index) |
Extract the word at the given index in the whitespace-separated list in text. | |
int | getWordCount (string text) |
Return the number of whitespace-separated words in text. | |
string | getWords (string text, int startIndex, int endIndex=-1) |
Extract a range of words from the given startIndex onwards thru endIndex. | |
string | removeField (string text, int index) |
Remove the field in text at the given index. | |
string | removeRecord (string text, int index) |
Remove the record in text at the given index. | |
string | removeWord (string text, int index) |
Remove the word in text at the given index. | |
string | restWords (string text) |
Return all but the first word in text. | |
string | setField (string text, int index, string replacement) |
Replace the field in text at the given index with replacement. | |
string | setRecord (string text, int index, string replacement) |
Replace the record in text at the given index with replacement. | |
string | setWord (string text, int index, string replacement) |
Replace the word in text at the given index with replacement. |
Functions to deal with whitespace-separated lists of values in strings.
TorqueScript extensively uses strings to represent lists of values. The functions in this group simplify working with these lists and allow to easily extract individual values from their strings.
The list strings are segregated into three groups according to the delimiters used to separate invididual values in the strings:
Aside from the functions here, another useful means to work with strings of words is TorqueScript's foreach$
statement.
string firstWord | ( | string | text | ) |
string getField | ( | string | text, | |
int | index | |||
) |
Extract the field at the given index in the newline and/or tab separated list in text.
Fields in text must be separated by newlines and/or tabs.
text | A list of fields separated by newlines and/or tabs. | |
index | The zero-based index of the field to extract. |
getField( "a b" TAB "c d" TAB "e f", 1 ) // Returns "c d"
int getFieldCount | ( | string | text | ) |
Return the number of newline and/or tab separated fields in text.
text | A list of fields separated by newlines and/or tabs. |
getFieldCount( "a b" TAB "c d" TAB "e f" ) // Returns 3
string getFields | ( | string | text, | |
int | startIndex, | |||
int | endIndex = -1 | |||
) |
Extract a range of fields from the given startIndex onwards thru endIndex.
Fields in text must be separated by newlines and/or tabs.
text | A list of fields separated by newlines and/or tabs. | |
startIndex | The zero-based index of the first field to extract from text. | |
endIndex | The zero-based index of the last field to extract from text. If this is -1, all fields beginning with startIndex are extracted from text. |
getFields( "a b" TAB "c d" TAB "e f", 1 ) // Returns "c d" TAB "e f"
string getRecord | ( | string | text, | |
int | index | |||
) |
Extract the record at the given index in the newline-separated list in text.
Records in text must be separated by newlines.
text | A list of records separated by newlines. | |
index | The zero-based index of the record to extract. |
getRecord( "a b" NL "c d" NL "e f", 1 ) // Returns "c d"
int getRecordCount | ( | string | text | ) |
Return the number of newline-separated records in text.
text | A list of records separated by newlines. |
getRecordCount( "a b" NL "c d" NL "e f" ) // Returns 3
string getRecords | ( | string | text, | |
int | startIndex, | |||
int | endIndex = -1 | |||
) |
Extract a range of records from the given startIndex onwards thru endIndex.
Records in text must be separated by newlines.
text | A list of records separated by newlines. | |
startIndex | The zero-based index of the first record to extract from text. | |
endIndex | The zero-based index of the last record to extract from text. If this is -1, all records beginning with startIndex are extracted from text. |
getRecords( "a b" NL "c d" NL "e f", 1 ) // Returns "c d" NL "e f"
string getWord | ( | string | text, | |
int | index | |||
) |
Extract the word at the given index in the whitespace-separated list in text.
Words in text must be separated by newlines, spaces, and/or tabs.
text | A whitespace-separated list of words. | |
index | The zero-based index of the word to extract. |
getWord( "a b c", 1 ) // Returns "b"
int getWordCount | ( | string | text | ) |
Return the number of whitespace-separated words in text.
Words in text must be separated by newlines, spaces, and/or tabs.
text | A whitespace-separated list of words. |
getWordCount( "a b c d e" ) // Returns 5
string getWords | ( | string | text, | |
int | startIndex, | |||
int | endIndex = -1 | |||
) |
Extract a range of words from the given startIndex onwards thru endIndex.
Words in text must be separated by newlines, spaces, and/or tabs.
text | A whitespace-separated list of words. | |
startIndex | The zero-based index of the first word to extract from text. | |
endIndex | The zero-based index of the last word to extract from text. If this is -1, all words beginning with startIndex are extracted from text. |
getWords( "a b c d", 1, 2, ) // Returns "b c"
string removeField | ( | string | text, | |
int | index | |||
) |
Remove the field in text at the given index.
Fields in text must be separated by newlines and/or tabs.
text | A list of fields separated by newlines and/or tabs. | |
index | The zero-based index of the field in text. |
removeField( "a b" TAB "c d" TAB "e f", 1 ) // Returns "a b" TAB "e f"
string removeRecord | ( | string | text, | |
int | index | |||
) |
Remove the record in text at the given index.
Records in text must be separated by newlines.
text | A list of records separated by newlines. | |
index | The zero-based index of the record in text. |
removeRecord( "a b" NL "c d" NL "e f", 1 ) // Returns "a b" NL "e f"
string removeWord | ( | string | text, | |
int | index | |||
) |
Remove the word in text at the given index.
Words in text must be separated by newlines, spaces, and/or tabs.
text | A whitespace-separated list of words. | |
index | The zero-based index of the word in text. |
removeWord( "a b c d", 2 ) // Returns "a b d"
string restWords | ( | string | text | ) |
string setField | ( | string | text, | |
int | index, | |||
string | replacement | |||
) |
Replace the field in text at the given index with replacement.
Fields in text must be separated by newlines and/or tabs.
text | A list of fields separated by newlines and/or tabs. | |
index | The zero-based index of the field to replace. | |
replacement | The string with which to replace the field. |
setField( "a b" TAB "c d" TAB "e f", 1, "g h" ) // Returns "a b" TAB "g h" TAB "e f"
string setRecord | ( | string | text, | |
int | index, | |||
string | replacement | |||
) |
Replace the record in text at the given index with replacement.
Records in text must be separated by newlines.
text | A list of records separated by newlines. | |
index | The zero-based index of the record to replace. | |
replacement | The string with which to replace the record. |
setRecord( "a b" NL "c d" NL "e f", 1, "g h" ) // Returns "a b" NL "g h" NL "e f"
string setWord | ( | string | text, | |
int | index, | |||
string | replacement | |||
) |
Replace the word in text at the given index with replacement.
Words in text must be separated by newlines, spaces, and/or tabs.
text | A whitespace-separated list of words. | |
index | The zero-based index of the word to replace. | |
replacement | The string with which to replace the word. |
setWord( "a b c d", 2, "f" ) // Returns "a b f d"