Difference between revisions of "String Manipulation Functions"

From Flowcode Help
Jump to navigationJump to search
m (ReeceL moved page String manipulation functions to String Manipulation Functions without leaving a redirect)
 
(39 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 +
:''See [[Calculation Icon Properties]]''
 +
 +
__TOC__
 +
 +
==String Functions==
 +
 
The String functions are a set of string manipulation functions that can be used to edit, change and examine the strings.
 
The String functions are a set of string manipulation functions that can be used to edit, change and examine the strings.
  
To add a function, drag it into the calculation box. The user can then edit this base code with the variables required.
+
To add a function, drag it into the calculation box or double click. The user can then edit this base code with the variables required.
 +
 
 +
 
 +
{| style="margin:auto; text-align:center;"
 +
|-
 +
|[[File:Btn_Calculation.gif|border]]
 +
| width="50px" |→
 +
|[[File:Gen_Calculation_Flowchart_Icon_01.png]]
 +
|width="50px" |→
 +
|[[File:Gen_StringFunctions_01.jpg]]
 +
|}
 +
 
  
  
 
Example Strings used in explaining the functions below:
 
Example Strings used in explaining the functions below:
  
Str1[20] = "Hello "
+
Str1[20]
 +
 
 +
Str2[20]
  
Str2[10] = "World"
+
TestStr[40]
  
TestStr[20]
 
  
  
=
+
== = ==
  
Changes the string to the string given. If the array.
+
To assign a string value to a string variable you can simply use the = character.
  
If the string is longer than the array size then any extra characters are lost.
+
Str1 = "Hello "
  
 +
Str2 = "World"
  
TestStr = Str1 + Str2
 
  
 +
Strings can also be treated like byte arrays to allow you to manipulate the individual bytes. For string data to work correctly with other string functions the end of the data must be signified with a 0 or null byte.
  
TestStr is now "Hello World"
+
Str1[0] = 'H'
  
 +
Str1[1] = 'e'
  
+
+
Str1[2] = 'l'
  
Concatenates the two string together in the order presented from left to right.
+
Str1[3] = 'l'
  
If the resulting string is longer than the array size of the receiving string then any extra characters are lost.
+
Str1[4] = 'o'
  
 +
Str1[5] = ' '
  
TestStr = Str1 + Str2
+
Str1[6] = 0
  
  
TestStr is now "Hello World"
 
  
 +
Assigning a string constant to a string variable will automatically add the null byte to the end of the string for you.
  
'''ToString$(value)'''
 
  
Changes the numeric value to a String.
+
Str1 = "Hello "
  
  
TestStr = ToStr$(1234)
 
  
 +
Strings can also be used with escape characters such as \n (newline), \r (carriage return), \t (tab), \xXX (hexadecimal byte value), \\ single backslash character.
  
TestStr is now "1234"
+
Str1 = "Hello\n\rWorld"
  
  
'''ToUpper$(string)'''
+
== + ==
  
Changes all letters to upper case.
+
Concatenates the two string together in the order presented from left to right.
 +
 
 +
If the resulting string is longer than the array size of the receiving string then any extra characters are lost.
  
 
TestStr = ToUpper$(Str1)
 
  
+
Str1 = "Hello "
TestStr is now "HELLO "
 
  
 +
Str2 = "World"
  
'''ToLower$(string)'''
+
TestStr = Str1 + Str2
  
Changes all letters to lower case.
 
  
+
TestStr is now "Hello World"
TestStr = ToLower$(Str1)
 
  
 
TestStr is now "hello "
 
  
  
'''Length$(string)'''
+
Multiple concatenations are best done like this to avoid using excess memory. Note that each line contains only two values to be added together.
  
Retrieves the length of the string.
+
TestStr = "Multiple " + Str1
  
This is not the Array size, but the number of characters in the array before a Null character is encountered.
+
TestStr = TestStr + Str2
  
  
RetVal = Length$(Str1)
+
TestStr is now "Multiple Hello World"
  
  
RetVal is now 6
+
'''NOTE:''' Due to a limitation in generated code the result of multiple concatenations in one operation is limited to 20 characters. So the result of Str1 + Str2 + Str3 should not exceed 20 characters.
  
  
Note: Str1 array size is 20, but has a String of only 6 characters currently assigned to it hence the return value of 6.
 
  
 +
==Left$(string, size)==
  
'''Left$(string, size)'''
 
 
Retrieves a substring from the string of size characters starting from the leftmost character.
 
Retrieves a substring from the string of size characters starting from the leftmost character.
  
Line 100: Line 115:
  
  
'''Right$(string, size)'''
+
 
 +
==Right$(string, size)==
  
 
Retrieves a substring from the string of size characters starting from the rightmost character.
 
Retrieves a substring from the string of size characters starting from the rightmost character.
Line 115: Line 131:
  
  
'''Mid$(string, start, size)'''
 
  
Retrieves a substring from the string starting at position start of size characters.
+
==Mid$(string, start, size)==
 +
 
 +
Retrieves a substring from the string starting at position start of size characters. The first character of the string is at position zero.
  
 
If the length of the string used to store the result is less than size of the substring returned then any extra characters are lost.
 
If the length of the string used to store the result is less than size of the substring returned then any extra characters are lost.
Line 128: Line 145:
  
  
'''Compare$(string1, string2, compare_type)'''
+
{{Fcfile|Mid$.fcfx|Mid$ String Example}}
 +
 
 +
 
 +
==Length$(string)==
 +
 
 +
Retrieves the length of the string.
 +
 
 +
This is not the number of byte elements in the string variable, but the number of characters in the array before an ASCII Null character is encountered.
 +
 
 +
 
 +
Str1 = "Hello "
 +
 
 +
 
 +
RetVal = Length$(Str1)
 +
 
 +
 
 +
RetVal is now 6
 +
 
 +
 
 +
{{Fcfile|Length$.fcfx|Length$ String Example}}
 +
 
 +
 
 +
==Char$(string, number)==
 +
 
 +
Retrieves a single byte from the specified position in the string.
 +
 
 +
Str1= "Hello"
 +
 
 +
RetVal = Char$(Str1, 0)
 +
 
 +
Retval is now 'H'.
 +
 
 +
 
 +
RetVal = Char$(Str1, 4)
 +
 
 +
Retval is now 'o'.
 +
 
 +
 
 +
RetVal = Char$(Str1, 5)
 +
 
 +
Retval is now 0 (NULL terminator to mark the end of valid data).
 +
 
 +
 
 +
 
 +
String bytes can also be accessed directly using standard C square bracket array notation.
 +
 
 +
RetVal = Str1[4]
 +
 
 +
Retval is now 'o'.
 +
 
 +
 
 +
{{Fcfile|Char$.fcfx|Char$ String Example}}
 +
 
 +
 
 +
==ToString$(value)==
 +
 
 +
Changes the numeric value or variable to a String.
 +
 
 +
 
 +
TestStr = ToString$(1234)
 +
 
 +
 
 +
TestStr is now "1234"
 +
 
 +
==ToUpper$(string)==
 +
 
 +
Changes all letters to upper case.
 +
 
 +
 +
TestStr = ToUpper$(Str1)
 +
 
 +
 +
TestStr is now "HELLO "
 +
 
 +
 
 +
 
 +
==ToLower$(string)==
 +
 
 +
Changes all letters to lower case.
 +
 
 +
 +
TestStr = ToLower$(Str1)
 +
 
 +
 +
TestStr is now "hello "
  
Compares the two strings, parameters 1 and 2, and returns a BYTE value corresponding to the following results:
 
  
  
0 = strings are identical
+
==Compare$(string1, string2, compare_type)==
  
1 = string1>string2
+
Compares the two strings, parameters 1 and 2, and returns a BYTE value corresponding to the following results:
  
255 = string2>string1
+
*0 = strings are identical
 +
*1 = string1>string2
 +
*255 = string2>string1
  
  
 
The 3rd parameter, compare_type,  determines whether or not the check is case sensitive. values for compare_type are:
 
The 3rd parameter, compare_type,  determines whether or not the check is case sensitive. values for compare_type are:
  
0 = case sensitive
+
*0 = case sensitive
 +
*1 = case insensitive.
  
1 = case insensitive.
 
  
 
Examples
 
Examples
Line 174: Line 276:
  
  
'''FloatToString$( float )'''
+
{{Fcfile|StringCompare.fcfx|String Compare Example}}
 +
 
 +
 
 +
 
 +
==FloatToString$(float)==
  
 
Converts a floating point number into a string.
 
Converts a floating point number into a string.
Line 184: Line 290:
  
  
string = FloatToString( float )
+
string = FloatToString(float, NumberOfDecimalplaces)
 +
 
 +
E.g to round a float to 2 decimal places you can use
 +
 
 +
string = FloatToString(float, 2)
 +
 
 +
The number of decimal places can be omitted so you can have
 +
 
 +
string = FloatToString(float)
 +
 
 +
 
 +
{{Fcfile|FloatToString.fcfx|Float To String Example}}
  
  
'''NumberToHex$( number )'''
+
==NumberToHex$(number)==
  
 
Converts a byte or int number into a hexadecimal string.
 
Converts a byte or int number into a hexadecimal string.
Line 197: Line 314:
  
  
string = FloatToString( INT )
+
string = NumberToHex( number )
 +
 
 +
 
  
 +
{{Fcfile|NumberToHex.fcfx|Number To Hex String Example}}
  
'''StringToInt$(string)'''
+
 
 +
 
 +
 
 +
==StringToInt$(string)==
  
 
Converts a string of numeric ASCII data into an integer numeric data value.
 
Converts a string of numeric ASCII data into an integer numeric data value.
Line 213: Line 336:
  
  
'''StringToFloat$( string )'''
+
 
 +
==StringToFloat$(string)==
  
 
Converts a string of numeric ASCII data into a floating point variable.
 
Converts a string of numeric ASCII data into a floating point variable.

Latest revision as of 14:32, 7 June 2023

See Calculation Icon Properties

String Functions

The String functions are a set of string manipulation functions that can be used to edit, change and examine the strings.

To add a function, drag it into the calculation box or double click. The user can then edit this base code with the variables required.


Btn Calculation.gif Gen Calculation Flowchart Icon 01.png Gen StringFunctions 01.jpg


Example Strings used in explaining the functions below:

Str1[20]

Str2[20]

TestStr[40]


=

To assign a string value to a string variable you can simply use the = character.

Str1 = "Hello "

Str2 = "World"


Strings can also be treated like byte arrays to allow you to manipulate the individual bytes. For string data to work correctly with other string functions the end of the data must be signified with a 0 or null byte.

Str1[0] = 'H'

Str1[1] = 'e'

Str1[2] = 'l'

Str1[3] = 'l'

Str1[4] = 'o'

Str1[5] = ' '

Str1[6] = 0


Assigning a string constant to a string variable will automatically add the null byte to the end of the string for you.


Str1 = "Hello "


Strings can also be used with escape characters such as \n (newline), \r (carriage return), \t (tab), \xXX (hexadecimal byte value), \\ single backslash character.

Str1 = "Hello\n\rWorld"


+

Concatenates the two string together in the order presented from left to right.

If the resulting string is longer than the array size of the receiving string then any extra characters are lost.


Str1 = "Hello "

Str2 = "World"

TestStr = Str1 + Str2


TestStr is now "Hello World"


Multiple concatenations are best done like this to avoid using excess memory. Note that each line contains only two values to be added together.

TestStr = "Multiple " + Str1

TestStr = TestStr + Str2


TestStr is now "Multiple Hello World"


NOTE: Due to a limitation in generated code the result of multiple concatenations in one operation is limited to 20 characters. So the result of Str1 + Str2 + Str3 should not exceed 20 characters.


Left$(string, size)

Retrieves a substring from the string of size characters starting from the leftmost character.

If the length of the string used to store the result is less than size of the substring returned then any extra characters are lost.


TestStr = Left$(Str1, 3)


TestStr is now "Hel"


Right$(string, size)

Retrieves a substring from the string of size characters starting from the rightmost character.

i.e. a size of 4 retrieves the 4 rightmost characters.

If the length of the string used to store the result is less than size of the substring returned then any extra characters are lost.


TestStr = Right$(Str1, 3)


TestStr is now "lo "


Mid$(string, start, size)

Retrieves a substring from the string starting at position start of size characters. The first character of the string is at position zero.

If the length of the string used to store the result is less than size of the substring returned then any extra characters are lost.


TestStr = Mid$(Str1, 2, 3)


TestStr is now "llo"


FC6 Icon.png Mid$ String Example


Length$(string)

Retrieves the length of the string.

This is not the number of byte elements in the string variable, but the number of characters in the array before an ASCII Null character is encountered.


Str1 = "Hello "


RetVal = Length$(Str1)


RetVal is now 6


FC6 Icon.png Length$ String Example


Char$(string, number)

Retrieves a single byte from the specified position in the string.

Str1= "Hello"

RetVal = Char$(Str1, 0)

Retval is now 'H'.


RetVal = Char$(Str1, 4)

Retval is now 'o'.


RetVal = Char$(Str1, 5)

Retval is now 0 (NULL terminator to mark the end of valid data).


String bytes can also be accessed directly using standard C square bracket array notation.

RetVal = Str1[4]

Retval is now 'o'.


FC6 Icon.png Char$ String Example


ToString$(value)

Changes the numeric value or variable to a String.


TestStr = ToString$(1234)


TestStr is now "1234"

ToUpper$(string)

Changes all letters to upper case.


TestStr = ToUpper$(Str1)


TestStr is now "HELLO "


ToLower$(string)

Changes all letters to lower case.


TestStr = ToLower$(Str1)


TestStr is now "hello "


Compare$(string1, string2, compare_type)

Compares the two strings, parameters 1 and 2, and returns a BYTE value corresponding to the following results:

  • 0 = strings are identical
  • 1 = string1>string2
  • 255 = string2>string1


The 3rd parameter, compare_type, determines whether or not the check is case sensitive. values for compare_type are:

  • 0 = case sensitive
  • 1 = case insensitive.


Examples


Str1 = "ABC"

Str2 = "abc"


RetVal = Compare$(Str1, Str2, 0)


RetVal is now 255 as Str2 is later in the ASCII sequence.


RetVal = Compare$(Str1, Str2, 1)


RetVal is now 0 as when case insensitive the two strings are identical.


Str2 = Str1

RetVal = Compare$(Str1, Str2, 0)


RetVal is now 0 as both strings are identical.


FC6 Icon.png String Compare Example


FloatToString$(float)

Converts a floating point number into a string.


float = Floating point number to convert.

string = String variable to hold the conversion data.


string = FloatToString(float, NumberOfDecimalplaces)

E.g to round a float to 2 decimal places you can use

string = FloatToString(float, 2)

The number of decimal places can be omitted so you can have

string = FloatToString(float)


FC6 Icon.png Float To String Example


NumberToHex$(number)

Converts a byte or int number into a hexadecimal string.


number = byte or integer number to convert.

string = String variable to hold the conversion data.


string = NumberToHex( number )


FC6 Icon.png Number To Hex String Example



StringToInt$(string)

Converts a string of numeric ASCII data into an integer numeric data value.


string = String variable containing numeric ASCII data.

returns numeric data from the string in decimal form.


number = StringToInt$( string )


StringToFloat$(string)

Converts a string of numeric ASCII data into a floating point variable.


string = String variable containing numeric ASCII data.

returns numeric data from the string in floating point form.


float = StringToFloat$( string )