Skip to content

Instantly share code, notes, and snippets.

@AbudiMutamba
Created July 1, 2021 09:27
Show Gist options
  • Save AbudiMutamba/b23c69833530d6773aab77dd16e39245 to your computer and use it in GitHub Desktop.
Save AbudiMutamba/b23c69833530d6773aab77dd16e39245 to your computer and use it in GitHub Desktop.
Ablestate cohort1 -JS session 8
/**
* String Methods: They help us manipilste strings at work with strings.
*
* Methoda are the functions that are part of an object or class
* Properties/Attributes are the variables tha are part of the an object or class
* Example in JavaScript string objects have the charAt() method and the lenght property.
* To access methods and properties with the dot syntax
* ie.
* instance.method()
* instance.property
*/
/**
* string.charAt([index]): Returnd the character at given index in the string. If the given index is out of bounds, it returns an empty string.
* To output that the last character the index should be string.length - 1
*/
console.clear();
let elinyaLyeNsi = "Uganda";
let obuwanvu = elinyaLyeNsi.length;
console.log(elinyaLyeNsi.charAt(0));
console.log(elinyaLyeNsi.charAt(obuwanvu - 1));
console.log(elinyaLyeNsi.charAt(obuwanvu + 2));
let enyukuta = elinyaLyeNsi.charAt(obuwanvu + 2);
if (!enyukuta) {
console.log(
`Character at position ${obuwanvu + 2}
is Not found in string ${elinyaLyeNsi}`
);
}
if (enyukuta === "") {
console.log("Character is Not found in string");
}
/**
* stirng.charCodeAt()
*/
console.log(elinyaLyeNsi.charCodeAt(0));
console.log(elinyaLyeNsi.charCodeAt(obuwanvu)); //NaN
console.log(elinyaLyeNsi.charCodeAt(2));
console.log("");
console.log(elinyaLyeNsi.charCodeAt(obuwanvu - 1));
/**
* string.concat(string2, string3[..., stringN]): Combines two or more strings and returns a new string
*/
// let ebikwataKuNsi = elinyaLyeNsi.conacat("" , "is pearl of Africa");
let ebikwataKuNsi = elinyaLyeNsi.concat(" is pearl of Africa");
console.log(ebikwataKuNsi);
let ekibugaEkikulu = elinyaLyeNsi.concat(); //
console.log(ekibugaEkikulu);
console.log(elinyaLyeNsi);
ekibugaEkikulu = elinyaLyeNsi.concat("kampala is the capital city");
console.log(ekibugaEkikulu);
/**
* string.indexOf(searchValue[, fromIndex]);
* It searches the string for a given string passed as the searchValue parameter and returns the position of the first occurance of searched string if it's found. If it's not a -1 will be returned. You can also specify an optional fromIndex parameter to suggest where the search will start from.
*/
let positionOfW = elinyaLyeNsi.indexOf("W"); // Check whether it will return -1
console.log(positionOfW); //-1 Is return
let positionOfFirsta = elinyaLyeNsi.indexOf("a");
console.log(positionOfFirsta); // Returns the correct position of the first a in Uganda
let positionOfLasta = elinyaLyeNsi.indexOf("a", 3);
console.log(positionOfLasta); // Returns the correct position of the first a in Uganda. which is 5 beacuse we specified the search to last from position 3 of the string.
/**
* string.LastIndexof()
*/
let positionOfZ = elinyaLyeNsi.lastIndexOf("W"); // Check whether it will return -1
console.log(positionOfZ); //-1 Is return
let positionOfLasta2 = elinyaLyeNsi.lastIndexOf("a");
console.log(positionOfLasta2); // Returns the correct position of the first a in Uganda
let positionOfFirsta2 = elinyaLyeNsi.lastIndexOf("a", 3);
console.log(positionOfFirsta2); /* Returns the correct position of the first a in Uganda. which is 5 beacuse we specified the search to last from position 3 of the string.*/
/**
* string.localCompare ([searchValue]): Compares two strings and returns 0 and 1 or -1
* 0 mean the strings fully match
* 1 means the searchValue is not found or is contained in the string but comes first in sort order
* - 1 means the searchValue is not found or is contained in the string but comes last in sort order
*/
console.clear();
let stringToSearch = "Uganda, Kampala";
let searchString1 = "Uganda";
let searchString2 = "Kampala";
console.log(stringToSearch.localeCompare("Mbarara")); // It will return 1
console.log(stringToSearch.localeCompare (stringToSearch)); // It will return 0
console.log(stringToSearch.localeCompare(searchString1)); // It will return 1
console.log(stringToSearch.localeCompare(searchString2)); // It will return 1
// Quiz. Using JavaScript implement an algorithm that validates a false values.
// http://www.asciitable.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment