如何删除SASS字符串中的空格? [英] How to remove whitespace in SASS string?

查看:51
本文介绍了如何删除SASS字符串中的空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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).

类似于:

$str-var: "The White Lion";

@mixin bg-img($name) {
  background-image: url("#{$name}.jpg");
}

.image-cover {
  @include bg-img(str-remove-whitespace($str-var));
}

预期结果:

.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;
}

您可以在 SASS 文档中看到的可用函数列表.

List of available functions you can see into SASS documentation.

这篇关于如何删除SASS字符串中的空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆