Is there a string function that remove whitespaces in a string in SASS?
For an instance, I'd like to use a variable (string with spaces) to specify a resource image file (name without spaces).
Something like:
$str-var: "The White Lion";
@mixin bg-img($name) {
background-image: url("#{$name}.jpg");
}
.image-cover {
@include bg-img(str-remove-whitespace($str-var));
}
Expected result:
.image-cover {
background-image: url("TheWhiteLion.jpg");
}
There is no such built-in function, but it can be implemented by searching for spaces into string and cutting them out. Something like this should work:
@function str-remove-whitespace($str) {
@while (str-index($str, ' ') != null) {
$index: str-index($str, ' ');
$str: "#{str-slice($str, 0, $index - 1)}#{str-slice($str, $index + 1)}";
}
@return $str;
}
List of available functions you can see into SASS documentation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With