Using Oraclize, I request a some external data in the form "1 2 3", i.e., space-separated integers (OR any other delimiter).

Indeed, these numbers could be requested one at a time, but this would be EXPENSIVE.

Now, to work with these integers, I need to convert them back to integers and store them inside an array.

I know the parseInt() function for converting a string to a number, but how to I break up the string into "1", "2", "3", such that I can parse the numbers and store them in an array?

  • 1
    @AchalaDissanayake and other editors: I reverted the edit made in revision 2 back to OP's original wording. OP is performing a conversion of integers to strings, not a split of strings to array of strings. Also, the edit mentions an "array with a delimiter" in simplest parsing. That doesn't even make sense! Lastly, editor's title turned string into "String" (uppercase) which is inappropriate in both interpretations of the word; the type in Solidity is string and the computer-y meaning is also simply just another English word. OP's title was fine as-is. Edit adds three mistakes.
    – lungj
    Oct 8, 2017 at 15:48
  • The function was generalized to use it with any separator not only with spaces. Oct 8, 2017 at 17:02

2 Answers 2


You may use a library like stringutils or solidity-util to for that. For your special case, casting the string to bytes and manipulating like the following function will work.

contract MyContract{

    bytes tempNum ; //temporarily hold the string part until a space is recieved
    uint[] numbers;

    function splitStr(string str, string delimiter) constant returns (int []){ //delimiter can be any character that separates the integers 

        bytes memory b = bytes(str); //cast the string to bytes to iterate
        bytes memory delm = bytes(delimiter); 

        for(uint i; i<b.length ; i++){          

            if(b[i] != delm[0]) { //check if a not space
            else { 
                numbers.push(parseInt(string(tempNum))); //push the int value converted from string to numbers array
                tempNum = "";   //reset the tempNum to catch the net number                 

        if(b[b.length-1] != delm[0]) { 
        return numbers;

If you can't use parseInt you may use your own function to convert to uint as follow found here,

function bytesToUint(bytes b) constant returns (uint result) {

    result = 0;
    for (uint i = 0; i < b.length; i++) { 
        if (b[i] >= 48 && b[i] <= 57) {
            result = result * 10 + (uint(b[i]) - 48); 
    return result;

Then instead of numbers.push(parseInt(string(tempNum))); you can use numbers.push(bytesToUint(tempNum));

Or you can look into the libraries code and use only the function you need. Those might be efficient.

Hope this helps!

  • Thanks! Do you know of this library is already deployed on Rinkeby testnet? Can you describe what your code does? Is 0x20 used as a separator? What if a real byte takes this value?
    – Shuzheng
    Oct 5, 2017 at 17:42
  • 0X20 is the hex value of 32 which is the char value of space , you can change the delimeter by changing that value, for your case since it's spsce I used 0x20 Oct 5, 2017 at 17:44
  • Yes, Thank you! It is so nice, just what I needed.
    – Shuzheng
    Oct 5, 2017 at 18:24
  • Your code has some minor errors, like the temp.push().
    – Shuzheng
    Oct 5, 2017 at 18:25
  • 1
    Comments are not for extended discussion; this conversation has been moved to chat.
    – Badr Bellaj
    Oct 7, 2017 at 12:10

You can also use solidity-util, a relatively new library:

string[] storage split = str.split(" ");

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.