img

Operating Strings

Operators for working with strings

OperatorDescription
= String assignment
+ Concatenation of strings
== Checking strings for equality
!= Checking strings for inequality
<, >, <=, >= String comparisons
[] Access to the elements of string by the index

Example. Printing all letters in a string one by one.

 int Initialize() { return(0); } int Run() { string a = "NetTradeX"; for (uint i=0;i<a.length;i++) { System.Print(""+int8(a[i])); } return(0); } int DeInitialize() { return(0); } 

Strings operating methods

MethodDescription
uint length() const Length of a string
void resize(uint) Changing the length of a string
bool isEmpty() const Verification that the string is empty
string substr(uint start = 0, int count = -1) const; Returning a substring starting from the position start with the length of count symbols.
int findFirst(const string &in str, uint start = 0) const; Finding the first occurrence of a substring str , starting from start position.
int findLast(const string &in str, int start = -1) const; Finding the last occurrence of a substring str not further than start symbols from the beginning.
array@ split(const string &in delimiter) const; Splitting a string into substrings that are recorded in an array.

Example of operating string methods:

 int Initialize() { return(0); } int Run() { string a = "Hello, NetTradex Advisor!"; System.Print("Substring = "+a.substr(3,10)); // Output: Substring = lo, NetTra System.Print("Find substring \"tT\" pos = "+a.findFirst("tT",1)); // Output: Find substring "tT" pos = 9 System.Print("Find last = "+a.findLast("o",6)); // Output: Find last = 4 array<string>words = a.split(" "); for(uint i=0;i<words.length();i++) { System.Print(words[i]); } System.Print("Joined="+join(words," ")); return 0; } int DeInitialize() { return(0); } 

Strings functions

Function nameDescription
string join(const array &in arr, const string &in delimiter); Putting together elements of an array arr using the delimiter, specified in delimiter variable.
int64 parseInt(const string &in, uint base = 10, uint &out byteCount = 0) Converting a string to an int64 variable
double parseFloat(const string &in, uint &out byteCount = 0) Converting a string to a double variable
string formatInt(int64 val, const string &in options, uint width = 0) Converting int64 variable to a string using the specified format * .
string formatFloat(double val, const string &in options, uint width = 0, uint precision = 0) Converting double variable to a string using the specified format*.

* Strings format specification flags

  • l - left-align
  • 0 - add zeros on the left
  • + - add + for positive numbers
  • Space - add a space from the left for positive numbers
  • h - use lowercase letters for hexadecimal numbers
  • H - use capital letters for hexadecimal numbers
  • e - use the lowercase "e" for the exponent
  • E - use the capital "E" for the exponent

Example 1. Demonstrating the use of formatInt and formatFloat functions:

 int Initialize() { return(0); } int Run() { System.Print(""+formatInt(1245,"0+", 10)); // выведется +000001245 System.Print(""+formatInt(0xAbCd,"lH", 10)); // выведется ABCD System.Print(""+formatFloat(1234567,"E",10,4)); // 1.2346E+006 return(0); } int DeInitialize() { return(0); } 

Example 2. Conversion a string into a number:

 int Initialize() { return(0); } int Run() { uint a; System.Print(""+parseInt("12345678",10,a)); System.Print("a="+a); return(0); } int DeInitialize() { return(0); }