|
|
| Line 1: |
Line 1: |
| − | {| style="font-family:Verdana, Genega, sans-sarif; font-size:80%;color:gray;" width="100%" cellpadding="0%" cellspacing="0" border="0"
| + | <yambe:breadcrumb self="Strings">VEX2_Functions_and_Variables|Functions and Variables</yambe:breadcrumb> |
| − | |-
| + | <br /> |
| − | |
| + | |
| − | ''[[Main_Page|Main]] → [[VEX2_Main|CORTEX]] → [[VEX2: Functions| Functions and Variables]] → [[VEX2:_Functions_-_Strings|Strings]] ''
| + | |
| − | |-
| + | |
| − | |}
| + | |
| − | | + | |
| | | | |
| − | {| class="wikiText" | + | {| |
| | |- | | |- |
| | |''For information about displaying on the VEX LCD, please see the article: [[VEX2:_Functions_-_Display|Display]].'' | | |''For information about displaying on the VEX LCD, please see the article: [[VEX2:_Functions_-_Display|Display]].'' |
| Line 17: |
Line 12: |
| | |- | | |- |
| | |} | | |} |
| − | | + | <br /> |
| | | | |
| | {| | | {| |
| void StringDelete(string &sDest, const int nIndex, const int nSize)
|
| (void) Deletes a substring from a string.
|
| Parameter
|
Explanation
|
Data Type
|
| sDest
|
The string to delete from.
|
string
|
| nIndex
|
The index of the first character to delete (1st character = index 0).
|
int
|
| nSize
|
The number of characters to delete, starting 'nIndex'.
|
int
|
|
string str = "mesGARBAGEsage"; // String 'str' is "mesGARBAGEsage".
StringDelete(str, 3, 7); // Delete from index 3 to the 7th char after that.
/* Confusing because 3 = index 3, not char 3. Since indexing */
/* starts with 0, index 3 is the 4th character. */
displayNextLCDString(3, "%s", str1); // Display the new 'str' ("message").
|
|
| void StringFormat(string &sDest, const string sFormatString, ...)
|
| (void) Formats a string using the specified format-string.
|
| Parameter
|
Explanation
|
Data Type
|
| sDest
|
The string to format.
|
string
|
| sFormatString
|
The format-string to apply to 'sDest'.
|
string
|
|
string str = ""; // Create String, 'str' and initialize it as 'empty'.
float num = 3.14159; // Create float, 'num' and initialize it as first 6 digits of pi.
StringFormat(str, "%1.2f", num); /* Format num into a float to 2 decimal places, then send it to
the string, 'str'. */
displayNextLCDString(str); // Display the new string, str. ("3.14").
|
|
| void strncat(void &pToBuffer, const void &pFromBuffer, const short nMaxBufferSize)
|
| (void) Function concatenates pFromBuffer onto end of pToBuffer. The variables are arrays of bytes terminated with a zero character. nMaxBufferSize is the maximum size of ‘pFromBuffer’ and is usually created with a sizeof(..) function call. Identical to the function found in conventional C 'string.h' library file.
|
| Parameter
|
Explanation
|
Data Type
|
| pToBuffer
|
'pFromBuffer' gets concatenated onto the end of this.
|
void
|
| pFromBuffer
|
This gets concatenated onto the end of 'pToBuffer'.
|
void
|
| nMaxBufferSize
|
How many characters to concatenate.
|
short
|
|
string str1 = "ROBOT"; // String 'str1' is "ROBOT".
string str2 = "C!"; // String 'str2' is "C!".
int copy_length = 1; // Int 'copy_length' is set to 1.
strncat(str1, str2, copy_length); /* Concatinate 'copy_length' amount of characters
from string 'str2' onto string 'str1'. */
displayNextLCDString(str1); // Display the new 'str' ("ROBOTC"), (the "!" is not copied).
|
|
| short strncmp(void &pString1, const void &pString2, const short nMaxBufferSize)
|
| (short) Function compares pString1 with pString2. Returns negative value if less than, 0 if equal and positive value if greater than. The variables are arrays of bytes terminated with a zero char. nMaxBufferSize is the maximum number of bytes to compare and is usually created with a sizeof(..) function call. Identical to the function found in conventional C 'string.h'
|
| Parameter
|
Explanation
|
Data Type
|
| pString1
|
A string to compare with 'pString2'.
|
void
|
| pString2
|
A string to compare with 'pString1'.
|
void
|
| nMaxBufferSize
|
How many characters to compare, starting from index 0.
|
short
|
|
string str1 = "Fett"; // String 'str1' is "Fett".
string str2 = "Fetts"; // String 'str2' is "Fetts".
int cmp_chars = 4; /* Int 'cmp_chars' is the amount of chars
to compare in "strncmp()". Here we will
compare the first 4 chars of each string. */
if(strncmp(str1, str2, cmp_chars) < 0) // If 'str1' < 'str2' up to 4 chars:
{
displayNextLCDString("str1 is < than str2"); // Display that 'str1' is < 'str2' up to 4 chars.
}
else if(strncmp(str1, str2, cmp_chars) > 0) // If 'str1' > 'str2' up to 4 chars:
{
displayNextLCDString("str1 is > than str2"); // Display that 'str1' is > 'str2' up to 4 chars.
}
else // If 'str1' == 'str2' up to 4 chars:
{
displayNextLCDString("str1 = str2"); // Display that 'str1' is = 'str2' up to 4 chars.
}
|
|