StringArray cannot found items items in Linux

Use case

Return StringArray seems to be not find items if it’s returned from specified method:

StringArray arr = JUCEApplicationBase::getCommandLineParameterArray ();
if (arr.contains(String ("--stats", true)))  {
    // do something 

arr.contains returns always false even it’s exist in array.

However, if I create by myself StringArray it does find looked string. How to fixed this ?

Your parentheses are in the wrong place. Did that even compile??

You also don’t need the String cast (really don’t understand why it seems so common for people to wrap all their string literals in String constructors…)

if (arr.contains ("--stats", true))

With your suggestion, it does work now.

Using String cast feels naturally for some reason (May some other frameworks uses this approach … ). Maybe this need written down to FMB (Frequently made bugs section) :smiley:

Yeah, it’s not really a bug, just a bad habit that makes code less readable and occasionally makes it slower (e.g. in your code above, it would create an unnecessary intermediate string object because the function expects a StringRef). I’m just a bit puzzled because I see it really often, and don’t know why anyone would start doing it, as I’d have thought most people’s instinct would be to first just type the literal… Ah well.

It’s kind of conditioning, each time you try to concatenate two const char* using operator+
One day you stop thinking about it…

1 Like

I have some external tools which will choke on Windows if I use operator+ in a Logger::writeToLog() with string literals… so I have to make them into String objects… otherwise I don’t unless I need to use a String method on a literal.