Shawn Cain Shawn Cain - 1 month ago 8
Sass (Sass) Question

Is there a way in SASS to do if string contains or ends with?

I need an if statement in sass like the so:

@each $name, $char in $font-icons {
@if ($name ends with "-outline") {
//do something
}
}


Is there a way to do this?

Answer

Not exactly what you were looking for but I think it's about as close as you can get with sass.

Using str-index($string, $substring) you can find out if $name contains -outline:

@each $name, $char in $font-icons {
    @if (str-index($name, '-outline')) {
        //do something
    }
}

EDIT: Just wrote a quick sass function to actually find out if the string ends with another string:

@function ends-with($string, $find) {
  @if (str-index($string, $find) == (str-length($string) - str-length($find) + 1)) {
    @return true;
  } @else {
    @return false;
  }
}

@each $name, $char in $font-icons {
    @if (ends-with($name, '-outline')) {
        //do something
    }
}