Homelab, Linux, JS & ABAP (~˘▾˘)~
 

[JavaScript] Working with strings

01
02
03
04
05
06
07
08
09
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
/* Escape Sequences in Strings
Code    Output                  
\'  single quote
\"  double quote
\\  backslash
\n  newline
\r  carriage return
\t  tab
\b  word boundary
\f  form feed               */
 
var firstStr = 'I come first. '
var secondStr = 'I come second.'
 
/* Concatenating Strings with Plus Operator */
var myStr = firstStr + secondStr;
 
/* Concatenating Strings with the Plus Equals Operator */
var myStr = firstStr;
myStr += secondStr;
 
/* Bracket Notation to Find the First Character */
var firstName = "Carlos";
var firstLetter = firstName[0]; // firstLetter is "C"
var firstName[0] = "K";         // not possible! Individual characters of a string literal cannot be changed.
 
/* Bracket Notation to Find the Last Character in a String */
var firstName = "Carlos";
var lastLetter = firstName[firstName.length - 1]; // lastLetter is "s"
 
/* Split a string into an array of substrings */
var str = "How are you doing today?";
var res = str.split(" ");  // [ 'How', 'are', 'you', 'doing', 'today?' ]
var otherString = "How9are7you2today";
var byDigits = otherString.split(/\d/); // ["How", "are", "you", "today"]
 
/* Array of strings to single string */
var stringArray = [ 'How', 'are', 'you', 'doing', 'today?' ];
var str = stringArray.join(" "); // "How are you doing today?"
 
/* Check if a string ends with "universe." */
var str = "Hello world, welcome to the universe.";
var n = str.endsWith("universe.");
 
/* Lower & Upper Case */
console.log('ALPHABET'.toLowerCase()); // 'alphabet'
console.log('alphabet'.toUpperCase()); // 'ALPHABET'
console.log('test'.charAt(0).toUpperCase() + 'test'.slice(1)); // Test
 
/* reverse String */
let str = "test";
let reverseStr = str.split("").reverse().join(""); // tset