String conversion to Array in Solidity

0 votes

In Solidity, is there a way I can convert my string text to an array using a separator to identify the composite parts within the string

Example

mystring = "This-Is-A-Problem";

to

myArray = [This,Is,A,Problem];   // using hyphen as separator
Sep 25, 2018 in Blockchain by slayer
• 29,170 points
256 views

3 answers to this question.

0 votes

For n parts we have n-1 delimiters. The code will be like this:

import "github.com/Arachnid/solidity-stringutils/strings.sol";

contract Contract {
   using strings for *;

   // ...

   function smt() {
    var s = ""This-Is-A-Problem"".toSlice();
    var delim = "-".toSlice();
    var parts = new string[](s.count(delim) + 1);

    for(uint i = 0; i < parts.length; i++) {
       parts[i] = s.split(delim).toString();
    }
   }
}
answered Sep 25, 2018 by digger
• 26,550 points
0 votes
import "github.com/Arachnid/solidity-stringutils/strings.sol";

contract Contract {                                                            
    using strings for *;                                                       

    function smt() public pure {                                               
        strings.slice memory s = "This-Is-A-Problem".toSlice();                
        strings.slice memory delim = "-".toSlice();                            
        string[] memory parts = new string[](s.count(delim));                  
        for (uint i = 0; i < parts.length; i++) {                              
           parts[i] = s.split(delim).toString();                               
        }                                                                      
    }                                                                          
}  

https://ethfiddle.com/TgY5JxLKvn

answered Oct 3, 2018 by Hari das
0 votes

There is no built-in method/function for this but you can use solidity-stringutils. then

import "github.com/Arachnid/solidity-stringutils/strings.sol";

contract Contract {
    using strings for *;

    // ...

    function smt() {
        var s = ""This-Is-A-Problem"".toSlice();
        var delim = "-".toSlice();
        var parts = new string[](s.count(delim));
        for(uint i = 0; i < parts.length; i++) {
           parts[i] = s.split(delim).toString();
        }
    }
}
answered Oct 3, 2018 by Charlie

Related Questions In Blockchain

+1 vote
2 answers

How to convert INT to STRING in Solidity?

Look at the following code : function uintToString(uint ...READ MORE

answered Jun 27, 2018 in Blockchain by Christine
• 15,790 points
1,154 views
0 votes
1 answer

How to convert byte array to hex string?

You are missing the padding in the ...READ MORE

answered Aug 17, 2018 in Blockchain by slayer
• 29,170 points
60 views
0 votes
1 answer

How to handle Overlapping of array objects in node js

Try this code, may be it works for ...READ MORE

answered Sep 18, 2018 in Blockchain by digger
• 26,550 points
38 views
0 votes
1 answer

How to Check if string is in list in javascript?

At first, make sure that the string ...READ MORE

answered Sep 19, 2018 in Blockchain by slayer
• 29,170 points
53 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
169 views
+1 vote
3 answers

Removing double quotes from a string from JSON response in PHP

Just remove the json_encode call, and it should work: $resp ...READ MORE

answered Sep 12, 2018 in Blockchain by digger
• 26,550 points
4,248 views
0 votes
1 answer
0 votes
1 answer

How to get all address and send ethers in solidity using a loop?

I found a similar code somewhere: contract  Holders{ uint ...READ MORE

answered Jul 31, 2018 in Blockchain by digger
• 26,550 points
232 views
0 votes
1 answer

What encoding or data type can be used to get alphanumeric string in elixir?

Integers in Elixir are arbitrary precision integers, ...READ MORE

answered Aug 31, 2018 in Blockchain by digger
• 26,550 points
51 views