> For the complete documentation index, see [llms.txt](https://yamoo9.gitbook.io/scss-mixins/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yamoo9.gitbook.io/scss-mixins/utilities/string/str-replace.md).

# str-replace()

문자 값의 일부를 다른 문자 값으로 대체하는 유틸리티 함수

## 용도 <a href="#use" id="use"></a>

특정 문자의 내용 중 일부 문자를 검색해 다른 문자로 대체하고자 할 경우 사용합니다.

## 사용법 <a href="#usage" id="usage"></a>

`str-replace()` 함수에 변경할 문자, 찾을 검색어, 변경할 문자 값을 전달합니다.

> str-replace($string:**string**, $search:**string**, $replace:**string**) → **string**

```javascript
@debug str-replace("string replace", " ", "-"); // "string-replace" 반환
@debug str-replace("top Left", " ");            // "topLeft" 반환
```

| 매개변수(parameter) | 유형(type) | 필수(required) | 기본 값(default) |
| :-------------: | :------: | :----------: | :-----------: |
|     $string     |  string  |      ✔︎      |               |
|     $search     |  string  |      ✔︎      |               |
|     $replace    |  string  |              |      `''`     |

## 로직 <a href="#logic" id="logic"></a>

`str-replace()` 유틸리티는 다음의 로직에 의해 작성되었습니다.&#x20;

```javascript
@function str-replace($string, $search, $replace: '') {
  $index: str-index($string, $search);

  @if $index {
    @return str-slice($string, 1, $index - 1) + $replace +
      str-replace(
        str-slice($string, $index + str-length($search)),
        $search,
        $replace
      );
  }

  @return $string;
}
```

1. 전달 받은 컬러 이름(`$name`)이 [ $colors 구성 변수](/scss-mixins/getting-started/configure.md#undefined)에 등록된 이름인지 검사
2. 컬러 이름이 컬러 구성표에 포함된 이름인 경우, `true` 반환
3. 컬러 이름이 컬러 구성표에 포함되지 않은 이름인 경우, `false` 반환

## 참고 <a href="#reference" id="reference"></a>

유틸리티 함수 로직에 사용된 Sass 빌트인 모듈은 다음과 같습니다.

* [@if / @else](https://sass-lang.com/documentation/at-rules/control/if)
* [str-index()](https://sass-lang.com/documentation/modules/string#index)
* [str-slice()](https://sass-lang.com/documentation/modules/string#slice)
* [str-length()](https://sass-lang.com/documentation/modules/string#length)
