js String

STRINGS

  • When adding a string and a number, JS behaves oddly! It turns the number into a string and smushes them together.
    1. let year = "1998";
    2. year + 1;
    3. "19981"
  • String Template Literal:   use $.    $${3+5}. --->$8.  
  • single or double quotes are fine, but need to be constant
  • Strings are indexed
  • thing.method()
  • casing: .toLowerCase()/ .toUpperCase()
  • .trim()
  • indexOf. return the first index that a character occurs in the String,  if no character found, return -1 
    • "catdog".indexOf('cat');      //0 
    • "catdog".indexOf('dog');   //3
    • "catdog".indexOf('z');   //-1
  • .slice
    • "Angle of Mine".slice(0,5);  //'Angle'.  [0,5) 
    • "Angel of Mine".slice(5); //  ' of Mine '
  • .replace('a','b')
  • Template Literals, $${52.0}. ---- $52.0
    • `I count ${3+5} sheep`; //"I count 7 sheep", use the back-ticks, not single quotes.
  • NULL & UNDEFINED
    • Null: International absence of any value, must be assigned
      • let a = null;
    • Undefined: Variables that do not have an assigned value are undefined, the console do not know what you want.
      • let pickles;//we did not assign a value
      • pickles; //undefined
      • let food = 'tacos';
      • food[7] //undefined
  • MATH OBJECT
    • Math.PI
    • Math.round(4.9). //5. rounding a number
    • Math.abs(-1). //1
    • Math.floor(3.9999). //3. removes decimal
    • Math.ceil(3.000001).  //4
  • RANDOM NUMBERS
    • Math.random().     [0,1). gives us a random decimal between 0 and 1(non-inclusive)
  • RANDOM INTEGERS
    • Math.floor(Math.random() * 10) + 1.   //generate random number between 1 and 10
      • const step1 = Math.random();
      • const step2 = step * 10;
      • const step3 = Math.floor(step2);
      • const step4 = step3+ 1;
1 //generate two randomly interger which is in [1,6]
2 const die1 = Math.floor(Math.random() * 6) + 1; //random number from 1-6
3 const die2 = Math.floor(Math.random() * 6) + 1; //random number from 1-6
4 
5 // creat a new variable called roll, which will  be a string that displays each die as well as their sum
6 //eg output: "You rolled a 3 and a 5. They sum to 8"
7 let roll = `"You rolled a ${die1} and a ${die2}. They sum to ${die1 + die2}"`;

  • Strings_cheatsheet.js
    • single/double quotes
    • .length
    • indexing
    • .toUpperCase()/.toLowerCase()
    • .trim()  is to remove space in the left most side and the right most side
    • .indexOf('a')
    • .slice(a).  .slice(a,b)
    • .replace('a','b') with the first match replaced
    • String Template Literals   ` `  ${}  backtick characters, not single quote!!!
 1 // Making Strings
 2 let color = "purple";
 3 
 4 // Single quotes work too:
 5 let city = 'Tokyo';
 6 
 7 //Strings have a length:
 8 city.length; //5
 9 
10 //We can access specific characters using their index:
11 city[0]; //'T'
12 city[3]; //'y'
13 
14 // String methods:
15 'hello'.toUpperCase(); // "HELLO";
16 'LOL'.toLowerCase(); // "lol"
17 '    omg  '.trim(); // "omg"
18 
19 // String methods with arguments:
20 // ==============================
21 
22 //indexOf returns the index where the character is found (or -1 if not found)
23 'spider'.indexOf('i'); //2
24 'vesuvius'.indexOf('u'); //3 - only returns FIRST matching index
25 'cactus'.indexOf('z'); //-1 not found
26 27 // slice - returns a "slice" of a string 28 "pancake".slice(3); //"cake" - slice from index 3 onwards 29 "pancake".slice(0, 3); //"pan" - slice from index 0 up to index 3 30 31 // replace - returns a new string, with the FIRST match replaced 32 "pump".replace("p", "b"); //"bump" - only replaces first "p" 33 34 // String Template Literals 35 // Use backtick characters, NOT SINGLE QUOTES! 36 // ======================== 37 const color = "olive green"; 38 const msg = `My favorite color is: ${color}` //"My favorite color is: olive green" 39 40 const str = `There are ${60 * 60 * 24} seconds in a day`//"There are 86400 seconds in a day"

 

上一篇:重建二叉树


下一篇:Python Slice对象到Sequence序列对象