NXC  Version 1.2.1 r5
 All Data Structures Files Functions Variables Groups Pages
ex_string.nxc

This is an example of how to use the string API functions: StrToNum, StrLen, StrIndex, NumToStr, StrCat, SubStr, Flatten, StrReplace, FormatNum, FlattenVar, UnflattenVar, ByteArrayToStr, ByteArrayToStrEx, and StrToByteArray.

task main()
{
string msgs[] = {"please work", "testing, 1, 2, 3"};
string fmts[] = {"x = %4.4d", "0x%x"};
string tmp = "123456";
string s = SubStr(tmp, 2, 3);
string a = "AA", b = "BB", c = "CC";
TextOut(0, LCD_LINE2, SubStr(msgs[0], 2, 3));
TextOut(0, LCD_LINE3, StrCat(a, SubStr(tmp, 2, 3), msgs[0]));
TextOut(0, LCD_LINE4, StrReplace(msgs[0], 2, StrCat(a, b)));
NumOut(0, LCD_LINE5, StrIndex(msgs[0], 0));
NumOut(0, LCD_LINE6, StrLen(msgs[0]));
TextOut(0, LCD_LINE7, FormatNum(fmts[0], Random(34)));
float val = StrToNum("10.5abc123");
NumOut(0, LCD_LINE8, val);
int x = 0x7172;
string foo = FlattenVar(x);
TextOut(0, LCD_LINE2, foo);
TextOut(0, LCD_LINE3, Flatten(0x7374));
NumOut(0, LCD_LINE4, strlen(foo));
NumOut(40, LCD_LINE4, UnflattenVar(foo, x));
TextOut(0, LCD_LINE5, FormatNum(fmts[1], x));
string bats = tmp; // "123456"
TextOut(0, LCD_LINE6, bats);
byte data[];
StrToByteArray(bats, data);
ByteArrayToStrEx(data, tmp);
TextOut(0, LCD_LINE8, tmp);
}