I am try to compare a string variable in solidity code, mainly i have to check if the string is empty or not. How can it be achieved and also how much long/large of a string i can save in a string variable.

contract BOLData{
struct Data {
    string sData;
    string ssCompanyData;
    string ConsigneeData;
    string BOL_ID;

mapping(string => Data) private BOL_Data;
//Add the initial value 
function BOLData (string s_Data, string  bolid)
    BOL_Data[bolid] = Data(s_Data,'', '', bolid);
//Add data, hash to Block Chain
function addData(string ssCompany_Data, string Consignee_Data, string bolid) public {
    string s_DataO = BOL_Data[bolid].sData;
    string ssCompany_DataO = BOL_Data[bolid].ssCompanyData;
    string Consignee_DataO = BOL_Data[bolid].ConsigneeData;
    if(ssCompany_DataO == '')
    BOL_Data[bolid] = Data(s_DataO,ssCompany_Data, Consignee_Data, bolid);

//Fetch data, hash values from block chain
function getData(string DataID) returns (string, string, string){
    string s_Data = BOL_Data[DataID].sData;
    string ssCompany_Data = BOL_Data[DataID].ssCompanyData;
    string Consignee_Data = BOL_Data[DataID].ConsigneeData;
    return(s_Data, ssCompany_Data, Consignee_Data);


1 Answer 1


A string can be of any size, there is no limit.

You can check if a string memory str is empty like this:

if (bytes(str).length == 0)
    // String is empty...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.