Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * STRING MANIPULATION
- *
- * These are methods for manipulating and playing with strings!
- * ALL STRING METHODS RETURN A NEW STRING... Strings cannot be
- * changed, they can only be replaced.
- */
- //string length//
- var string = "Here's some silly nonsense!!";
- console.log(string.length);
- /*
- * similar to array.length, evaluates how many characters are
- * inside of a string.
- */
- //searching strings//
- console.log(string.indexOf("s", 10));
- console.log(string.lastIndexOf("s"));
- console.log(string.search("s"));
- /*
- * indexOf and search will return the first position of either
- * a string or character within a string, where as lastIndexOf
- * will return the last. You can define a starting position in
- * the indexOf method, as in the example above. The same CANNOT
- * be done with the search method. The search method, however,
- * can use regular expressions in it's parameters. So...
- *
- * string.search(/words/i);
- *
- * would perform a case insensitive search for the 'words'.
- * Other regular expressions include
- * g to perform a global search, it won't stop after the
- * first result.
- * m to perform multiline matching.
- *
- * There are other ways to utilize regular expressions in the
- * search method, for future reference..
- */
- //extracting string parts//
- console.log(string.slice(16, 20));
- /*
- * slice takes two arguments and returns the portion of the
- * string between those two indecies. Negative paramaters can
- * be used for both parameters. A negative in the first will
- * cause it to count from the end of the string, a negative in
- * the second will count backwards from the index defined in
- * the first. If you use this method with only the first
- * parameter, it will slice out the rest of the string.
- */
- console.log(string.substring(16, 20));
- /*
- * substring is nearly identical to slice, but it cannot take
- * negative numbers.
- */
- console.log(string.substr(16, 4));
- /*
- * substr differs from slice in that the second parameter
- * defines the length the string is extracted. It cannot be
- * negative. The first parameter functions identically to slice,
- * however.
- */
- //replacing part of a string//
- console.log(string.replace("nonsense", "very serious business"));
- /*
- * The replace method takes two arguments and replaces the first
- * instance of the first argument with the second. By default, it
- * is case sensitive, so in my example NONsense would not replace
- * nonsense. Regular expressions can be used to expand it's reach.
- * /g is used to replace all matches (global matches). So..
- *
- * string.replace(/nonsense/g, "very serious business");
- *
- * /i can be used to make replace case insensitive.
- * string.replace(/NoNSEnse/i, "very serious business");
- *
- */
- //toUpperCase and toLowerCase//
- console.log(string.toUpperCase());
- console.log(string.toLowerCase());
- /*
- * These methods return the string in uppercase or lowercase,
- * respectively.
- */
- //joining strings//
- var string2 = "But actually it's very serious.";
- console.log(string.concat(" ", string2));
- /*
- * concat is used to join two or more strings. It works the same
- * as doing string1 + " " + string2;
- */
- //extracting characters//
- console.log(string.charAt(7));
- console.log(string.charCodeAt(7));
- /*
- * These are, according to w3schools, the only two safe methods
- * for extracting a character from a string. charAt will evaluate
- * the character at the position, and charCodeAt evaluates the
- * unicode of the character at the specified index.
- */
- console.log(string[7]);
- /*
- * This method does not work with internet explorer 5, 6, or 7.
- * It makes your string look like an array which can be confusing
- * for others looking over your code. It's best to avoid for now.
- */
- //convert string to array//
- console.log(string.split(" "));
- console.log(string.split(""));
- /*
- * The split method will create an array from a string at any
- * instances of the character passed into the argument. If you use
- * "" without a defined character, it splits the string at every
- * character. Neat!
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement