Well, I'll help you understand the concepts you're trying to clarify in your training as a web developer
First of all we must understand that it is a String before handling it, a String (text string) is nothing more than a Array of characters , example:
//Se pueden declarar tanto con dobles comillas (" "), comillas simples (' ') o template strings (' ')
let miNombre = "Daniel"
let tuNombre = 'Viviana'
let persona = 'Random'
If you have not seen the arrays or vectors , they are simply a structure of data which we access through numerical indexes (the most basic) . To declare an array we use the brackets [] and separate the internal data by comas
// Por ejemplo nuestras variables declaradas anteriormente se pueden interpretar así:
["D","a","n","i","e","l"]
["V","i","v","i","a","n","a"]
Access a specific character
If we want to access a character of our String we can do it through its numerical indexes as I indicated before, if we understand that in programming the indexes start to count from zero we can deduce that:
tuNombre[0] //Accedemos a "V"
tuNombre[3] // Accedemos a la segunda "i"
let primeraLetra = tuNombre[0] // Podemos almacenar esos caracteres en otras variables
console.log(primeraLetra) // Tendremos un output de "V"
Following this logic we can access a specific character of our Text Chain and perform the actions we want.
Get the index of a character from a String
For this I understand that you want to know the numerical index of the array where the character you want to find is located.
Sure you have heard or read that everything in Javascript is an object , well, a String is also interpreted as an object and thanks to that we have methods and properties that They allow you to treat data types very effectively. Imagine as if your String was encapsulated in a sphere which allows you to access special features.
One of these methods is called indexOf () , its syntax is as follows:
cadena.indexOf(valorBusqueda[, indiceDesde])
// Si queremos acceder por ejemplo a nuestra "V"
tuNombre.indexOf("V", 0) //Le decimos que busque la "V" a partir del indice cero
/* Nos retornará el indice de la segunda "i" ya que se salta la primera
al empezar nosotros a contar desde el indice 2 */
tuNombre.indexOf("i", 2) // Retorna índice 3
Convert a String to uppercase and lowercase
If you understand that everything in Javascript is an object then we can treat them with special methods, this section will be easier because we have two methods that do everything for us that are:
String.toLowerCase() y String.toUpperCase()
//Ejemplo
let nombreMayus = "ENRIQUE"
nombreMayus.toLowerCase() // --> nombreMayus = "enrique"
let nombreMinus = "carla"
nombreMinus.toUpperCase() // --> nombreMinus = "CARLA"
Cutting a string
For this we have another method, which is called slice () , extracts the text from a string and returns a new string. Changes in the text of one string do not affect the other string.
That is, if we want to cut a string, we have to save it in another variable
String.slice()
let tuNombre = "Viviana"
let nombreCortado = tuNombre.splice(3,6) // cortara "ana"
console.log(nombreCortado) // Tendrás un output de "Vivi"
It is important to emphasize that the first index that we passed to slice () does not include it, that is, it would start cutting from 4, instead the > second index if it includes it, with which we are cutting from index 4 to 6 which is the string "ana"
Convert a String to a Number
This part of the casting has many details and I recommend you to look at it more closely but the basics would be to use parseInt () to a string that is expressed as a whole number (ie they do not count decimals, for that we have parseFloat ())
//Ejemplos
parseInt("3") // Retornara un 3 de tipo number
parseInt("32") // Retornara un 32 de tipo number
parseInt("100") // Retornara un 100 de tipo number
parseInt("654") // Retornara un 654 de tipo number
I hope it has helped you to take another step in your way, I recommend that whenever you have any questions, I am sure that in the Mozilla developer network you will find the information necessary to move forward .
Developers mozilla network