-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.sol
47 lines (38 loc) · 1.26 KB
/
memory.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//SPDX-License-Identifier:MIT
pragma solidity ^0.4.0;
library String{
function concat(string _base, string _value) public view returns(string){
bytes memory _baseByte = bytes(_base);
bytes memory _baseValue = bytes(_value);
string memory _tmpValue = new string(_baseByte.length+_baseValue.length);
bytes memory _newValue =bytes(_tmpValue);
uint i;
uint j;
for(i=0;i<_baseByte.length;i++){
_newValue[j++]=_baseByte[i];
}
for (i=0;i<_baseValue.length;i++){
_newValue[j++]= _baseValue[i];
}
return string (_newValue);
}
function strpos(string _base,string _value) public pure returns(int){
bytes memory _posbaseByte = bytes(_base);
bytes memory _posbaseValue = bytes(_value);
for (uint i=0; i<_posbaseByte.length;i++){
if (_posbaseByte[i]==_posbaseValue[0]){
return int(i);
}
}
return -1;
}
}
contract TestString{
using String for string;
function testComntract(string _base,string _value) public view returns(string){
return _base.concat(_value);
}
function testposition(string _base)public view returns(int){
return _base.strpos("m");
}
}