[string.js](http://stringjs.com)
=========
[![build status](https://secure.travis-ci.org/jprichardson/string.js.png)](http://travis-ci.org/jprichardson/string.js)
[![Sauce Test Status](https://saucelabs.com/browser-matrix/stringjs.svg)](https://saucelabs.com/u/stringjs)
`string.js`, or simply `S` is a lightweight (**< 5 kb** minified and gzipped) JavaScript library for the browser or for Node.js that provides extra String methods. Originally, it modified the String prototype. But I quickly learned that in JavaScript, this is considered poor practice.
Why?
----
Personally, I prefer the cleanliness of the way code looks when it appears to be native methods. i.e. when you modify native JavaScript prototypes. However, if any app dependency required `string.js`, then the app's string prototype would be modified in every module. This could be troublesome. So I settled on creating a wrapper a la jQuery style. For those of you prototype hatin' fools, there is the method `extendPrototype()`.
Here's a list of alternative frameworks:
* [Prototype Framework's String library](http://prototypejs.org/api/string)
* [Uize.String](http://www.uize.com/reference/Uize.String.html)
* [Google Closure's String](http://closure-library.googlecode.com/svn/docs/namespace_goog_string.html)
* [Underscore.string](http://epeli.github.com/underscore.string/)
* [Sugar.js](http://sugarjs.com)
* [php.js](http://phpjs.org/functions/)
Why wasn't I happy with any of them? They are all static methods that don't seem to support chaining in a clean way 'OR' they have an odd dependency. Sugar is the notable exception.
Installation
------------
npm install --save string
Experiment with String.js Now
-----------------------------
Assuming you're on http://stringjs.com, just simply open up the Webkit inspector in either Chrome or Safari, or the web console in Firefox and you'll notice that `string.js` is included in this page so that you can start experimenting with it right away.
Usage
-----
### Node.js
```javascript
var S = require('string');
```
Originally, I was using `$s` but glancing over the code, it was easy to confuse `$s` for string.js with `$` for jQuery. Feel free to use the most convenient variable for you.
### Rails
Checkout this gem to easily use string.js on the asset pipeline: https://github.com/jesjos/stringjs-rails
### Browsers
```html
<!-- HTML5 -->
<script src="https://cdn.rawgit.com/jprichardson/string.js/master/lib/string.min.js"></script>
<!-- Note that in the mime type for Javascript is now officially 'application/javascript'. If you
set the type to application/javascript in IE browsers, your Javscript will fail. Just don't set a
type via the script tag and set the mime type from your server. Most browsers look at the server mime
type anyway -->
<!-- For HTML4/IE -->
<script type="text/javascript" src="https://cdn.rawgit.com/jprichardson/string.js/master/lib/string.min.js"></script>
```
A global variable `window.S` or simply `S` is created.
### AMD Support
It now [has AMD support](https://github.com/jprichardson/string.js/pull/20). See [require.js](http://requirejs.org/) on how to use with AMD modules.
### Both
```javascript
var doesIt = S('my cool string').left(2).endsWith('y'); //true
```
Access the wrapped string using `s` variable or `toString()`
```javascript
var name = S('Your name is JP').right(2).s; //'JP'
```
is the same as…
```javascript
var name = S('Your name is JP').right(2).toString(); //'JP'
```
Still like the clean look of calling these methods directly on native Strings? No problem. Call `extendPrototype()`. Make sure to not call this at the module level, at it'll effect the entire application lifecycle. You should really only use this at the method level. The one exception being if your application will not be a dependency of another application.
```javascript
S.extendPrototype();
var name = 'Your name is JP'.right(2); //'JP'
S.restorePrototype(); //be a good citizen and clean up
```
### Browser Compatiblity
`string.js` has been designed to be compatible with Node.js and with IE6+, Firefox 3+, Safari 2+, Chrome 3+. Please [click here][browsertest] to run the tests in your browser. Report any browser issues here: https://github.com/jprichardson/string.js/issues
### Extending string.js
See: https://github.com/jprichardson/string.js/pull/57
Native JavaScript Methods
-------------------------
`string.js` imports all of the native JavaScript methods. This is for convenience. The only difference is that the imported methods return `string.js` objects instead of native JavaScript strings. The one exception to this is the method `charAt(index)`. This is because `charAt()` only returns a string of length one. This is typically done for comparisons and a `string.js` object will have little to no value here.
All of the native methods support chaining with the `string.js` methods.
**Example:**
```javascript
var S = require('string');
var phrase = S('JavaScript is the best scripting language ever!');
var sub = 'best scripting';
var pos = phrase.indexOf(sub);
console.log(phrase.substr(pos, sub.length).truncate(8)); //best...
```
Methods
-------
See [test file][testfile] for more details.
I use the same nomenclature as Objective-C regarding methods. **+** means `static` or `class` method. **-** means `non-static` or `instance` method.
### - constructor(nativeJsString) ###
This creates a new `string.js` object. The parameter can be anything. The `toString()` method will be called on any objects. Some native objects are used in some functions such as `toCSV()`.
Example:
```javascript
S('hello').s //"hello"
S(['a,b']).s //"a,b"
S({hi: 'jp'}).s //"[object Object]""
```
### - between(left, right)
Extracts a string between `left` and `right` strings.
Example:
```javascript
S('<a>foo</a>').between('<a>', '</a>').s // => 'foo'
S('<a>foo</a></a>').between('<a>', '</a>').s // => 'foo'
S('<a><a>foo</a></a>').between('<a>', '</a>').s // => '<a>foo'
S('<a>foo').between('<a>', '</a>').s // => ''
S('Some strings } are very {weird}, dont you think?').between('{', '}').s // => 'weird'
S('This is a test string').between('test').s // => ' string'
S('This is a test string').between('', 'test').s // => 'This is a '
```
### - camelize()
Remove any underscores or dashes and convert a string into camel casing.
Example:
```javascript
S('data_rate').camelize().s; //'dataRate'
S('background-color').camelize().s; //'backgroundColor'
S('-moz-something').camelize().s; //'MozSomething'
S('_car_speed_').camelize().s; //'CarSpeed'
S('yes_we_can').camelize().s; //'yesWeCan'
```
### - capitalize() ###
Capitalizes the first character of a string.
Example:
```javascript
S('jon').capitalize().s; //'Jon'
S('JP').capitalize().s; //'Jp'
```
### - chompLeft(prefix)
Removes `prefix` from start of string.
Example:
```javascript
S('foobar').chompLeft('foo').s; //'bar'
S('foobar').chompLeft('bar').s; //'foobar'
```
### - chompRight(suffix)
Removes `suffix` from end of string.
Example:
```javascript
S('foobar').chompRight('bar').s; //'foo'
S('foobar').chompRight('foo').s; //'foobar'
```
### - collapseWhitespace() ###
Converts all adjacent whitespace characters to a single space.
Example:
```javascript
var str = S(' String \t libraries are \n\n\t fun\n! ').collapseWhitespace().s; //'String libraries are fun !'
```
### - contains(ss) ###
Returns true if the string contains `ss`.
Alias: `include()`
Example:
```javascript
S('JavaScript is one of the best languages!').contains('one'); //true
```
### - count(substring) ###
Returns the count of the number of occurrences of the substring.
Example:
```javascript
S('JP likes to program. JP does not play in the NBA.').count("JP")// 2
S('Does not exist.').count("Flying Spaghetti Monster") //0
S('Does not exist.').count("Bigfoot") //0
S('JavaScript is fun, therefore Node.js is fun').count("fun") //2
S('funfunfun').count("fun") //3
```
### - dasherize() ##
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Visual Studio Code.zip (2000个子文件)
ayyo.css 550KB
workbench.main.css 444KB
issueReporterMain.css 13KB
octicons.css 8KB
url.css 6KB
markdown.css 5KB
base.css 5KB
markdown.css 3KB
purebasic.css 2KB
grayscale.css 2KB
markdown.css 2KB
gruvbox-light.css 1KB
gruvbox-dark.css 1KB
hybrid.css 1KB
atelier-estuary-light.css 1KB
atelier-savanna-light.css 1KB
atelier-plateau-light.css 1KB
atelier-estuary-dark.css 1KB
atelier-savanna-dark.css 1KB
atelier-plateau-dark.css 1KB
atelier-cave-light.css 1KB
agate.css 1KB
atelier-cave-dark.css 1KB
railscasts.css 1KB
sunburst.css 1KB
idea.css 1KB
default.css 1KB
tomorrow-night-blue.css 1KB
atelier-sulphurpool-light.css 1KB
atelier-sulphurpool-dark.css 1KB
tomorrow-night.css 1KB
github.css 1KB
solarized-light.css 1KB
solarized-dark.css 1KB
docco.css 1KB
atelier-lakeside-light.css 1KB
atelier-lakeside-dark.css 1KB
pojoaque.css 1KB
atelier-seaside-light.css 1KB
atelier-seaside-dark.css 1KB
atelier-forest-light.css 1KB
atelier-forest-dark.css 1KB
atelier-heath-light.css 1KB
atelier-heath-dark.css 1KB
atelier-dune-light.css 1KB
atelier-dune-dark.css 1KB
foundation.css 1KB
tomorrow-night-eighties.css 1KB
tomorrow-night-bright.css 1KB
obsidian.css 1KB
xcode.css 1KB
kimbie.light.css 1KB
kimbie.dark.css 1KB
hopscotch.css 1KB
arduino-light.css 1KB
googlecode.css 1KB
xt256.css 1KB
tomorrow.css 1KB
monokai-sublime.css 1KB
dracula.css 1016B
paraiso-light.css 1015B
paraiso-dark.css 1014B
ocean.css 1004B
school-book.css 999B
rainbow.css 983B
qtcreator_light.css 978B
tomorrow.css 978B
qtcreator_dark.css 977B
zenburn.css 947B
monokai.css 938B
github-gist.css 919B
magula.css 891B
darkula.css 890B
color-brewer.css 884B
ir-black.css 871B
arta.css 852B
far.css 849B
brown-paper.css 842B
codepen-embed.css 842B
vs.css 837B
dark.css 794B
androidstudio.css 774B
mono-blue.css 749B
prettify.css 676B
ascetic.css 591B
sections.css 203B
octicons-animations.css 118B
properties.css 74B
RACSignal+Operations.h 32KB
metamacros.h 30KB
RACStream.h 13KB
RACSignal.h 9KB
MTLManagedObjectAdapter.h 9KB
MTLJSONAdapter.h 7KB
RACScheduler.h 7KB
SQRLUpdater.h 6KB
RACSequence.h 6KB
RACTuple.h 5KB
MTLModel.h 5KB
MTLModel+NSCoding.h 5KB
共 2000 条
- 1
- 2
- 3
- 4
- 5
- 6
- 20
资源评论
- 边江天然呆2018-09-07不能用,坑爹
bigicy
- 粉丝: 2
- 资源: 276
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功