Request: String class additional manipulations

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:

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

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

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

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*)…

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:

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

All good suggestions!

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('.')));

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…

[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.

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.

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

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

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.

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

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..

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?

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" :)