String array in solidity
Asked Answered
D

9

24

I came across quite a common problem that it seems I can't solve elegantly and efficiently in solidity.

I've to pass an arbitrary long array of arbitrary long strings to a solidity contract.

In my mind it should be something like

function setStrings(string [] row)

but it seems it can't be done.

How can I solve this problem?

Drews answered 10/3, 2017 at 11:5 Comment(1)
I just added an updated answer as of December 2021Wretched
S
37

This is a limitation of Solidity, and the reason is that string is basically an arbitrary-length byte array (i.e. byte[]), and so string[] is a two-dimensional byte array (i.e. byte[][]). According to Solidity references, two-dimensional arrays as parameters are not yet supported.

Can a contract function accept a two-dimensional array?

This is not yet implemented for external calls and dynamic arrays - you can only use one level of dynamic arrays.

One way you can solve this problem is if you know in advanced the max length of all of your strings (which in most cases are possible), then you can do this:

function setStrings(byte[MAX_LENGTH][] row) {...}

Scarron answered 21/3, 2017 at 16:52 Comment(1)
And then how the conversion would be?Calculating
W
15

December 2021 Update

As of Solidity 0.8.0, ABIEncoderV2, which provides native support for dynamic string arrays, is used by default.

pragma solidity ^0.8.0;

contract Test {
    string[] public row;

    function getRow() public view returns (string[] memory) {
        return row;
    }

    function pushToRow(string memory newValue) public {
        row.push(newValue);
    }
}
Wretched answered 3/12, 2021 at 13:22 Comment(0)
E
2

String arrays as parameters aren't supported yet in solidity.

Equivocate answered 8/1, 2018 at 0:20 Comment(0)
L
2

You can convert the array elements to a byte string and then deserialize that byte string back to the array inside the function. Although this can prove to be quite expensive you can try it if you don't have a choice. You can follow this short article to serialize/deserialize any datatype in solidity.

Lawn answered 6/6, 2018 at 19:44 Comment(0)
L
2

all solutions you need:-

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.1;

contract HelloWorld {
    string[] strings;

    // push one string to array
    function pushToStrings(string memory _data) public{
        strings.push(_data);
    }
    
    //get all the strings in array form
    function GetAllStrings() view public returns(string[] memory){
        return strings;
    }

    //get nth string of strings array
    function GetNthStrings(uint x) view public returns(string memory){
        return strings[x];
    }

    //push array of strings in strings
    function pushStringsArray(string[] memory someData) public{
        for (uint i=0; i < someData.length; i++) {
           strings.push(someData[i]);
        }
    }
    
    //change whole strings, take array of strings as input
    function changeWholeString(string[] memory someData) public{
       strings=someData;

    }
}
Lackaday answered 9/1, 2022 at 7:24 Comment(0)
G
1

string array is not available in Solidity because String is basically array of character Nested dynamic arrays not implemented

Gooch answered 23/7, 2018 at 13:50 Comment(0)
T
1

There are two types arrays in solidity: static array and dynamic array.

declaration of array

static array: These has fixed size.

int[5] list_of_students;
list_of_students = ["Faisal","Asad","Naeem"];

we access the values using index number

Dynamic arrays: The size of these arrays dynamically increases or decreases.

int[] list_of_students;
list_of_students.push("Faisal");
list_of_students.push("Asad");
list_of_students.push("Smith");

we can access the value using index number. push and pop function is used to insert and delete the values. length function is used to measure the length of the array.

Trimetric answered 30/8, 2019 at 13:54 Comment(0)
B
1

It can be done by using pragma experimental ABIEncoderV2; at the top of your contract you may then use dynamic arrays of strings. Ex. string[] memory myStrings;

Berryberryhill answered 28/11, 2019 at 4:32 Comment(0)
S
0

This is an example contract to manage the array push, get, getAll, and remove

pragma solidity ^0.8.4;

contract Array {
  string[] private fruits = ["banana", "apple", "avocado", "pineapple", "grapes"];

  function push(string memory item) public {
    fruits.push(item);
  }

  function get(uint256 index) public view returns (string memory) {
    return fruits[index];
  }

  function remove(uint256 index) public returns (bool) {
    if (index >= 0 && index < fruits.length) {
      fruits[index] = fruits[fruits.length - 1];
      fruits.pop();
      return true;
    }
    revert("index out of bounds");
  }

  function getAll() public view returns (string[] memory) {
    return fruits;
  }
}

Superfecundation answered 4/2, 2022 at 7:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.