Skip to content

Instantly share code, notes, and snippets.

@defields923
Forked from anonymous/index.html
Last active December 1, 2016 21:11
Show Gist options
  • Save defields923/6c6fcf445b8e959483652ac976c6639b to your computer and use it in GitHub Desktop.
Save defields923/6c6fcf445b8e959483652ac976c6639b to your computer and use it in GitHub Desktop.
StringsStrings studies// source http://jsbin.com/bimiqe
// -----Data Types: STRING----- //
/* Strings are character or textual data strung together enclosed
by either single or double quotes. */
var stri = "I'm a sequence of character data!";
console.log(stri);
/* Almost anything can be put within a string with the exception
of a number of special characters, such as the newline "\n" and
horizontal tab "\t". Since a string must be on a single line,
to type and log a newline character, precede it by another
backslash. The two will collapse into each other and type out the
intended text: */
var exStri = "I want to show you a (backslash n) \\n!"
// The double backlash causes them to collapse into one
console.log(exStri);
var newlineStri = "First line.\n Second line.";
// "\n" by itself starts a new line.
console.log(newlineStri);
/* Individual characters of a string are zero-based indexed,
and can have its contents altered by a number of methods; however,
those individual contents are immutable and cannot be directly
altered after a string's creation: */
var nopeStri = "You can't alter my individual characters!";
nopeStri[0] = "P";
console.log(nopeStri); // Still logs "You..."
/* Altering the contents using a built-in method is possible: */
var re = /(\w+)\s(\w+)/;
var str = 'Devin Fields';
var newStr = str.replace(re, '$2, $1');
console.log(newStr); // logs "Fields, Devin"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment