What Are Strings In Solidity?

Solidity strings are used to represent textual data. They are a sequence of Unicode characters enclosed in double quotes ().

Strings can contain any Unicode character, including letters, digits, punctuation marks, and special characters.

In this article, we will explore Solidity strings in depth, discussing their properties, limitations, how they are declared, and how they are used in smart contracts.



Solidity Strings

In Solidity, String literals can be used with either double quotes () or single quotes ().

To declare a string in Solidity, you can use the keyword string.

The String data type is available to declare variables of type String:

Example: 

// SPDX-License-Identifier: GPL-3.0 pragma solidity ^0.8.0;contract SolidityTest{function get_String() public pure returns(string memory){ string memory mrx="Hey User !! You are learning about Solidity String"; return mrx;}}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Example: 

// SPDX-License-Identifier: GPL-3.0pragma solidity ^0.8.0;contract SolidityTest{function printString() public pure returns(string memory){string memory mrx=" Greetings User !! We are learning about Solidity Strings ";return mrx; }}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Example Explanation

According to the example above, “Hey User !! You are learning about Solidity String” represents a string literal with data representing a string variable.

Bytes are preferred over Strings since string operations require more gas than byte operations.

There is an inbuilt conversion between bytes and strings in Solidity.

It is easy to assign a String literal to a byte32 type variable in Solidity. Solidity treats it as a byte32 literal.

Example: 

// SPDX-License-Identifier: GPL-3.0pragma solidity ^0.8.0;contract SolidityTest{bytes32 name="Mr.Examples";function get_Bytes_Name() public returns(bytes32){ return name; }}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Example: 

// SPDX-License-Identifier: GPL-3.0pragma solidity ^0.8.0;contract SolidityTest{bytes32 my_string_to_byte="I hope Solidity strings are easy";function String_To_Bytes() public returns(bytes32){return my_string_to_byte; } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

solidity strings example output


Solidity Escape Characters

In Solidity, escape characters are used to represent special characters or sequences of characters in a string.

Here are some of the commonly used escape characters in Solidity:

CharactersOverview
\nInitializes a new line.
\\Backslash.
\’Single Quote.
\”Double Quote.
\bBackspace.
\fForm Feed.
\rDisplays Carriage.
\tTab spaces are allotted to the string.
\vVertical Tab.
\xNNA Hex value is represented and the appropriate bytes are inserted.
\uNNNNAdds UTF-8 sequence and represents Unicode value.

Bytes to String Conversion

The string() constructor can be used to convert Bytes to Strings.

Example: 

// SPDX-License-Identifier: GPL-3.0pragma solidity ^0.8.0;contract SolidityTest{function get_String() public pure returns(string memory){bytes memory my_Bytes = new bytes(12); string memory message = string(my_Bytes); return message; }}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Example: 

// SPDX-License-Identifier: GPL-3.0pragma solidity ^0.8.0;contract SolidityTest{function get_Integer_to_String() public pure returns(uint){ uint mrx=4; uint ample=6;uint sum=mrx+ample;return sum;}function get_String() public pure returns(string memory){bytes memory my_Bytes = new bytes(20);string memory message = string(my_Bytes); return message; }function get_Length() public pure returns (uint256) { string memory s="Mr.Examples";bytes memory b = bytes(s); return b.length; }}
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Example: 

// SPDX-License-Identifier: GPL-3.0pragma solidity ^0.8.0;contract SolidityTest { uint myNumber = 31211;function convertToString() public view returns (string memory) { return uintToString(myNumber); }function uintToString(uint mrx) internal pure returns (string memory) { if (mrx == 0) { return "0"; } uint ample = mrx; uint length; while (ample != 0) { length++; ample /= 10; } bytes memory my_Byte_String = new bytes(length); uint mrx_ample = length; while (mrx != 0) { mrx_ample = mrx_ample-1; uint8 temp = uint8(48 + mrx % 10); my_Byte_String[mrx_ample] = bytes1(temp); mrx /= 10; } return string(my_Byte_String); } }
<div class="spinner-border" role="status"><span class="sr-only">Loading...</span></div>

Follow the steps in the Solidity First Application chapter to run the above program.

Conclusion

Solidity strings are an essential component of many smart contract applications, providing a versatile and powerful way to store and manipulate data on the Ethereum blockchain.

While they have some limitations, such as immutability and higher costs for memory storage, these can be worked around by using Solidity’s built-in functions and libraries for string manipulation.

As the blockchain ecosystem continues to evolve and expand, the importance of Solidity strings in smart contract development will only increase.

By understanding their properties and limitations, and how to work with them effectively, developers can create more robust and efficient blockchain applications that can benefit users around the world.

We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *