README.md (5836B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2021 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # substringBefore 22 23 > Return the part of a string before a specified substring. 24 25 <!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. --> 26 27 <section class="intro"> 28 29 </section> 30 31 <!-- /.intro --> 32 33 <!-- Package usage documentation. --> 34 35 <section class="usage"> 36 37 ## Usage 38 39 ```javascript 40 var substringBefore = require( '@stdlib/string/substring-before' ); 41 ``` 42 43 #### substringBefore( str, search ) 44 45 Returns the part of a string before a specified substring. 46 47 ```javascript 48 var str = 'beep boop'; 49 var out = substringBefore( str, ' ' ); 50 // returns 'beep' 51 52 out = substringBefore( str, 'o' ); 53 // returns 'beep b' 54 ``` 55 56 </section> 57 58 <!-- /.usage --> 59 60 <!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 61 62 <section class="notes"> 63 64 ## Notes 65 66 - If a substring is not present in a provided string, the function returns the input string. 67 - If provided an empty substring, the function returns an empty string. 68 69 </section> 70 71 <!-- /.notes --> 72 73 <!-- Package usage examples. --> 74 75 <section class="examples"> 76 77 ## Examples 78 79 <!-- eslint no-undef: "error" --> 80 81 ```javascript 82 var substringBefore = require( '@stdlib/string/substring-before' ); 83 84 var out = substringBefore( 'beep boop', 'p' ); 85 // returns 'bee' 86 87 out = substringBefore( 'Hello World!', 'xyz' ); 88 // returns 'Hello World!' 89 90 out = substringBefore( 'Hello World!', '' ); 91 // returns '' 92 93 out = substringBefore( '', 'xyz' ); 94 // returns '' 95 ``` 96 97 </section> 98 99 <!-- /.examples --> 100 101 <!-- Section for describing a command-line interface. --> 102 103 * * * 104 105 <section class="cli"> 106 107 ## CLI 108 109 <!-- CLI usage documentation. --> 110 111 <section class="usage"> 112 113 ### Usage 114 115 ```text 116 Usage: substring-before [options] --search=<string> [<string>] 117 118 Options: 119 120 -h, --help Print this message. 121 -V, --version Print the package version. 122 --search string Search string. 123 --split sep Delimiter for stdin data. Default: '/\\r?\\n/'. 124 ``` 125 126 </section> 127 128 <!-- /.usage --> 129 130 <!-- CLI usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 131 132 <section class="notes"> 133 134 ### Notes 135 136 - If the split separator is a [regular expression][mdn-regexp], ensure that the `split` option is either properly escaped or enclosed in quotes. 137 138 ```bash 139 # Not escaped... 140 $ echo -n $'foo\nbar\nbaz' | substring-before --search a --split /\r?\n/ 141 142 # Escaped... 143 $ echo -n $'foo\nbar\nbaz' | substring-before --search a --split /\\r?\\n/ 144 ``` 145 146 - The implementation ignores trailing delimiters. 147 148 </section> 149 150 <!-- /.notes --> 151 152 <!-- CLI usage examples. --> 153 154 <section class="examples"> 155 156 ### Examples 157 158 ```bash 159 $ substring-before abcdefg --search d 160 abc 161 ``` 162 163 To use as a [standard stream][standard-streams], 164 165 ```bash 166 $ echo -n $'beep\nboop' | substring-before --search p 167 bee 168 boo 169 ``` 170 171 By default, when used as a [standard stream][standard-streams], the implementation assumes newline-delimited data. To specify an alternative delimiter, set the `split` option. 172 173 ```bash 174 $ echo -n 'beep\tboop' | substring-before --search p --split '\t' 175 bee 176 boo 177 ``` 178 179 </section> 180 181 <!-- /.examples --> 182 183 </section> 184 185 <!-- /.cli --> 186 187 <!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 188 189 <section class="references"> 190 191 </section> 192 193 <!-- /.references --> 194 195 <!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. --> 196 197 <section class="related"> 198 199 * * * 200 201 ## See Also 202 203 - <span class="package-name">[`@stdlib/string/substring-before-last`][@stdlib/string/substring-before-last]</span><span class="delimiter">: </span><span class="description">return the part of a string before the last occurrence of a specified substring.</span> 204 - <span class="package-name">[`@stdlib/string/substring-after`][@stdlib/string/substring-after]</span><span class="delimiter">: </span><span class="description">return the part of a string after a specified substring.</span> 205 - <span class="package-name">[`@stdlib/string/substring-after-last`][@stdlib/string/substring-after-last]</span><span class="delimiter">: </span><span class="description">return the part of a string after the last occurrence of a specified substring.</span> 206 207 </section> 208 209 <!-- /.related --> 210 211 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 212 213 <section class="links"> 214 215 [standard-streams]: https://en.wikipedia.org/wiki/Standard_streams 216 217 [mdn-regexp]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions 218 219 <!-- <related-links> --> 220 221 [@stdlib/string/substring-before-last]: https://github.com/stdlib-js/string/tree/main/substring-before-last 222 223 [@stdlib/string/substring-after]: https://github.com/stdlib-js/string/tree/main/substring-after 224 225 [@stdlib/string/substring-after-last]: https://github.com/stdlib-js/string/tree/main/substring-after-last 226 227 <!-- </related-links> --> 228 229 </section> 230 231 <!-- /.links -->