Bug 1590 – std.string.startsWith and endsWith please

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2007-10-16T19:28:00Z
Last change time
2015-06-09T01:14:19Z
Assigned to
andrei
Creator
wbaxter

Comments

Comment #0 by wbaxter — 2007-10-16T19:28:41Z
These would be very nice to have in Phobos' std.string. Java's strings have them (startsWith, endsWith) Python's strings have them (startswith, endswith) Ruby strings have them (starts_with, ends_with) Perl maybe doesn't have it but perl has ~=/^xxx/ and ~=/xxx$/. Don't let D be the only one without! They're simple functions but oft-used.
Comment #1 by andrei — 2007-10-19T13:07:23Z
The functions hasPrefix(E1[] longer, E2[] shorter) and hasSuffix(E1[] longer, E2[] shorter) will be part of the next minor release and work with all sensible combinations of E1 and E2.
Comment #2 by andrei — 2007-10-19T14:52:43Z
The best approach would be to define std.algorithm and put the two functions in there. As for terminology, google indeed reveals that startsWith and endsWith are much more popular, so I'll commit to these names.
Comment #3 by wbaxter — 2007-10-19T14:58:53Z
Just for the record, Andrei's reply above was a response to this NG post: """ Sounds good, but what module? Doesn't seem to have much reason to be in std.string if it works with any types. Perhaps it's time std got an array utils module with generic array manipulation functions? (A la cashew.utils.array). You know with "advanced" functionality like removing an item from an array, etc. And if it's not going into std.string, I think there's a case to be made that std.string should still have a startsWith and endsWith (that just alias hasPrefix/hasSuffix) because std.string is where people will look for it and those are the names people will expect. """
Comment #4 by wbaxter — 2007-10-19T15:06:06Z
(In reply to comment #2) > The best approach would be to define std.algorithm and put the two functions in > there. As for terminology, google indeed reveals that startsWith and endsWith > are much more popular, so I'll commit to these names. Seems reasonable. Things in there will only work on objects that support the T[] random-access interface for now (and probably only actual D arrays?) but can be expanded later to include iterators if Phobos ever defines a standard for those.