Including string from a position

Asked

Viewed 50 times

1

I’m racking my brain with an item that may be even simple to do but after twelve hours of work the mind doesn’t help... rsrsrsrs

Friends, I have a stirng query, for example:

&requiredfields=(Vencedor:vencedor).(Categoria:competitividade|Categoria:produtividade)

Where I need to include a new item to it in a specific position. Check this out:

&requiredfields=(Vencedor:vencedor).(Categoria:jovem parceiro|Categoria:competitividade|Categoria:produtividade)

Note that includes Categoria:jovem parceiro in the query string at the beginning of the parentheses and made a separation by |. The position where this character starts in specific I already have, to better contextualize, when items have the same key, they should be within the same parentheses separated by |, thus the position from which it begins - in this example - Categoria I already have. I need to move on.

They could help me with this problem. I need to do this with Javascript. Thanks in advance!!!

1 answer

1


If you already have the position and want to insert the text2 inside the text1, at position X, you can use:

var texto1 = "&requiredfields=(Vencedor:vencedor).(Categoria:competitividade|Categoria:produtividade)"
var texto2 = "Categoria:jovem parceiro|"
var posicao = 37; //Coloque aqui a posição que você já tem
var novotexto = [texto1.slice(0, posicao), texto2, texto1.slice(posicao)].join(''); 
  • Perfect! Thank you very much Icaro.

Browser other questions tagged

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