Use of substring for each type of situation

Asked

Viewed 96 times

1

I own a variable with the following information.

var dataEvento = "SEXTA-FEIRA 25 JAN 20H00";

I need to create 3 variables different, one variable for 25(Day), JAN(Month) and 20H00(Opening hours).So far I’ve been able to extract the time using this function(Let me know if it is incorrect).

var horario = dataEvento.substring(dataEvento.lastIndexOf(' ')+1); 
this.horario = horario;

How to create a function equal to the above for the day and month?

  • 3

    That takes care of your problem: var r = dataEvento.split(' ') then just recover by the indexes: this.data = r[1]; this.mes = r[2]; this.horario = r[3];

2 answers

7


You can use the method String.split() that "breaks" a string into multiple pieces (an array with N elements), just define which will be the separator.

Ex:

"abc 123 def 456".split(' ');  // ['abc', '123', 'def', '456']

In your case, if you applied the above method we would have:

"SEXTA-FEIRA 25 JAN 20H00".split(' ');
// ['SEXTA-FEIRA', '25', 'JAN', '20H00']

From this it is only create the desired variables and ignore the first element.

let result = ['SEXTA-FEIRA', '25', 'JAN', '20H00']
dia = result[1]
mes = result[2]
horario = result[3]

If compatibility is not an issue, you can also use destructuring assignment of Ecmascript2015:

let [_, dia, mes, horario] = ['SEXTA-FEIRA', '25', 'JAN', '20H00']

Putting it all together:

const extractData = stringEvento => {
    let [_, dia, mes, hora] = stringEvento.split(' ')
    return {dia, mes, hora}
}

Full example:

const extractData = stringEvento => {
    let [_, dia, mes, hora] = stringEvento.split(' ')
    return {dia, mes, hora}
}

let sessoes = [
    {
        codEvento: 29,
        descricao: "SEXTA-FEIRA 25 JAN 20H00",
        atual: true
    }, {
    	codEvento: 30, 
        descricao: "SÁBADO 26 JAN 21H00", 
        atual: false
    }, {
    	codEvento: 31, 
        descricao: "DOMINGO 27 JAN 20H00", 
        atual: false
    }
]

// Atualizar sessoes com a data alterada
sessoes = sessoes.map(evento => {
    evento.data = extractData(evento.descricao)
    return evento
})

console.log(sessoes)

  • 1

    Do not change anything, you just need to prepare your code first. I will make an example and send you the link. Wait a minute

  • I misasked the question, I get this array in case, not as ['SEXTA-FEIRA', '25', 'JAN', '20H00'] , but as in this example https://imgur.com/a/GkZqw7r

  • 1

    You can use a for or Array.map() to apply the logic to each of the array elements. Here I made an example that traverses the array and creates a new property data within each element of the array with the converted value.

  • 1

    I updated the question with an example using arrays

  • Working perfectly, great examples!

6

You can use the .split() like the fernandosavio also suggested:

const [, dia, mes, hora] = 'SEXTA-FEIRA 25 JAN 20H00'.split(' ');
console.log(dia); // 25
console.log(mes); // JAN
console.log(hora); // 20H00

Or you can use a Regexp to extract the result:

const regExp = /[^\d]+(\d+)\s([a-z]+)\s(\d+H\d+)/i;
const string = 'SEXTA-FEIRA 25 JAN 20H00';
const [, dia, mes, hora] = string.match(regExp);
console.log(dia); // 25
console.log(mes); // JAN
console.log(hora); // 20H00

The Regexp explained would be:

  • [^\d]+ - any character other than a number, 1 or more times
  • (\d+) - a number, once or more, with capture (to be used later)
  • \s - blank space
  • ([a-z]+) - one letter, once or more times, with catch
  • /i - large and small print

Browser other questions tagged

You are not signed in. Login or sign up in order to post.