Thread overview | |||||||
---|---|---|---|---|---|---|---|
|
February 27, 2015 Getting what came *before* the results of a find call | ||||
---|---|---|---|---|
| ||||
What's the idiomatic way of getting everything *before* the results of a call to find? assert("hello world".find(" world").what_goes_here??? == "hello"); In an article Andrei wrote a few years ago (http://www.informit.com/articles/article.aspx…) he mentioned a function like this with the name "until", but I can't find it or its replacement in Phobos. |
February 27, 2015 Re: Getting what came *before* the results of a find call | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mark Isaacson | Not that it's super important, but the link didn't copy well, so here's that: http://www.informit.com/articles/article.aspx?p=1407357&seqNum=12 |
February 27, 2015 Re: Getting what came *before* the results of a find call | ||||
---|---|---|---|---|
| ||||
Posted in reply to Mark Isaacson | On 02/27/2015 02:29 PM, Mark Isaacson wrote:
> What's the idiomatic way of getting everything *before* the results of a
> call to find?
>
> assert("hello world".find(" world").what_goes_here??? == "hello");
>
> In an article Andrei wrote a few years ago
> (http://www.informit.com/articles/article.aspx…) he mentioned a function
> like this with the name "until", but I can't find it or its replacement
> in Phobos.
findSplit and friends in std.algorithm:
import std.algorithm;
void main()
{
assert("hello world".findSplit(" world")[0] == "hello");
}
Ali
|
February 27, 2015 Re: Getting what came *before* the results of a find call | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | Ahhh right - forgot about that. Thanks! |
February 27, 2015 Re: Getting what came *before* the results of a find call | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Friday, February 27, 2015 14:42:17 Ali Çehreli via Digitalmars-d-learn wrote:
> On 02/27/2015 02:29 PM, Mark Isaacson wrote:
> > What's the idiomatic way of getting everything *before* the results of a call to find?
> >
> > assert("hello world".find(" world").what_goes_here??? == "hello");
> >
> > In an article Andrei wrote a few years ago (http://www.informit.com/articles/article.aspx…) he mentioned a function like this with the name "until", but I can't find it or its replacement in Phobos.
>
> findSplit and friends in std.algorithm:
>
> import std.algorithm;
>
> void main()
> {
> assert("hello world".findSplit(" world")[0] == "hello");
> }
There's also std.algorithm.until if all you want is what's before, whereas findSplit gives you both what's before and after.
- Jonathan M Davis
|
Copyright © 1999-2021 by the D Language Foundation