I am trying to build a simple application on ethereum where strings can be added/deleted from an array and the contents of this array can be shown in a frontend UI.
The basic structure of the smart contract is:
pragma solidity ^0.4.18;
contract ExampleApp {
<string array over here>
function add(string x) public {
}
function delete(string x) public{
}
}
I have 2 questions: How can I initialise a string array and secondly how to delete a specific item from the string array? Do I have to iterate through the array to find the string or is there some simpler way?
Best Answer
Initializing a dynamic length string array is done just like any other type:
Deleting a specific item from an array of any type by the value itself is not possible without looping thru all elements until you find the target element. You could use something like a
Set
instead, though that's not a native type in Solidity.