Strings and numbers problems [closed]

1

I am participating in a application process to train as a web developer, I have doubts with these concepts:

  • Obtain a specific character from a string.
  • Get the index of a character of a string.
  • Convert a string to uppercase. Convert a string to lowercase.
  • Cutting a string.
  • Convert a string to a note number.
asked by Viviana Navarrete 22.07.2017 в 03:24
source

2 answers

1

Here I send you some examples that can help you

- Get a specific character from a string.

Use substr indicating the first parameter the position and the second parameter in 1 because it is a single character, example:

var cadena = "Cadena para buscar letras";

var posicion = 0        

cadena.substr(posicion, 1);   //retorna "C"

posicion = 3;        

cadena.substr(posicion, 1);   //retorna "e"
posicion = -3;

cadena.substr(posicion, 1);   //retorna "r"
posicion = -4

cadena.substr(posicion, 2);   //retorna "tr"

- Get the index of a character from a string.

Use indexOf as it is the search for a single character, for example:

var micadena = "Esta es mi cadena";

micadena.indexOf("E")    // retorna 0

micadena.indexOf("e")    // retorna 5

- Convert a string to uppercase. Convert a string to lowercase.

For uppercase letters toUpperCase () and for lowercase toLowerCase, for example:

var mivariable = "cadena en minuscula";

mivar.toUpperCase();   //retorna "CADENA EN MINUSCULA"

var otravariable = "Una cadena CUalQuiEra"

otravariable.toLowerCase();   //retorna "una cadena cualquiera"

- Cutting a string.

A string you cut with a split and you get its parts in an array (array)

var clarga = "cadena de texto que se cortara en varias palabras";

separador = " "; // un espacio en blanco

clarga.split(separador);   
//retorna (9) ["cadena", "de", "texto", "que", "se", "cortara", "en", "varias", "palabras"]

clarga.split(separador, 5); 
//retorna (5)["cadena", "de", "texto", "que", "se"]

- Convert a string to a note number.

Here I suppose you want to perform operations with strings that have only numbers. If so, you should use parseInt for integers and parseFloat for decimals, for example:

var UnaCadena = "100";

var OtraCadena = "200";

UnaCadena + OtraCadena;    //retorna "100200"

var UnNumero = parseInt(UnaCadena);

var OtroNumero = parseInt(OtraCadena);

UnNumero + OtroNumero;    //retorna "300"

var minumero = "10.56"

var miotronumero = "20.44"

minumero+miotronumero   //retorna "10.5620.44"

parseFloat(minumero) + parseFloat(miotronumero);  //retorna 31
    
answered by 22.07.2017 в 08:58
0

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

    
answered by 22.07.2017 в 12:29