-
Notifications
You must be signed in to change notification settings - Fork 22
/
string-methods.js
93 lines (72 loc) · 3.89 KB
/
string-methods.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/**
* 字符串方法
*
* @参考资料:
* http://www.w3schools.com/js/js_string_methods.asp
* http://techiejs.com/Blog/Post/Essential-JavaScript-String-Functions
* https://rapd.wordpress.com/2007/07/12/javascript-substr-vs-substring/
* http://www.bennadel.com/blog/2159-using-slice-substring-and-substr-in-javascript.htm
*
*/
// String.prototype.charAt()
var myName = "Oleg Shalygin";
// 使用方式: myName.charAt(index)
var letter = myName.charAt(6); // h
// String.prototype.indexOf()
var fullName = "Oleg Shalygin";
// 使用方式: fullName.indexOf(searchTerm, startingIndex)
var index = fullName.indexOf("Oleg"); // 0
index = fullName.indexOf("Shalygin"); // 5
index = fullName.indexOf("l"); // 1
index = fullName.indexOf("l",4); // 8
index = fullName.indexOf("Girlfriend"); // -1
// String.prototype.lastIndexOf()
var storyMode = "Once upon a time, there was a magical foobar that was controlling the universe...";
console.log(storyMode.lastIndexOf(",")); // 16
console.log(storyMode.lastIndexOf(".")); // 80
console.log(storyMode.lastIndexOf("!")); // -1
console.log(storyMode.lastIndexOf("foobar")); // 38
// String.prototype.match()
var someString = "Hello there, my name is aararand and I am a magical foobarus creature";
var resultsArray = someString.match(/a{2}/);
// resultsArray = ["aa", index: 24, input: "Hello there, my name is aararand and I am a magical foobarus creature"]
var someOtherResultsArray = someString.match(/b{2}/);
// someOtherResultsArray = null
// String.prototype.replace()
var someString = "Hello there, my name is aararand and I am a magical foobarus creature";
var newString = someString.replace(/a{2}/, "lol");
// newString = "Hello there, my name is lolrarand and I am a magical foobarus creature"
// String.prototype.slice()
var story = "Foobarus is a magical unicorn with an ID of 21313 which flies higher than all other unicorns. Unicorns fly? Regardless!";
var previewStory = story.slice(0, 40); // Foobarus is a magical unicorn with an ID
// String.prototype.split()
var story = "Foobarus is a magical unicorn with an ID of 21313 which flies higher than all other unicorns. Unicorns fly? Regardless!";
var previewStory = story.split(".");
console.log(previewStory[0]); // Foobarus is a magical unicorn with an ID of 21313 which flies higher than all other unicorns
console.log(previewStory[1]); // Unicorns fly? Regardless!
// String.prototype.substring()
// 注:
// substring 方法的第二个参数代表截取停止位置的 index(不包含最后这个值)
// 而 substr 的第二份参数则代表截取长度
// 语法: string.substr(start, length);
// 语法: string.substring(start, stop);
//
// 除此以外,slice() 和 substring() 类似,但不同之处在于 slice 可以接收负的 index,即从数组末尾开始操作
var story = "Foobarus is a magical unicorn with an ID of 21313 which flies higher than all other unicorns. Unicorns fly? Regardless!";
var theLastFewCharacters = story.substring(story.length - 20);
console.log("..." + theLastFewCharacters); // ...ns fly? Regardless!
// String.prototype.toLowerCase() 和 String.prototype.toUpperCase()
var story = "Foobarus is a magical unicorn with an ID of 21313 which flies higher than all other unicorns. Unicorns fly? Regardless!";
var allUpperCase = story.toUpperCase();
var allLowerCase = story.toLowerCase();
var foobarus = allUpperCase.match(/FOOBARUS/); // ["FOOBARUS", index: 0, input: "FOOBARUS IS A MAGICAL UNICORN WITH AN ID OF 21313 …N ALL OTHER UNICORNS. UNICORNS FLY? REGARDLESS!]
// String.prototype.trim()
var fullName = " Oleg Shalygin ";
var trimmedFullName = fullName.trim(); // Oleg Shalygin
// String.prototype.concat()
var firstName = "Oleg";
var lastName = "Shalygin";
var ID = 12321312;
// 使用方式: firstName.concat(string2, string 3, string 3, ...);
var fullIdentification = firstName.concat(lastName, ":", ID);
console.log(fullIdentification); // OlegShalygin:12321312