Skip to content

Commit bedd99e

Browse files
committed
Add trim function
1 parent ff92b02 commit bedd99e

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

resources/function_help/trim-en_US

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<h3>trim() function</h3>
2+
Removes all leading and trailing whitespace (spaces, tabs, etc) from a string.
3+
4+
<p><h4>Syntax</h4>
5+
trim(<i>string</i>)</p>
6+
7+
<p><h4>Arguments</h4>
8+
<!-- List args for functions here-->
9+
<i> string</i> &rarr; is string. The string to trim.</p>
10+
11+
<p><h4>Example</h4>
12+
<!-- Show example of function.-->
13+
trim(' hello world ') &rarr; 'hello world'</p>

src/core/qgsexpression.cpp

+10-1
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,13 @@ static QVariant fcnTitle( const QVariantList& values, QgsFeature* , QgsExpressio
536536
}
537537
return QVariant( elems.join( " " ) );
538538
}
539+
540+
static QVariant fcnTrim( const QVariantList& values, QgsFeature* , QgsExpression* parent )
541+
{
542+
QString str = getStringValue( values.at( 0 ), parent );
543+
return QVariant( str.trimmed() );
544+
}
545+
539546
static QVariant fcnLength( const QVariantList& values, QgsFeature* , QgsExpression* parent )
540547
{
541548
QString str = getStringValue( values.at( 0 ), parent );
@@ -1316,7 +1323,8 @@ const QStringList &QgsExpression::BuiltinFunctions()
13161323
<< "coalesce" << "regexp_match" << "$now" << "age" << "year"
13171324
<< "month" << "week" << "day" << "hour"
13181325
<< "minute" << "second" << "lower" << "upper"
1319-
<< "title" << "length" << "replace" << "regexp_replace" << "regexp_substr"
1326+
<< "title" << "length" << "replace" << "trim"
1327+
<< "regexp_replace" << "regexp_substr"
13201328
<< "substr" << "concat" << "strpos" << "left"
13211329
<< "right" << "rpad" << "lpad"
13221330
<< "format_number" << "format_date"
@@ -1379,6 +1387,7 @@ const QList<QgsExpression::Function*> &QgsExpression::Functions()
13791387
<< new StaticFunction( "lower", 1, fcnLower, QObject::tr( "String" ) )
13801388
<< new StaticFunction( "upper", 1, fcnUpper, QObject::tr( "String" ) )
13811389
<< new StaticFunction( "title", 1, fcnTitle, QObject::tr( "String" ) )
1390+
<< new StaticFunction( "trim", 1, fcnTrim, QObject::tr( "String" ) )
13821391
<< new StaticFunction( "length", 1, fcnLength, QObject::tr( "String" ) )
13831392
<< new StaticFunction( "replace", 3, fcnReplace, QObject::tr( "String" ) )
13841393
<< new StaticFunction( "regexp_replace", 3, fcnRegexpReplace, QObject::tr( "String" ) )

tests/src/core/testqgsexpression.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,8 @@ class TestQgsExpression: public QObject
296296
QTest::newRow( "lpad" ) << "lpad('Hello', 10, 'x')" << false << QVariant( "Helloxxxxx" );
297297
QTest::newRow( "lpad truncate" ) << "rpad('Hello', 4, 'x')" << false << QVariant( "Hell" );
298298
QTest::newRow( "title" ) << "title(' HeLlO WORLD ')" << false << QVariant( " Hello World " );
299+
QTest::newRow( "trim" ) << "trim(' Test String ')" << false << QVariant( "Test String" );
300+
QTest::newRow( "trim empty string" ) << "trim('')" << false << QVariant( "" );
299301
QTest::newRow( "format" ) << "format('%1 %2 %3 %1', 'One', 'Two', 'Three')" << false << QVariant( "One Two Three One" );
300302

301303
// implicit conversions

0 commit comments

Comments
 (0)