Skip to content
Permalink
Browse files

Add trim function

  • Loading branch information
nyalldawson committed May 12, 2013
1 parent ff92b02 commit bedd99e68478d6a9b252b895d7ad422c88558aea
Showing with 25 additions and 1 deletion.
  1. +13 −0 resources/function_help/trim-en_US
  2. +10 −1 src/core/qgsexpression.cpp
  3. +2 −0 tests/src/core/testqgsexpression.cpp
@@ -0,0 +1,13 @@
<h3>trim() function</h3>
Removes all leading and trailing whitespace (spaces, tabs, etc) from a string.

<p><h4>Syntax</h4>
trim(<i>string</i>)</p>

<p><h4>Arguments</h4>
<!-- List args for functions here-->
<i> string</i> &rarr; is string. The string to trim.</p>

<p><h4>Example</h4>
<!-- Show example of function.-->
trim(' hello world ') &rarr; 'hello world'</p>
@@ -536,6 +536,13 @@ static QVariant fcnTitle( const QVariantList& values, QgsFeature* , QgsExpressio
}
return QVariant( elems.join( " " ) );
}

static QVariant fcnTrim( const QVariantList& values, QgsFeature* , QgsExpression* parent )
{
QString str = getStringValue( values.at( 0 ), parent );
return QVariant( str.trimmed() );
}

static QVariant fcnLength( const QVariantList& values, QgsFeature* , QgsExpression* parent )
{
QString str = getStringValue( values.at( 0 ), parent );
@@ -1316,7 +1323,8 @@ const QStringList &QgsExpression::BuiltinFunctions()
<< "coalesce" << "regexp_match" << "$now" << "age" << "year"
<< "month" << "week" << "day" << "hour"
<< "minute" << "second" << "lower" << "upper"
<< "title" << "length" << "replace" << "regexp_replace" << "regexp_substr"
<< "title" << "length" << "replace" << "trim"
<< "regexp_replace" << "regexp_substr"
<< "substr" << "concat" << "strpos" << "left"
<< "right" << "rpad" << "lpad"
<< "format_number" << "format_date"
@@ -1379,6 +1387,7 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
<< new StaticFunction( "lower", 1, fcnLower, QObject::tr( "String" ) )
<< new StaticFunction( "upper", 1, fcnUpper, QObject::tr( "String" ) )
<< new StaticFunction( "title", 1, fcnTitle, QObject::tr( "String" ) )
<< new StaticFunction( "trim", 1, fcnTrim, QObject::tr( "String" ) )
<< new StaticFunction( "length", 1, fcnLength, QObject::tr( "String" ) )
<< new StaticFunction( "replace", 3, fcnReplace, QObject::tr( "String" ) )
<< new StaticFunction( "regexp_replace", 3, fcnRegexpReplace, QObject::tr( "String" ) )
@@ -296,6 +296,8 @@ class TestQgsExpression: public QObject
QTest::newRow( "lpad" ) << "lpad('Hello', 10, 'x')" << false << QVariant( "Helloxxxxx" );
QTest::newRow( "lpad truncate" ) << "rpad('Hello', 4, 'x')" << false << QVariant( "Hell" );
QTest::newRow( "title" ) << "title(' HeLlO WORLD ')" << false << QVariant( " Hello World " );
QTest::newRow( "trim" ) << "trim(' Test String ')" << false << QVariant( "Test String" );
QTest::newRow( "trim empty string" ) << "trim('')" << false << QVariant( "" );
QTest::newRow( "format" ) << "format('%1 %2 %3 %1', 'One', 'Two', 'Three')" << false << QVariant( "One Two Three One" );

// implicit conversions

0 comments on commit bedd99e

Please sign in to comment.
You can’t perform that action at this time.