Thread overview | |||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 09, 2005 .ends .starts .contains | ||||
---|---|---|---|---|
| ||||
I was just thinking, that it would be useful to have: char []S; if(S.ends(".com")){.... tha same with .starts .contains. I need this quite often, I must say. What you think? |
February 09, 2005 Re: .ends .starts .contains | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin | Martin wrote: > I was just thinking, that it would be useful to have: > > char []S; > > if(S.ends(".com")){.... > > tha same with .starts .contains. > I need this quite often, I must say. > > What you think? Sounds like functions, not properties... Such as the already existing "std" ones, that could probably be used or modified ? import std.string; > /************************************* > * Find first occurrance of sub[] in string s[]. > * Return index in s[] where it is found. > * Return -1 if not found. > */ > > int find(char[] s, char[] sub); > /************************************* > * Find last occurrance of sub in string s. > * Return index in s where it is found. > * Return -1 if not found. > */ > > int rfind(char[] s, char[] sub); Strings in D are arrays, and not classes... This mean they use functions, not methods. As opposed to, for instance this class method: http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#endsWith(java.lang.String) --anders |
February 09, 2005 Re: .ends .starts .contains | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | "Anders F Björklund" <afb@algonet.se> wrote in message news:cud27p$2ac2$1@digitaldaemon.com... > Martin wrote: > > > I was just thinking, that it would be useful to have: > > > > char []S; > > > > if(S.ends(".com")){.... > > > > tha same with .starts .contains. > > I need this quite often, I must say. > > > > What you think? > > Sounds like functions, not properties... > > Such as the already existing "std" ones, > that could probably be used or modified ? > > import std.string; > > > /************************************* > > * Find first occurrance of sub[] in string s[]. > > * Return index in s[] where it is found. > > * Return -1 if not found. > > */ > > > > int find(char[] s, char[] sub); > > > /************************************* > > * Find last occurrance of sub in string s. > > * Return index in s where it is found. > > * Return -1 if not found. > > */ > > > > int rfind(char[] s, char[] sub); > > Strings in D are arrays, and not classes... > This mean they use functions, not methods. > But in D char[] can use functions as if it were a property. So we can use int ends(char[] str1, char[] str2) like this: str1.ends(".com"); > As opposed to, for instance this class method: > http://java.sun.com/j2se/1.4.2/docs/api/java/lang/String.html#endsWith(java. lang.String) > > --anders |
February 09, 2005 Re: .ends .starts .contains | ||||
---|---|---|---|---|
| ||||
Posted in reply to Martin | As Ivan mentioned, for arrays you can use functions directly as properties. So, you can just define # bool ends(char[] s, char[] p) # { # return (p.length <= s.length) && (s[length-p.length..length] == p); # } # # bool starts(char[] s, char[] p) # { # return (p.length <= s.length) && (s[0..p.length] == p); # } # # bool contains(char[] s, char[] p) # { # return s.find(p) != -1; # } and you're done! Nick In article <cud0n0$28vh$1@digitaldaemon.com>, Martin says... > >I was just thinking, that it would be useful to have: > >char []S; > >if(S.ends(".com")){.... > >tha same with .starts .contains. >I need this quite often, I must say. > >What you think? |
February 09, 2005 Re: .ends .starts .contains | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | Ivan Senji wrote:
>>Sounds like functions, not properties...
>>
>>Strings in D are arrays, and not classes...
>>This mean they use functions, not methods.
>
> But in D char[] can use functions as if it were a property.
> So we can use int ends(char[] str1, char[] str2) like this:
> str1.ends(".com");
Yeah, that is part of the "make-believe" features. Just like bool.
Thanks for pointing it out, as it does make the code look nicer...
--anders
|
February 09, 2005 Re: .ends .starts .contains | ||||
---|---|---|---|---|
| ||||
Posted in reply to Nick | I think getting this for all types would be awesome what do ya'll think ? Charlie "Nick" <Nick_member@pathlink.com> wrote in message news:cuddbn$2nio$1@digitaldaemon.com... > As Ivan mentioned, for arrays you can use functions directly as properties. > > So, you can just define > > # bool ends(char[] s, char[] p) > # { > # return (p.length <= s.length) && (s[length-p.length..length] == p); > # } > # > # bool starts(char[] s, char[] p) > # { > # return (p.length <= s.length) && (s[0..p.length] == p); > # } > # > # bool contains(char[] s, char[] p) > # { > # return s.find(p) != -1; > # } > > and you're done! > > Nick > > In article <cud0n0$28vh$1@digitaldaemon.com>, Martin says... > > > >I was just thinking, that it would be useful to have: > > > >char []S; > > > >if(S.ends(".com")){.... > > > >tha same with .starts .contains. > >I need this quite often, I must say. > > > >What you think? > > |
February 09, 2005 Re: .ends .starts .contains | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | > But in D char[] can use functions as if it were a property.
> So we can use int ends(char[] str1, char[] str2) like this:
> str1.ends(".com");
I don't think that's part of the language spec is it? It's an accident that it works and possibly even considered a bug that Walter hasn't fixed yet.
|
February 09, 2005 Re: .ends .starts .contains | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ben Hinkle | Ben Hinkle wrote:
>>But in D char[] can use functions as if it were a property.
>>So we can use int ends(char[] str1, char[] str2) like this:
>>str1.ends(".com");
>
> I don't think that's part of the language spec is it? It's an accident that it works and possibly even considered a bug that Walter hasn't fixed yet.
Pretty sweet "bug", though? But even without the syntactic sugar,
it is: ends(str1,".com"); Still no need for any extra properties.
--anders
|
February 09, 2005 Re: .ends .starts .contains | ||||
---|---|---|---|---|
| ||||
Posted in reply to Charles | "Charles" <no@email.com> wrote in message news:cudidc$2sht$1@digitaldaemon.com... > I think getting this for all types would be awesome what do ya'll think ? It works for all array types. I had a strong feeling that it was documented by now, but can't seem to find it anywhere. And i don't think it is a bug. A long time ago i used this syntax with templates and something didn't work (don't remember what exactly, something with aliases) and Walter fixed that and didn't mention that this syntax is wrong. It would be nice if it was in the spec. > > Charlie > > "Nick" <Nick_member@pathlink.com> wrote in message news:cuddbn$2nio$1@digitaldaemon.com... > > As Ivan mentioned, for arrays you can use functions directly as > properties. > > > > So, you can just define > > > > # bool ends(char[] s, char[] p) > > # { > > # return (p.length <= s.length) && (s[length-p.length..length] == p); > > # } > > # > > # bool starts(char[] s, char[] p) > > # { > > # return (p.length <= s.length) && (s[0..p.length] == p); > > # } > > # > > # bool contains(char[] s, char[] p) > > # { > > # return s.find(p) != -1; > > # } > > > > and you're done! > > > > Nick > > > > In article <cud0n0$28vh$1@digitaldaemon.com>, Martin says... > > > > > >I was just thinking, that it would be useful to have: > > > > > >char []S; > > > > > >if(S.ends(".com")){.... > > > > > >tha same with .starts .contains. > > >I need this quite often, I must say. > > > > > >What you think? > > > > > > |
February 09, 2005 Re: .ends .starts .contains | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ivan Senji | In article <cudsrs$58o$1@digitaldaemon.com>, Ivan Senji says... > >"Charles" <no@email.com> wrote in message news:cudidc$2sht$1@digitaldaemon.com... >> I think getting this for all types would be awesome what do ya'll think ? > > >It works for all array types. I had a strong feeling that it was documented >by now, >but can't seem to find it anywhere. And i don't think it is a bug. A long >time ago >i used this syntax with templates and something didn't work (don't remember >what exactly, something with aliases) and Walter fixed that and didn't >mention >that this syntax is wrong. > >It would be nice if it was in the spec. If this becomes part of the spec, is there any good reason why it shouldn't work for other (non-array) types as well, such as ints and doubles? I can see that structs and classes would be a problem though. Nick |
Copyright © 1999-2021 by the D Language Foundation