AppStudio 4 brings some handy new functions. Each of these saves you time: they replace several lines of sometimes messy code in your app.
Read on to see how to use Format, IndexOf, Push and Splice.
Format(string, replace0[,replace1…])
Replaces {n} placeholders with arguments. One or more arguments can be passed, in addition to the string itself, to insert into the string.
Ever need to replace several items in one string? This lets you do so in one statement. For each point you want to make a substitution, put an ascending number in curly braces ( {0}, {1}, {2} etc). Format will replace each of these with the additional parameters.
Print Format("1{0}2","a") Print Format("My name is {0} and I live in {1}.", "Cartman", "South Park") 'output 1a2 My name is Cartman and I live in South Park.
IndexOf(var, item[, fromIndex])
The IndexOf() method returns the position of item in var, starting the search at fromIndex. The variable item can be a string or an array. The result returned starts with 0. If item is not found, -1 is returned.
Ever wish you could search for an item in an array as easily as you can in a string using instr()? IndexOf lets you do this. (In fact, since IndexOf works on strings as well, you don’t need to use InStr() at all. Just remember that InStr() returns 1 instead of 0 if the item is found in the first position).
a=[1,2,3,4,5] Print IndexOf(a,3) Print IndexOf("abcde","c") Print IndexOf(a,9) 'output 2 2 -1
Push(array, item)
Push() adds an item to end of an array. This function changes the original array. It returns the number of items in the array after adding the new item.
This makes adding a new item to an array much easier. You no longer have to get the length of the array and increment it to get the new index.
fruits = ["Banana", "Orange", "Apple", "Mango"] count=Push(fruits,"Strawberry") Print count, fruits 'output 5 Banana,Orange,Apple,Mango,Strawberry
Splice(array, index, howmany[, item])
The splice() method adds/removes items to/from an array, and returns the removed item(s). This function changes the original array.
- array: The name of an existing array.
- index: An integer that specifies at what position to add/remove items, Use negative values to specify the position from the end of the array. Starts from 0.
- howmany: The number of items to be removed. If set to 0, no items will be removed.
- item: Optional. The new item(s) to be added to the array at index.
The function returns the deleted items.
Adding and deleting items in the middle of an array is always a nuisance. You need to create a For/Next loop and worry about the cases of the first and last element. Using Splice() will save you coding and debugging – and your code will be easier to understand.
fruits = ["Banana", "Orange", "Apple", "Mango"] returnVal = Splice(fruits,2,2) Print "Removed:", returnVal Print "New value", fruits Splice(fruits,2,1,"Blueberry") Print fruits 'output Removed: Apple,Mango New value: Banana,Orange Banana,Orange,BlueBerry