Skip to content

Instantly share code, notes, and snippets.

@Snugug
Created November 13, 2013 16:32
Show Gist options
  • Select an option

  • Save Snugug/7452004 to your computer and use it in GitHub Desktop.

Select an option

Save Snugug/7452004 to your computer and use it in GitHub Desktop.
Generated by SassMeister.com.
// ----
// Sass (v3.3.0.rc.1)
// Compass (v0.13.alpha.10)
// ----
$test: 'Hello World My Name Is Sam';
//////////////////////////////
// str-replace as single function
//////////////////////////////
@function str-replace($string, $search, $replace) {
$length: str-length($replace);
$index: str-index($string, $search);
$slice: $string;
@while $index != 0 {
$slice: str-slice($slice, 0, $index - 1);
$slice: $slice + $replace;
$slice: $slice + str-slice($string, $index + $length);
@return str-replace($slice, $search, $replace);
}
@return $slice;
}
.single-function {
content: str-replace($test, ' ', '-');
}
//////////////////////////////
// str-replace as recursive function
//////////////////////////////
@function str-replace($string, $search, $replace) {
$index: str-index($string, $search);
@if $index != 0 {
$slice: str-slice($string, 0, $index - 1);
$slice: $slice + $replace;
$slice: $slice + str-slice($string, $index + str-length($replace));
@return str-replace($slice, $search, $replace);
}
@return $string;
}
.recursive-function {
content: str-replace($test, ' ', '-');
}
.single-function {
content: "Hello-World-My-Name-Is-Sam";
}
.recursive-function {
content: "Hello-World-My-Name-Is-Sam";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment