Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to concatenate strings in solidity

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Solidity how to concatenate strings, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

How to concatenate strings in solidity?

As you may have tried, the following code tries to concatenate two strings using additive operators, but this does not work:

Var str = 'asdf'var b = str +' sdf'

In fact, according to solidity's official documentation, currently in solidity, we need to implement string concatenation on our own.

Implementation code for stitching strings

For example, the following code implements the concatenation of two strings, basically using the type conversion between bytes and string to achieve string concatenation:

Contract EzDemo {function strConcat (string _ a, string _ b) internal returns (string) {bytes memory _ ba = bytes (_ a); bytes memory _ bb = bytes (_ b); string memory ret = new string (_ ba.length + _ bb.length + _ bc.length + _ bd.length + _ be.length); bytes memory bret = bytes (ret); uint k = 0; for (uint I = 0; I)

< _ba.length; i++)bret[k++] = _ba[i]; for (i = 0; i < _bb.length; i++) bret[k++] = _bb[i]; return string(ret); } } 上面的代码很容易扩展到拼接多个字符串。

Use third-party libraries

But fortunately, someone has written a library that can save us some effort:

Import "github.com/Arachnid/solidity-stringutils/strings.sol"; contract C {using strings for *; string public s; function foo (string S1, string S2) {s = s1.toSlice (). Concat (s2.toSlice ());}} after reading the above, have you mastered how to concatenate strings in solidity? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report