Bytes vs Strings in Solidity
Strings are values that are made up of ordered sequences of characters, such as "hello world!" A string can contain any sequences of characters visible or invisible, and characters may be repeated. Solidity supports string literal using both double quote (" ") and single quote (' ').
- 'hello' - is a string;
- "hello" - is a string;
- "37" - is a string;
- 37 - is not a string, it is an integer.
How we actually write a string in #solidity ? We just type in string (which is a datatype in solidity), then give it a name (for example 'greetings") and we can set what we want inside the string - we bring in our quotations and type in what we want the string to be (for example 'Hello!').
- string - is the datatype;
- greetings - is the name of the string;
- 'Hello!' - is the string literal.
Now that we have this 'hello' string, what can we actually do with in in our contract? Why don't we create a function that returns our string? Let's create a function named sayHello that will return a string. It is important to know that you can not just return a string in solidity because it has to go somewhere, a string has to be stored. In order to store it, we want to let solidity know to store our string to memory.
Memory is much like RAM. Memory in Solidity is a temporary place to store data whereas storage holds data between functions. The Solidity Smart Contract can use any amount of memory during execution, but once the execution stops, the memory is completely wiped off for the next execution.
So in our function we want to return our greetings, our string value.
It is working :) The key takeaway here is that we've created a string called greetings and then we initialized it to 'Hello!'. Next, we have a function that returns this 'Hello!'.
Now, what if we want to change our greetings, make it something else? Well, we could create another function that essentially is going to take an argument (an input) where we can actually change what the greetings is and then return the new greetings.
Let's name the new function called changeGreeting and give it an argument of _change (string type and and add it to memory), make it public without returning anything. Set the greetings from our first function to the input to our _change from this function.
Now, let's deploy the contract. We can see that we have our two functions: sayHello and changeGreeting . If I click sayHello we will get string: Hello!
领英推è
If we want to change the greetings to Romanian, we will type 'Buna ziua!' and click the button changeGreeting first and the button sayHello again:
It is working :)
Well, sometimes we want to manipulate the strings or we want to get access to their characters, figure out what's going on in these strings. So, how do we do this in solidity? It's a little bit more tricky in solidity to do this than it is in other languages. For example in JavaScript we can use return greetings.length and it will work - we will receive the result 6 (there a six characters in the string 'Hello!'). Length is a method which can get us the length of our string (greetings). In solidity it is not possible to do this.
Why it is not working? This doesn't work because solidity doesn't want it to work - it would be very expensive for solidity to hash this out. Strings are too expensive computationally to get length in solidity so you can't do it like other languages. In solidity we should convert strings into bytes. This is very important concept in solidity, because working with bytes in general saves computational expenses as opposed to working with strings. Thinking about bytes and strings together is important to consider.
So, how would we convert our 'Hello!' into bytes? There is another datatype called bytes. Bytes is like strings only instead of registering characters in string format, it's going to go to the basic byte format. Bytes are the basic unit of measurement in computer processing. Computers are speaking in bytes, they understanding everything in bytes.
Let's convert our string to bytes by writing in our contract the datatype bytes and then create a name stringToBytes and what this is going to equal is our conversion. We can do this with bytes and then what do we want to be converting - greetings. Also important to note that bytes need to be placed in storage as well, so we will use memory.
And now, in our function, what will we return? We can use leverage the length of greetings, but instead of grabbing it from greetings, we can grab the converted version in stringToBytes.
Now, let's deploy the contract!
This is essentially the key important things to consider when thinking about strings.
See you in the next articles about #blockchain , #remix , #solidity , #crypto !