1 function manipulateString(passedString1, passedString2) {
2
3 var concatString;
4
5 // The string passed to concat is added to the end of the first string
6
7 concatString = passedString1.concat(passedString2);
8
9 alert(concatString);
10
11 // The following if statement will be true since first word is Tony
12
13 if (concatString.charAt(3) == "y") {
14
15 alert("Character found!");
16
17 }
18
19 // The last position of the letter n is 10
20
21 alert("The last index of n is: " + concatString.lastIndexOf("n"));
22
23 // A regular expression is used to locate and replace the substring
24
25 var newString = concatString.replace(/Tony/gi,"General");
26
27 // The following yields Please salute General Patton
28
29 alert("Please salute " + newString);
30
31 // The match function returns an array containing all matches found
32
33 matchArray = concatString.match(/Tony/gi);
34
35 for (var i=0; i<matchArray.length;i++) {
36
37 alert("Match found: " + matchArray);
38
39 }
40
41 // Determine if the regular expression is found, a ?1 indicates no
42
43 if (newString.search(/Tony/) == -1) {
44
45 alert("String not found");
46
47 } else {
48
49 alert("String found.");
50
51 }
52
53 // Extract a portion of the string and store it in a new variable
54
55 var sliceString = newString.slice(newString.indexOf("l")+2,newString.length);
56
57 alert(sliceString);
58
59 // The split function creates a new array containing each value separated by a space
60
61 stringArray = concatString.split(" ");
62
63 for (var i=0; i<stringArray.length;i++) {
64
65 alert(stringArray;
66
67 }
68
69 alert(newString.toUpperCase());
70
71 alert(newString.toLowerCase());
72
73 }
| 欢迎光临 站长论坛 (http://tzlink.com/bbs/) | Powered by Discuz! X3.2 |