1

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?

Adam Kipnis
  • 670
  • 1
  • 5
  • 12
Huang Kai
  • 11
  • 1
  • 3
  • This is such a common struggle, I wrote a cheat sheet about it over here: https://ethereum.stackexchange.com/questions/13167/are-there-well-solved-and-simple-storage-patterns-for-solidity – Rob Hitchens Jan 04 '18 at 17:22

1 Answers1

1

Initializing a dynamic length string array is done just like any other type:

string[] someStrings;

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.