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

[JavaScript] Working with strings

/* 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

Leave a Reply

Your email address will not be published. Required fields are marked *