evolve
Strings
Strings
The following statements can be used to manipulate strings.
Adding to a string
The str_add command can be used to add two strings or a string and another variable type together, this is demonstrated in the following code. It can be used to add multiple values into a string by simply appending the addition values to the end of the command.
str_add Str1 IntVar " test " 0x32
//---------------------------------------
// File: StringAdd.e
//---------------------------------------
//---------------------------------------
// Block: go
//---------------------------------------
block Go
//define some variables
Str1 = "add "
Str2 = "test "
IntVar = 10
//add different data types to string
str_add Str1 Str2
outl Str1
str_add Str1 "="
outl Str1
str_add Str1 " "
outl Str1
//Add multiple items
str_add Str1 IntVar " test " 0x32
outl Str1
_block
Finding a String within a String
The str_find command allows you to find the occurrence of one string within another the command takes four arguments. The first argument should be passed an integer variable to store the position of the found string in, if the string is not found then the command will return the position 0xFFFF. The second argument is the string to search within, the third argument is the string to search for and the final argument is the position within the string to start the search at. The str_findr command is the same but searches backwards through the string.
So the following line will return the position of 'token' in Str1 starting at the first character, the variable TokenPosition will be set to the index of the start of 'token' in Str1
str_find TokenPosition Str1 "token" 0
The following example shows str_find and str_findr in action.
//---------------------------------------
// File: StringFind.e
//---------------------------------------
//---------------------------------------
// Block: go
//---------------------------------------
block Go
//define a string
Str1 = "token1, token2, token3, token4 "
//find the tokens
str_find TokenPosition Str1 "token" 0
while TokenPosition != 0xFFFF
outl "Token found at:" TokenPosition
++ TokenPosition
str_find TokenPosition Str1 "token" TokenPosition
_while
//find the tokens in the reverse direction
str_len TokenPosition Str1
while TokenPosition != 0xFFFF && TokenPosition != 0
-- TokenPosition
str_findr TokenPosition Str1 "token" TokenPosition
outl "Token found at:" TokenPosition
_while
_block
Searching in a string using a Regular Expression
Strings can be searched using regular expressions these allow data to be extracted using a template. The str_regex command the syntax is
str_regex <container list> <string> <regular expression>
The first variable is a container list where any results of found strings and the positions are returned. If no matches are found then the list will be empty. The second variable is the string that will be searched and the third variable is the regular expression to be found. The container that is returns contains 2 variables .Str is the found matching string and .Pos is the position of the found string. The following example shows how it works. For more information on regular expressions look on line.
//---------------------------------------
// File: StringRegExe.e
//---------------------------------------
//---------------------------------------
// Block: Go
//---------------------------------------
block Go
Test = "this subject has a submarine as a subsequence"
RegEx = "\b(sub)([^ ]*)"
str_regex @Result Test RegEx
lst_size Size Result_Pos
loop I Size
outl Result.Pos[I] " " Result.Str[I]
_loop
_block
Searching in a using wild card expression
Strings can be searched using wild card expressions these allow data to be extracted using a template. The str_wild command the syntax is
str_wild <result container> <string> <wild card expression> <start pos>
The first variable is a container list where any results of found strings and their positions are returned. If no matches are found then the list will be empty. The second variable is the string that will be searched, the third variable is the regular expression to be found. The final one is the position in the string to start the search from this argument is optional, if it is not included then the search will be recursive until the end of the string is found. The wild card symbol is *.
So if you wanted to search a string for all instances of words wrapped in parenthesis then the search string would be (*). The following example shows this after the command is run the Result list should have 3 entries '1', 'name' and 'word'.
str_wild Result “(1) some text (name) more text (word)” “(*)”
The following example shows how it works.
//---------------------------------------
// File: StrWild.e
//---------------------------------------
//---------------------------------------
// Block: Go
//---------------------------------------
block Go
//Non recursive example
Str = "ThisResult1TestResult2Help----ThisResult3TestResult4Help"
TestStr = "This*Test*Help"
str_wild @Result Str TestStr 0
outl Result.Str[0] " at " Result.Pos[0]
outl Result.Str[1] " at " Result.Pos[1]
new_line
str_wild @Result Str TestStr 15
outl Result.Str[0] " at " Result.Pos[0]
outl Result.Str[1] " at " Result.Pos[1]
new_line
//Recursive example
Str = "ThisResult1TestResult2Help----ThisResult3TestResult4Help"
TestStr = "This*Test*Help"
str_wild @Result Str TestStr
outl Result.Str[0] " at " Result.Pos[0]
outl Result.Str[1] " at " Result.Pos[1]
outl Result.Str[2] " at " Result.Pos[2]
outl Result.Str[3] " at " Result.Pos[3]
_block
Replacing a Substring
A substring of a string can be replaced with another string using the str_replace command. The commands syntax is as follows.
str_replace <string> <find string> <replace string>
It searches the string with the string to find and replaces any occurrences with the replacement string.
//---------------------------------------
// File: StringReplace.e
//---------------------------------------
//---------------------------------------
// Block: Go
//---------------------------------------
block Go
Str = "this is my test"
str_replace Str "my" "your"
outl Str
//mulitple finds
Str = "this is my test my test"
str_replace Str "my" "your"
outl Str
//replace with value
Str = "this is test10"
str_replace Str "10" 20
outl Str
//single character
Str = "aaaaaaaaaaaaaaa"
str_replace Str "a" "b"
outl Str
_block
Getting a Substring
The str_sub command allows extraction of part of a string, the command takes the following arguments the destination string, the source string, the character to start at and the number of characters to get. The following line of code will set SubStr to the two charcters that start at index three so it would equal 'de'.
str_sub SubStr “abcdefg” 3 2
This is demonstrated in the following code
//---------------------------------------
// File: StringSub.e
//---------------------------------------
//---------------------------------------
// Block: go
//---------------------------------------
block Go
TOKENSIZE = 6
//define a string
Str1 = "token1, token2, token3, token4"
//find the tokens
str_find TokenPosition Str1 "token" 0
while TokenPosition != 0xFFFF
//get the token from the string
str_sub Token Str1 TokenPosition TOKENSIZE
//display the token
outl Token
++ TokenPosition
str_find TokenPosition Str1 "token" TokenPosition
_while
_block
Splitting a String
The str_split command can be used to split a string to the left or the right of an index within the string. The command take three arguments a string to return to the new sub string, the string to split, the index to split the string and a constant #LEFT or #RIGHT to define which section of the string should be returned. The returned string will contain the sub string to the left or right of the index excluding the character at the index.
//---------------------------------------
// File: StringSplit.e
//---------------------------------------
//---------------------------------------
// Block: Go
//---------------------------------------
block Go
Str = "Split this String"
str_split Str1 Str 5 #LEFT
outl Str1
str_split Str1 Str 5 #RIGHT
outl Str1
_block
Other String Statements
str_len is used to get the length of a string the string is the second argument and the length is returned in the first argument.
str_len Length “abcdefgh”
In the above line of code Length would be set to 8.
str_strtobyte takes three arguments The first is the byte to return the character in the second is the string to get the character from and the third is the index of the character to get.
str_strtobyte ByteVal “teststring” 3
In the above line of code ByteVal would be set to 116.
str_bytetostr converts a byte value into a string the first argument is the string and the second is the byte to convert to the string. The following code will to convert the ASCII value 117 to the string “A”.
str_bytetostr Str 117
These statements are demonstrated in the following code.
//---------------------------------------
// File: StringOther.e
//---------------------------------------
//---------------------------------------
// Block: go
//---------------------------------------
block Go
TOKENSIZE = 6
//define a string
Str1 = "abcdfghijkl"
//display the length
str_len Length Str1
outl "Length: " Length
//loop displaying the bytes of the string
I = 0
while I < Length
str_strtobyte ByteValue Str1 I
outl "Char" I " = " ByteValue
++ I
_while
//Create a new string from bytes
Str1 = ""
ByteValue = 0x00
ByteValue = 'm'
//add bytes until get to z
while ByteValue < 'z'
//convert byte to string
str_bytetostr StrByte ByteValue
//add to main string
str_add Str1 StrByte
//increment the byte
++ ByteValue
_while
//display the strings
outl Str1
_block