Strings & Bytes

Strings
The important point here is string arrays are static and push and pop are not applicable however there is away to make this possible using bytes
In Solidity, a string is a dynamically sized array of UTF-8 encoded characters, represented by the
stringtype. Unlike fixed-size types likeuintorbytes32, the size of a string can be changed dynamically during runtime.You can declare a string variable like this:
string myString;
- You can also initialize a string variable with a string literal, like this:
string myString = "Hello, world!";
- You can access individual characters in a string using array indexing, like this:
char myChar = myString[0];
In this example, we access the first character of
myStringand assign it to themyCharvariable.One thing to note is that strings are stored in memory by default, which means that they are more expensive to use in terms of gas costs than other types like
uintorbool. To save on gas costs, you can use thebytestype instead ofstringin cases where you need to store a variable-length sequence of bytes.
To convert a string to a bytes array in Solidity, you can use the bytes() function, like this:
string myString = "Hello, world!";
bytes memory myBytes = bytes(myString);
In this example, we create a bytes array called myBytes that contains the UTF-8 encoded bytes of the myString variable.
Bytes
In Solidity, you can create dynamic arrays of any type using the
newkeyword. However, the one exception to this is thebytestype.The reason for this is that
bytesis a special type in Solidity that represents an array of bytes with a fixed length. Unlike other types likeuintorstring,bytesis not a dynamically sized type.When you declare a
bytesvariable, you need to specify the number of bytes that the array will hold, like this:bytes32 myBytes;In this example,
myBytesis abytesvariable with a length of 32 bytes.Because
bytesis a fixed-size type, you cannot create dynamic arrays ofbytesusing thenewkeyword like you can with other types.Instead, if you need a dynamically sized byte array, you can use the
bytesorbyte[]type instead, which are similar tostringin that they are dynamically sized arrays of bytes.
For example, if you need a dynamically sized byte array, you can declare it like this:
bytes myDynamicBytes;
In this example, myDynamicBytes is a dynamically sized byte array that can hold any number of bytes. You can resize this array as needed using the push and pop methods
Curios Case Of Byte array
- When you create a
bytesvariable in Solidity, you can initialize it using a string literal like this:
bytes memory jeevan = bytes("hello");
In this example,
jeevanis abytesvariable that is initialized with the value "hello". Note that thememorykeyword is used to specify that the variable is stored in memory.However, although
jeevanis a dynamic array, you cannot use thepush()method to add new elements to it. This is becausebytesarrays are fixed-size arrays of bytes, and their size cannot be changed once they are declared.To modify the value of
jeevan, you need to use array indexing to access and modify individual bytes in the array:
jeevan[0] = "H";
- In this example, we use array indexing to change the first byte in
jeevanfrom "h" to "H". However, we cannot add new bytes tojeevanusing thepush()method or any other similar method.




