RiaStdStringTools: Add method for removing white space

This commit is contained in:
Kristian Bendiksen
2023-08-21 10:53:06 +02:00
parent 76b8e69029
commit 14c37c4b2c
3 changed files with 33 additions and 0 deletions

View File

@@ -52,6 +52,16 @@ std::string RiaStdStringTools::trimString( const std::string& s )
return rightTrimString( leftTrimString( s ) );
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
std::string RiaStdStringTools::removeWhitespace( const std::string& line )
{
std::string s = line;
s.erase( std::remove_if( s.begin(), s.end(), isspace ), s.end() );
return s;
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------

View File

@@ -33,6 +33,7 @@ public:
static std::string trimString( const std::string& s );
static std::string rightTrimString( const std::string& s );
static std::string leftTrimString( const std::string& s );
static std::string removeWhitespace( const std::string& line );
static bool isNumber( const std::string& s, char decimalPoint );

View File

@@ -125,6 +125,28 @@ TEST( RiaStdStringToolsTest, RightTrimString )
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------
TEST( RiaStdStringToolsTest, RemoveWhitespace )
{
std::vector<std::pair<std::string, std::string>> testData = {
std::make_pair( " bla ", "bla" ),
std::make_pair( "bla ", "bla" ),
std::make_pair( " bla", "bla" ),
std::make_pair( "\tbla", "bla" ),
std::make_pair( "\tbla \n\t ,bla", "bla,bla" ),
std::make_pair( "\tbla\v", "bla" ),
std::make_pair( "bla", "bla" ),
std::make_pair( "", "" ),
};
for ( auto [input, expectedText] : testData )
{
EXPECT_EQ( RiaStdStringTools::removeWhitespace( input ), expectedText );
}
}
//--------------------------------------------------------------------------------------------------
///
//--------------------------------------------------------------------------------------------------