我想从一个非常大的字符串的开头剪切一些字符。为了节省一些内存和时间,我想避免复制它,而只是让字符串从后面的索引开始,就像在 C 中通过增加指向字符串(数组)的指针很容易实现一样。
这可能吗?
最佳答案
你不能玩“增加字符串指针”的把戏,但试试这个:
str = 'I would like to cut a number of characters from the beginning of a very large String'
str[0, 10] = ''
str # => "ke to cut a number of characters from the beginning of a very large String"
这是我所知道的在 Ruby 中截断部分字符串的最简单方法。
关于ruby - 如何在不复制字符串的情况下裁剪它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5856806/