[solved] Filter filename string?
Totte
Posts: 13,971
I just checking if there is an API somewhere in the scripting to filter a string so it wont contain non file system savvy characters, or if that is something I need to do myself.
Just that it's often faster and better to call an API (which I tried to locate) than doing things in scripts, even though RegExp would probbaly be fast.
Post edited by Totte on
Comments
Well, there isn't a QString method to replace multiple characters, nor is there a Trim method. You COULD use multiple QString::replace() calls to remove each from the string, from a list of 'invalid' characters. You could also do a 'split/join' pairing on those invalid characters to do the same. The replace method does have a RegExp version you can apply, and so does the split method.
Not sure how much of that is exposed in the scripting engine, though.
Rolling your own should be pretty simple though.....simply go through, character-by-character, and don't allow anything that isn't valid printable ASCII. A little slow, but as long as the strings aren't bigger than 1KB in length, it should still be plenty quick for a script.
Wasn't a question of simplicity, more of the fact the the philosophy would always be "If it is in an API, use it"
This was the result:
function filename_fixify(sFileName) {
var sOut="";
for (var index = 0; index < sFileName.length; index ++ ) {
var cChar = sFileName.charAt(index);
if ( cChar.match(new RegExp("^[a-zA-Z0-9_ ]*$")) != null ) {
sOut += cChar;
}
}
return sOut;
}