Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/split-on-first
Split a string on the first occurrence of a given separator
https://github.com/sindresorhus/split-on-first
npm-package split string string-manipulation string-split
Last synced: about 1 month ago
JSON representation
Split a string on the first occurrence of a given separator
- Host: GitHub
- URL: https://github.com/sindresorhus/split-on-first
- Owner: sindresorhus
- License: mit
- Created: 2019-02-07T10:21:41.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2022-07-08T13:05:26.000Z (over 2 years ago)
- Last Synced: 2024-04-13T17:56:42.456Z (7 months ago)
- Topics: npm-package, split, string, string-manipulation, string-split
- Language: JavaScript
- Homepage:
- Size: 8.79 KB
- Stars: 74
- Watchers: 3
- Forks: 14
- Open Issues: 1
-
Metadata Files:
- Readme: readme.md
- Funding: .github/funding.yml
- License: license
- Security: .github/security.md
Awesome Lists containing this project
README
# split-on-first
> Split a string on the first occurrence of a given separator
This is similar to [`String#split()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split), but that one splits on all the occurrences, not just the first one.
## Install
```
$ npm install split-on-first
```## Usage
```js
import splitOnFirst from 'split-on-first';splitOnFirst('a-b-c', '-');
//=> ['a', 'b-c']splitOnFirst('key:value:value2', ':');
//=> ['key', 'value:value2']splitOnFirst('a---b---c', '---');
//=> ['a', 'b---c']splitOnFirst('a-b-c', '+');
//=> []splitOnFirst('abc', '');
//=> []
```## API
### splitOnFirst(string, separator)
#### string
Type: `string`
The string to split.
#### separator
Type: `string`
The separator to split on.
## Related
- [split-at](https://github.com/sindresorhus/split-at) - Split a string at one or more indices