Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove whitespace in SASS string?

Tags:

sass

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");
}
like image 684
Küroro Avatar asked Oct 18 '17 07:10

Küroro


1 Answers

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.

like image 63
Flying Avatar answered Nov 17 '22 21:11

Flying