Solidity – Storing String Value in Solidity Efficiently

contract-developmentsolidity

Are there any restrictions on the length of the value that can be stored as type string in Solidity? I am trying to store a string value in the smart contract member field. The length comes to around 220 chars. If I reduce the string value to around 140 chars I am able to store it. Any more and it does not work.

Best Answer

the problem is not about the string size but about the provided gas, the storage costs more gas as you store more data. so when you increase the string size increase the gas limit provided in your transaction. using the following example :

contract store{
string public storage_;

function store_it(string s){

    storage_=s;
}


}

I've provided a string with 220 character it costs me 774675 gas.

Related Topic