Request: String class additional manipulations


#1

Hi Jules!

Could you, please, add two kinds of member functions to the String class.

  1. some kind of indexAnyOf(const tchar* collection) to find first occurence of any symbol from the collection. Like std::string::find_first_of(…) STL function. And, sure, the reverse one like lastIndexAnyOf(…).

  2. special trim… function that accepts a characters collection to remove from string edges. For instance, the string.trim(")([]") call must remove any of )([] occurences from the end and start of a string. Sure, trimStart and trimEnd must exist too.

Could you, please please plizzzzzzzz :slight_smile:


#2

Yep, those are good requests, and easy enough to do - thanks!


#3

Thank you, Jules, very much! You’re the best!


#4

Jules, would it be possible to make a type of convert function which returns a char*? May come in handy.


#5

you can already do that by casting it to a (const char*)… i think that converting it to a non const is impossible since the cast operator just create a temporary copy of the string (in the case it is stored in utf8, so as tchar*)…


#6

Hi Jules, again.

Could you add additional functions like indexNotAnyOf(…) (like std::string::find_first_not_of(…)) too to facilitate skipping to some relevant positions. (e.g. skipping(not removing) white spaces and tab characters from the beginning/end of the string to the first alphanumerical character).

Could you? :slight_smile:


#7

There is dropLastCharacters, but not dropFirstCharacters, which would be nice.


#8

All good suggestions!


#9

Hi Julian!

me again :slight_smile: You have two overloaded versions of ‘String::substring’ method. Why not to merge them? If we pass the second parameter as ‘-1’ then it can return a substring from the first parameter position to the end of the string. You can pass the second parameter as a search result which can return ‘-1’ on failure and thus retrieving the substring till the end of the string.

for now, we have to check it like this:

if(searchEnd == -1) str = str.substring(startPos); else str = str.substring(startPos, searchEnd);
but then we can write like this whether searchEnd result is ‘-1’ or not:

If I want another way to handle ‘-1’ result, so I do a check. But my proposed usage is more suitable and frequent in use by programmers than that what’s used for now.

For instance, to extract a filename without extension from a path we can write (despite whether the file has an extension or has not):

String fileName = path.substring(path.lastIndexAnyOf(T("\\/")), path.lastIndexOfChar(T('.')));


#10

Not a bad idea, but that’d involve a change to the way the method works, which could silently break existing code, and that’d create some nasty surprises for people…


#11

[quote]which could silently break existing code, and that’d create some nasty surprises for people
[/quote]
…that’s what docs and changelogs are existing for. Even better, the default behaviour of the String::substring(int, int =- 1) would be the same as String::substring(int) now.


#12

Hmm. I do agree that’d be a better way for the method to work, but I always prefer to avoid changes that could easily go undetected. It’s very easy to miss something like this in the changelog.


#13

ok. I think you’ll do the right thing.

the other proposal is to introduce the new function that unecapes (and escapes) characters like the one I’ve posted there http://www.rawmaterialsoftware.com/juceforum/viewtopic.php?t=1345


#14

Good suggestion. (I’ve already got some code for that in the URL class).


#15

I guess the trim(“chars”) were lost in the noise.

When you only have one char to trim, upToLastOccurrence works, but not when you have a lot of different char to trim.


#16

Yes, I’d forgotten that request. As it’s just a 5-minute job, I’ll add it now before I forget again…


#17

I would like to see a reversed String method, I can't seem to find one?.. something like String reversed (String& strToReverse);

I could always use my own method but it would be convenient to call it from the string itself..


#18

I think the only place where I've ever actually seen a reverse-string function used is in interview questions! Don't remember ever needing one in my own code.

I've no objection to adding one, but would be curious to hear what you need it for?


#19

it's nice to have when trying to learn how to speak backwards fluently :)

 

just kidding, I see your point about it might not be that useful to everyone ;) I use it in a part of a (weak) obfuscation method, somehow I just expected to find it there since there's so many manipulation methods in there already. Don't dont waste time on it if you think it's becoming a "one hit wonder" :)


#20