String functions

The following functions - mostly from the standard C library - can be used to manipulate strings:

strcpy (string dest, string src): string

Fills the char array dest with the content of string src. The buffer size of dest must not be exceeded.

Parameters:

dest - destination string, either a char array of sufficient length or a temporary string as returned from strmid, strf, or strx.
src - source string
 

strcat (string dest, string src): string

Appends a copy of src to the end of the string contained in the char array dest. The buffer size of dest must not be exceeded.

Parameters:

dest - destination string, either a char array of sufficient length or a temporary string as returned from strmid, strf, or strx.
src - source string
 

strlen (string str): int

Returns the number of characters in the given string.

Parameters:

str - string
 

strcount (string str, char c): int

Returns the number of occurrences of the character c in the given string. This is not a standard C string function.

Parameters:

str - string
c - character to be counted, f.i. 0x0a for a line feed.
 

strcmp (string str1, string str2): int

Compares the strings (case sensitive) and returns 0 if they have the same content. For comparing strings with text constants and case insensivity, the == and != operators can alternatively be used.

Parameters:

str1 - first string to compare
str2 - second string to compare
 

strstr (string str1, string str2): string

Returns a substring beginning with the first occurrence (case sensitive) of str2 within str1, or NULL when str2 is not contained in str1. This function is often used to check if a string has the same content or is contained in another string.

Parameters:

str1 - string to search within
str2 - substring to be found
 

strchr (string str, int c): string

strrchr (string str, int c): string

Returns a substring beginning with the first (strchr) or last (strrchr) occurrence of the character c in the string str, or NULL when c is not contained in str. This function can be used f.i. for getting the extension (search for '.') or the file name (search for '\\') from a path name. Another use is checking if a certain character is contained in a set of characters.

Parameters:

str - string to search within
c - character to be found
 

strtok (string str, string delimiter): string

Returns a substring from the string str. The set of characters in the delimiter string specifies the characters that separate the substrings.

Parameters:

str - string to separate the first substring from, or 0 for continuing the search with the next substring. The string is modified by replacing the delimiters with 0 bytes.
delimiter - character or set of characters that separate the substrings, f.i. "," for the comma that separates fields in a CSV file.
 

strvar (string str, string name, var val): var

Parses a number from the string str that is preceded by the identifier name. If no identifier name is found in the string str, the function returns val (default 0). Can be used to parse an .ini file. This is not a standard C string function.

Parameters:

str - string containing the number.
name - substring, variable identifier, or 0 to read the first number out of the string.
val - returned when no identifier name or no number was found in the string.
 

strtext (string str, string name, string default): string (temporary)

Parses text from the string str and returns a temporary string containing the text preceded by the identifier name. If the text contains spaces, it should be in double quotes inside str. If no identifier name is found in the string str, the function returns default. Can be used to parse an .ini file. This is not a standard C string function.

Parameters:

str - string containing the text to be extracted.
name - substring, text identifier.
default - returned when no identifier name was found in the string.
 

strtr (TRADE* T): string

Returns a temporary string with a trade identifier, as in the log messages. This is not a standard C string function.

Parameters:

T - trade pointer.
  

strmid (string str, int first, int count): string

Returns a temporary copy of a substring of str, with length count and starting at position first. This is not a standard C string function.

Parameters:

str - string containing the source text.
first - position of the first character of the copy; first = 0 copies the string from the begin.
count - number of characters in the copy; 1000 characters maximum. If count exceeds the length of str, it is copied until the end.
 

strx (string str, string orig, string repl): string (temporary)

strxc (string str, char orig, char repl): string (temporary)

Returns a temporary copy of str where all ocurrences of orig are exchanged with repl. This is not a standard C string function. For cutting off a string at a certain character, replace it with 0 (f.i. strxc(Name, '.', 0) cuts off the extension from a file name).

Parameters:

str - string containing the source text.
orig - original text or char to be exchanged.
repl - replacement text or char; the returned string must not exceed 1000 characters.
 

strw (string str): short* (temporary)

Converts an 8-bit char string to a temporary 16-bit wide string (an array of shorts). Useful for passing small strings, such as file names, to DLLs that require 16-bit characters. This is not a standard C string function.

Parameters:

str - string containing the text to be converted, 1000 characters max.
 

stridx (string str): int

Converts a string to a unique integer in the 0..1000 range. Useful for indexing arrays or datasets with strings, f.i asset names. This is not a standard C string function.

Parameters:

str - string to be converted to an index, 15 characters max.

strxid (int index): string

Returns the string associated by stridx to the given index.
 

strf (string format, ...): string (temporary)

Returns a temporary string with data and variables formatted by the format string (see format codes). This is not a standard C string function.

Parameters:

format - format string, limited to a single line (no "\n" characters) and to maximum 1000 characters of the returned string.
 

sprintf (string dest, string format, ...): int

Like strf, but fills a given char array with formatted data, and returns the number of characters written. The dest char array must have sufficient size.

Parameters:

dest - destination string, a char array of sufficient length.
format - format string (see format codes).
 

sscanf (string str, string format, ...): int

Reads formatted data from a string into variable pointers or substrings according to the format string (see example). Returns the number of variables read from the string. The execution time of this function depends on the string length, so don't use it for very long strings such as the content of a whole file. Special format codes are supported, f.i. "%[^\n]" reads a substring until the end of the line, or "%[^,]" parses a line into comma-separated strings. Details can be found in any C/C++ manual or online documentation.  !!  Unlike sprintf and printf, for reading floating point numbers with %f the target variable must be float; var or double require %lf.

Parameters:

str - string to parse
format - format string similar to format codes, but with additional fields.
 

atof (string str): var

atoi (string str): int

Converts a number from a string to a var or int variable. If the string does not begin with a valid number, 0 is returned.

Parameters:

str - string containing a number.

Remarks:

Examples (see also file functions and Simulate.c):

// read a variable out of a string
// with a format like "name = 123.456"
var readVar(string str,string name)
{
	float f = 0; // sscanf needs float
	string s = strstr(str,name);
	if(s) s = strstr(s,"=");
	if(s) sscanf(s+1,"%f",&f);
	return f;
}

function main()
{
	var Test1 = 0,Test2 = 0;
	string s = file_content("Strategy\\vars.ini");
	if(s) {
		Test1 = readVar(s,"Test1");
		Test2 = readVar(s,"Test2");
	}	
	printf("\nTest1 = %f, Test2 = %f",Test1,Test2);
}

See also:

printf, string, strdate, file_functions, putvar, format codes

► latest version online