<div dir="ltr"><div>I've posted a while back a string=>string substring function that doesn't allocating: google </div>"nonallocating unicode string manipulations"<div><br></div><div>code:<br><div><br>
</div><div><div>auto slice(T)(T a,size_t u, size_t v)if(is(T==string)){//TODO:generalize to isSomeString</div><div><span class="" style="white-space:pre">      </span>import std.exception;</div><div><span class="" style="white-space:pre">      </span>auto m=a.length;</div>
<div><span class="" style="white-space:pre">    </span>size_t i;</div><div><span class="" style="white-space:pre">  </span>enforce(u<=v);</div><div><span class="" style="white-space:pre">  </span>import std.utf;</div><div><span class="" style="white-space:pre">    </span>while(u-- && i<m){</div>
<div><span class="" style="white-space:pre">            </span>auto si=stride(a,i);</div><div><span class="" style="white-space:pre">               </span>i+=si;</div><div><span class="" style="white-space:pre">             </span>v--;</div><div><span class="" style="white-space:pre">       </span>}</div>
<div><span class="" style="white-space:pre">    </span>//<span class="" style="white-space:pre">        </span>assert(u==-1);</div><div><span class="" style="white-space:pre">     </span>//<span class="" style="white-space:pre">        </span>enforce(u==-1);</div>
<div><span class="" style="white-space:pre">    </span>size_t i2=i;</div><div><span class="" style="white-space:pre">       </span>while(v-- && i2<m){</div><div><span class="" style="white-space:pre">             </span>auto si=stride(a,i2);</div>
<div><span class="" style="white-space:pre">            </span>i2+=si;</div><div><span class="" style="white-space:pre">    </span>}</div><div><span class="" style="white-space:pre">  </span>//<span class="" style="white-space:pre">        </span>assert(v==-1);</div>
<div><span class="" style="white-space:pre">    </span>enforce(v==-1);</div><div><span class="" style="white-space:pre">    </span>return a[i..i2];</div><div>}</div><div>unittest{</div><div><span class="" style="white-space:pre">   </span>import std.range;</div>
<div><span class="" style="white-space:pre">    </span>auto a="≈açç√ef";</div><div><span class="" style="white-space:pre">  </span>auto b=a.slice(2,6);</div><div><span class="" style="white-space:pre">       </span>assert(a.slice(2,6)=="çç√e");</div>
<div><span class="" style="white-space:pre">    </span>assert(a.slice(2,6).ptr==a.slice(2,3).ptr);</div><div><span class="" style="white-space:pre">        </span>assert(a.slice(0,a.walkLength) is a);</div><div><span class="" style="white-space:pre">      </span>import std.exception;</div>
<div><span class="" style="white-space:pre">    </span>assertThrown(a.slice(2,8));</div><div><span class="" style="white-space:pre">        </span>assertThrown(a.slice(2,1));</div><div>}</div></div></div></div><div class="gmail_extra">
<br><br><div class="gmail_quote">On Sat, Oct 26, 2013 at 3:17 PM, Ali Çehreli <span dir="ltr"><<a href="mailto:acehreli@yahoo.com" target="_blank">acehreli@yahoo.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5">On 10/26/2013 02:25 PM, Namespace wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
On Saturday, 26 October 2013 at 21:23:13 UTC, Gautam Goel wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Dumb Newbie Question: I've searched through the library reference, but<br>
I haven't figured out how to extract a substring from a string. I'd<br>
like something like string.substring("Hello", 0, 2) to return "Hel",<br>
for example. What method am I looking for? Thanks!<br>
</blockquote>
<br>
Use slices:<br>
<br>
string msg = "Hello";<br>
string sub = msg[0 .. 2];<br>
</blockquote>
<br></div></div>
Yes but that works only if the string is known to contain only ASCII codes. (Otherwise, a string is a collection of UTF-8 code units.)<br>
<br>
I could not find a subString() function either but it turns out to be trivial to implement with Phobos:<br>
<br>
import std.range;<br>
import std.algorithm;<br>
<br>
auto subRange(R)(R s, size_t beg, size_t end)<br>
{<br>
    return s.dropExactly(beg).take(end - beg);<br>
}<br>
<br>
unittest<br>
{<br>
    assert("abcçdef".subRange(2, 4).equal("cç"));<br>
}<br>
<br>
void main()<br>
{}<br>
<br>
That function produces a lazy range. To convert it eagerly to a string:<br>
<br>
import std.conv;<br>
<br>
string subString(string s, size_t beg, size_t end)<br>
{<br>
    return s.subRange(beg, end).text;<br>
}<br>
<br>
unittest<br>
{<br>
    assert("Hello".subString(0, 2) == "He");<br>
}<span class="HOEnZb"><font color="#888888"><br>
<br>
Ali<br>
<br>
</font></span></blockquote></div><br></div>