Skip to content
Permalink
Browse files
Add string format() function to expression
  • Loading branch information
NathanW2 committed Jan 24, 2013
1 parent f2029b4 commit 3c29cd25a041a4d826894a9e921e950769d6f0dd
Showing with 18 additions and 0 deletions.
  1. +18 −0 src/core/qgsexpression.cpp
@@ -569,6 +569,23 @@ static QVariant fcnLPad( const QVariantList& values, QgsFeature* , QgsExpression
return string.leftJustified( length, fill.at( 0 ), true );
}

static QVariant fcnFormatString( const QVariantList& values, QgsFeature* , QgsExpression *parent )
{
QString string = getStringValue( values.at( 0 ), parent );
for ( int n = 1; n <= values.length() - 1; n++ )
{
QString arg = getStringValue( values.at( n ), parent );
QString place = '%' + QString::number( n );
if ( string.indexOf( place ) == -1 )
{
continue;
}
string.replace( place, arg );
}
return string;
}


static QVariant fcnNow( const QVariantList&, QgsFeature* , QgsExpression * )
{
return QVariant( QDateTime::currentDateTime() );
@@ -1100,6 +1117,7 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
<< new StaticFunction( "right", 2, fcnRight, QObject::tr( "String" ) )
<< new StaticFunction( "rpad", 3, fcnRPad, QObject::tr( "String" ) )
<< new StaticFunction( "lpad", 3, fcnLPad, QObject::tr( "String" ) )
<< new StaticFunction( "format", -1, fcnFormatString, QObject::tr( "String" ) )
<< new StaticFunction( "format_number", 2, fcnFormatNumber, QObject::tr( "String" ) )
<< new StaticFunction( "format_date", 2, fcnFormatDate, QObject::tr( "String" ) )
<< new StaticFunction( "xat", 1, fcnXat, QObject::tr( "Geometry" ), "", true )

1 comment on commit 3c29cd2

@dakcarto

This comment has been minimized.

Copy link
Member

@dakcarto dakcarto commented on 3c29cd2 Jan 24, 2013

Very handy and 'QString::arg()'-like. Thanks Nathan!

Please sign in to comment.