AngelScript Strings
Original document: https://www.angelcode.com/angelscript/sdk/docs/manual/doc_script_stdlib_string.html
Basic String Methods
- uint length // Use without "()"
Returns the length of the string.
- void resize(uint)
Sets the length of the string.
- bool isEmpty() const
Returns true if the string is empty (length is zero).
- string substr(uint start = 0, int count = -1) const
Returns a string with the content starting at start and the number of bytes given by count. The default arguments will return the whole string as the new string.
- void insert(uint pos, const string &in other)
Inserts another string other at position pos in the original string.
- void erase(uint pos, int count = -1)
Erases a range of characters from the string, starting at position pos and counting count characters.
- pos = s.findFirst("letimix"); // find substring
- log("pos: "+pos); // pos: 7
- pos = s.findFirst("e");
- log("pos: "+pos); // pos: 3
- pos = s.findFirst("e", pos+1); // find next "e"
- log("pos: "+pos); // pos: 8
- pos = s.findLast("e");
- log("pos: "+pos); // pos: 20
- pos = s.findLast("e", pos-1); // find previous "e"
- log("pos: "+pos); // pos: 8
- pos = s.findLast("abc"); // not in the string
- log("pos: "+pos); // pos: -1
- pos = s.findFirstOf("slD"); // find any of chars
- log("pos: "+pos); // pos: 2 ('s' in Users)
- pos = s.findFirstOf("slD", pos+1);
- log("pos: "+pos); // pos: 5 (next 's' in Users)
- pos = s.findLastOf("slD");
- log("pos: "+pos); // pos: 23 ('s' in Documents)
- pos = s.findLastOf("slD", pos-1);
- log("pos: "+pos); // pos: 15 ('D' in Documents)
- pos = s.findFirstNotOf("/");
- log("pos: "+pos); // pos: 1 ('U' in Users)
- pos = s.findFirstNotOf("/", pos+1);
- log("pos: "+pos); // pos: 2 ('s' in Users)
- pos = s.findLastNotOf("/");
- log("pos: "+pos); // pos: 23 ('s' in Documents)
- pos = s.findLastNotOf("/", pos-1); // find any of chars
- log("pos: "+pos); // pos: 22 ('t' in Documents)
Search Methods
- int findFirst(const string &in str, uint start = 0) const
Find the first occurrence of the value str in the string, starting at start. If no occurrence is found a negative value will be returned.
- int findLast(const string &in str, int start = -1) const
Find the last occurrence of the value str in the string. If start is informed the search will begin at that position, i.e. any potential occurrence after that position will not be searched. If no occurrence is found a negative value will be returned.
- int findFirstOf(const string &in chars, int start = 0) const
Finds the first character in the string that matches one of the characters in chars, starting at start. If no occurrence is found a negative value will be returned.
- int findFirstNotOf(const string &in chars, int start = 0) const
Finds the first character that doesn't match any of those in chars.
- int findLastOf(const string &in chars, int start = -1) const
Same as findFirstOf but starts the search from the end of the string.
- int findLastNotOf(const string &in chars, int start = -1) const
Same as findFirstNotOf but starts the search from the end of the string.
- string s = "$_USER_DOCUMENTS_PATH_$";
- log(s); // /Users/letimix/Documents
- int pos;
- pos = s.findFirst("letimix");
- log("pos: "+pos); // pos: 7
- pos = s.findFirst("e");
- log("pos: "+pos); // pos: 3
- pos = s.findFirst("e", pos+1); // find next "e"
- log("pos: "+pos); // pos: 8
- pos = s.findLast("e");
- log("pos: "+pos); // pos: 20
- pos = s.findLast("e", pos-1); // find previous "e"
- log("pos: "+pos); // pos: 8
- pos = s.findLast("abc"); // not in the string
- log("pos: "+pos); // pos: -1
- pos = s.findFirstOf("slD"); // find any of chars
- log("pos: "+pos); // pos: 2 ('s' in Users)
- pos = s.findFirstOf("slD", pos+1);
- log("pos: "+pos); // pos: 5 (next 's' in Users)
- pos = s.findLastOf("slD");
- log("pos: "+pos); // pos: 23 ('s' in Documents)
- pos = s.findLastOf("slD", pos-1);
- log("pos: "+pos); // pos: 15 ('D' in Documents)
- pos = s.findFirstOf("slD"); // find any of chars
- log("pos: "+pos); // pos: 2 ('s' in Users)
- pos = s.findFirstNotOf("/");
- log("pos: "+pos); // pos: 1 ('U' in Users)
- pos = s.findFirstNotOf("/", pos+1);
- log("pos: "+pos); // pos: 2 ('s' in Users)
- pos = s.findLastNotOf("/");
- log("pos: "+pos); // pos: 23 ('s' in Documents)
- pos = s.findLastNotOf("/", pos-1); // find any of chars
- log("pos: "+pos); // pos: 22 ('t' in Documents)
Split and join
- array<string>@ split(const string &in delimiter) const
Splits the string in smaller strings where the delimiter is found.
- string join(const array<string> &in arr, const string &in delimiter)
Concatenates the strings in the array into a large string, separated by the delimiter.
- string s = "$_USER_DOCUMENTS_PATH_$";
- log(s); // /Users/letimix/Documents
- array<string> ar = s.split("/");
- log("array size: "+ar.length); // 4 (first empty before first /)
- for(uint i=0;i<ar.length;i++) {
- log(ar[i]); // outputs [empty], Users, letimix, Documents
- }
- ar.insertLast("Blue Cat Audio"); // add pieces to array
- ar.insertLast("Blue Cat's Plug'n Script");
- s = join(ar, "/"); // join array back to string
- log(s); // /Users/letimix/Documents/Blue Cat Audio/Blue Cat's Plug'n Script
Parsing Functions
- int64 parseInt(const string &in str, uint base = 10, uint &out byteCount = 0)
Parses the string for an integer value. The base can be 10 or 16 to support decimal numbers or hexadecimal numbers. If byteCount is provided it will be set to the number of bytes that were considered as part of the integer value.
- uint64 parseUInt(const string &in str, uint base = 10, uint &out byteCount = 0)
Same as parseInt but for unsigned integers.
- double parseFloat(const string &in, uint &out byteCount = 0)
Parses the string for a floating point value. If byteCount is provided it will be set to the number of bytes that were considered as part of the value.
- string s;
- s = "123";
- int i;
- log("--parseInt--");
- i = parseInt(s);
- log("i: "+i); // i: 123
- uint len;
- s = "-123.44 miles";
- i = parseInt(s, 10, len);
- log("i: "+i + " len: "+len); // i: -123 len: 4
- s = " 123 number";
- i = parseInt(s, 10, len); // can starts only with a number or -,+
- log("i: "+i + " len: "+len); // i: 0 len: 0
- s = "10e2"; // scientific notation works only with parseFloat
- i = parseInt(s);
- log("i: "+i); // i: 10
- s = "FF";
- i = parseInt(s, 16); // convert hex value
- log("i: "+i); // i: 255
- log("--parseUInt--");
- uint u;
- s = "123";
- u = parseUInt(s);
- log("u: "+u); // u: 123
- s = "-123";
- u = parseUInt(s);
- log("u: "+u); // u: 0
- s = "-123";
- u = parseInt(s); // just to test the difference
- log("u: "+u); // u: 4294967173
- s = "FF";
- u = parseUInt(s, 16); // convert hex value
- log("u: "+u); // u: 255
- s = "FFCCDD";
- u = parseUInt(s, 16, len);
- log("u: "+u + " len: "+len); // u: 16764125 len: 6
- s = "#FFCCDD"; // will fail cause starts with #
- u = parseUInt(s, 16, len);
- log("u: "+u + " len: "+len); // u: 0 len: 0
- log("--parseFloat--");
- double f;
- s = "123.45678910";
- f = parseFloat(s);
- log("f: "+formatFloat(f, "", 3, 10)); // f: 123.4567891000
- s = "-1e4 number"; // scientific notation works here
- f = parseFloat(s, len);
- log("f: "+f + " len: "+len); // f: -10000 len: 4
Formatting Functions
- string formatInt(int64 val, const string &in options = '', uint width = 0)
- // Blue Cat Audio alternative for DSP thread to convert without memory allocation
- void intToString(int64 val, string& s, const string &in options = "", uint width = 0);
Formats an integer value with specified options and width.
- string formatUInt(uint64 val, const string &in options = '', uint width = 0)
- // Blue Cat Audio alternative for DSP thread to convert without memory allocation
- void uIntToString(uint64 val, string& s, const string &in options = "", uint width = 0);
Formats an unsigned integer value with specified options and width.
- string formatFloat(double val, const string &in options = '', uint width = 0, uint precision = 0)
- // Blue Cat Audio alternative for DSP thread to convert without memory allocation
- void floatToString(double val, string& s, const string &in options = "", uint width = 0, uint precision = 0);
Formats a floating-point value with specified options, width, and precision.
Format Options
| Character | Description |
|---|---|
| l | left justify |
| 0 | pad with zeroes |
| + | always include the sign, even if positive |
| space | add a space in case of positive number |
| h | hexadecimal integer small letters (not valid for formatFloat) |
| H | hexadecimal integer capital letters (not valid for formatFloat) |
| e | exponent character with small e (only valid for formatFloat) |
| E | exponent character with capital E (only valid for formatFloat) |
Examples:
- // Left justify number in string with 10 characters
- string justified = formatInt(number, 'l', 10);
- // Create hexadecimal representation with capital letters, right justified
- string hex = formatInt(number, 'H', 10);
- // Right justified, padded with zeroes and two digits after decimal separator
- string num = formatFloat(number, '0', 8, 2);
Supporting String Object and Functions
The string object supports a number of operators, and has several class methods and supporting global functions to facilitate the manipulation of strings.
Note: Unless otherwise stated, all methods and functions work on the individual bytes in the string. They do not attempt to understand encoded characters, e.g. UTF-8 encoded characters that can take up to 4 bytes.
Operators
`=` Assignment
- string = string
The assignment operator copies the content of the right hand string into the left hand string.
Assignment of primitive types is allowed, which will do a default transformation of the primitive to a string.
`+`, `+=` Concatenation
- string + string
- string += string
The concatenation operator appends the content of the right hand string to the end of the left hand string.
Concatenation of primitive types is allowed, which will do a default transformation of the primitive to a string.
`==`, `!=` Equality
- string == string
- string != string
Compares the content of the two strings.
`<`, `>`, `<=`, `>=` Comparison
- string < string
- string > string
- string <= string
- string >= string
Compares the content of the two strings. The comparison is done on the byte values in the strings, which may not correspond to alphabetical comparisons for some languages.
`[]` Index Operator
- string[index]
The index operator gives access to a single byte in the string.
Comments
Please, authorize to view and post comments.