groovy-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From MG <mg...@arscreat.com>
Subject Re: More Default String Methods
Date Sun, 14 Oct 2018 20:11:32 GMT


On 14.10.2018 11:04, Jochen Theodorou wrote:
> On 12.10.2018 21:22, adithyank wrote:
>> As suggested by Jochen Theodorou in this
>> link
>> <http://groovy.329449.n5.nabble.com/New-DSLs-in-the-groovy-platform-itself-for-more-script-like-use-cases-td5750522.html>

>>
>> post, I have created this topic for `More Default String Methods`
>>
>> I would like to work in these if the community accepts this proposal
>>
>> //Method to return left n characters
>> *1. static String left(String self, int chars)*
>
> self[0..chars]?

Having a method here might be useful, but I would call it "fromLeft .
"left" methd to me would be "all the chars left of the given char 
position", which would also be good to have, imho.

>
>> //Method to return right n characters
>> *2. static String right(String self, int chars)*
>
> self[-chars..-1]?

Same as "left" above (with the addition that "-chars-1" is easier to get 
wrong ;-) ).


>
>> //Method to return the String that is after the first occurrence of the
>> given searchString
>> *3. static String after(String self, String searchString)*
>>
>> //Method to return the String that is before the first occurrence of the
>> given searchString
>> *4. static String before(String self, String searchString)*
>
> these sound more interesting... empty String if the search string is 
> not in self?

Asking mysself, how often does anyone need that, and if one does need 
it, would he rememeber/find it with that name ?
Along the same line: Use a regex for special cases like that ?

>
>> //Method to return the String that is before the first '=' character
>> *5. static String getBeforeEq(String self)*
>
> which is foo.before("=") if number 4 is taken... This method looks to 
> me a bit overspecialized

I think it is overspecialized in any case. Better to use a class that 
parses e.g. an properties file for you, or go for a full blown parser here.

>
>> /**
>> Splits the given String with the given separator String and returns the
>> value at the given column Index. By
>> default, successive occurrence of the separator String is taken are 
>> one unit
>> and the split is performed
>> */
>> *6. static String column(String self, String separator, int colIndex)*
>>
>> /**
>> Splits the given String with Space characters and returns the value 
>> at the
>> given column Index.
>> */
>> *7. static String column(String self, int colIndex)*
>
> self.split(speparator)[colIndex]?

Also quite a special method, but depending on implementation, 
performance of "column" method woud be much better.

>
>> //Method to return the String that is between the pair of double quotes
>> *8. static String bwDoubleQuotes(String line)*
>>
>> //Method to return the String that is between the pair of single quotes
>> *9. static String bwSingleQuotes(String line)*
>
> again these look a bit overspecialized to me

unquote(String s, String quoteString = '"') ?

>
>> //Method to return the String that is between the pair of given 
>> enclosure
>> String
>> *10. static String bw(String line, String enclosure)*
>>
>> //Method to return the String that is between the 2 given strings
>> *11. static String bw(String line, String from, String to)*

Is this for parsing, or is it more:
strip(String s, String bracketString) ?
trim(String s, String trimChars) ?

"bw" is way too short / non-obvious a method name in my book.

>>
>>
>> //Method to return the String that is between the nth pair of double 
>> quotes
>> *12. static String bwDoubleQuotes(String line, int index)*
>>
>>
>> //Method to return the String that is between the nth pair of single 
>> quotes
>> *13. static String bwSingleQuotes(String line, int index)*
>
> so 10 is 11 with from=to, 12 is 10 if enclosure " and 13 is 10 with 
> enclosure '. so don`t know about 12 and 13
>
>> //Method to return the String that is between the nth pair of given
>> enclosure String
>> *14. static String bw(String line, String enclosure, int index)*
>
> this could be interesting to have

Too special again, imho.

>
>> //Method to check whether searchString exists within self String 
>> ignoring
>> the case
>> *15. static boolean containsIgnoreCase(String self, String 
>> searchString)*
>>
>> //Method to check whether the String starts with the searchString 
>> ignoring
>> the case
>> *16. static boolean startsWithIgnoreCase(String self, String 
>> searchString)*
>>
>> //Method to check whether the String ends with the searchString 
>> ignoring the
>> case
>> *17. static boolean endsWithIgnoreCase(String self, String 
>> searchString)*
>
> these could be interesting because they are actually not so easy to 
> get right and efficient in a unicode world

Agree.

Cheers,
mg






Mime
View raw message