5
I would like a Javascript method that takes part of a String up to a certain point (character) from it. In this case I would specify a character and only be taken part of the String until the character I specified.
- I wouldn’t want to use the methods
substring
,indexOf
or thesplit
. Because they require me to do this in more than one line of code (using array or indexes) and, in my specific problem, it needs to be done in a single line.
Example of what I would need:
let frase = "99 troços";
//Eu precisaria disso (que está abaixo) só que sem utilizar outras linhas de código antes da variável armazenarValor.
let armazenarValor = "99"; //Ignoraria o espaço e o 'troços'.
//Eu precisaria que ficasse algo assim
let armazenarValor = pegarAteEspaco(frase, " ");
//Imaginemos que pegarAteEspaco() fosse um método JavaScript que pegasse uma String até determinado caracter.
//Eu só gostaria de pegar o "99" e ignorar o resto.
- I couldn’t do a function for that either.
- In PHP there are methods that do this. But I would like to do in JS.
- I saw some things here at the Stack Overflow, but most are using
substring
,split
orindexOf
. - Maybe there isn’t something native to JS made exactly for this.
Without these functions you need a loop, which complicates solving in a row (weirder requirement!) Using substring + index you can do in a row...
– bfavaretto
Thank you, bfavareto. The question of a regular expression, quoted in the reply of Luiz Felipe, seemed to me very interesting. But I don’t think it would be in a single line either. I actually don’t think there’s anything native to Javascript that does this.
– Gato de Schrödinger
But why not just
let trecho = frase.substring(0, frase.indexOf(' '))
?– bfavaretto
Soon I will test your answer correctly, Luiz Felipe. Working out, I give the vote and accept her ;D
– Gato de Schrödinger
Complementing @bfavaretto, you can also do
let valor = frase.split(' ')[0]
. That’s just a line...– hkotsubo
maybe it will also work, @bfavaretto. I will test in a little while and return to you the result.
– Gato de Schrödinger
Ah, I get it... You’re running this code on one of these courses/challenges sites, right? Only they put these absurd requirements, how to solve...
– bfavaretto
When writing code in "one line," you will often be giving up code readability for a factor that rarely "matters" in the long run. Readability is usually much more important than the number of lines. I think it’s worth reviewing the need to do in "one line"... :)
– Luiz Felipe
Worse than not, @bfavaretto. It’s a necessity you painted here at work. I’m using Chart Js and painted some bullshit here that I had to do to solve the problem.
– Gato de Schrödinger
@Luizfelipe, I don’t usually do this. It was the need even. It’s actually the first time I do this.
– Gato de Schrödinger
Worse than now they’ve had a lot of answers they might solve. I will give preference to those who responded first to those that I can really understand. I don’t know how to use regular expression, but @Luizfelipe’s reply was the first.
– Gato de Schrödinger
You must accept the one that best solved the problem for you, regardless of the order :-)
– hkotsubo
So the reply from @hkotsubo served me nicely, I even tried to add a few numbers after space but really only came out the 99. But I found this question of parseint() somewhat sinister for such a situation. Luiz Felipe’s answer also served me well. But I know nothing about regular expression (And using something in the code you don’t understand is kind of boring) and I don’t know if it could open up any loopholes in my case. The two helped me a lot. I am in doubt which answer to accept. Any of the choices, would be unfair to the other.
– Gato de Schrödinger
It’s up to you to choose any of them. In your case, it’s interesting to read this discussion: https://pt.meta.stackoverflow.com/q/1540/112052 - particularly, I don’t think it would be "unfair" to me since Felipe’s other response was also good (and this use of
parseInt
I don’t like, I don’t know, I prefer not to depend on these "magic"). Anyway, you decide :-)– hkotsubo
Dilemma, man. I don’t think parseint is the right way for this question of mine, but it worked. The regular expression worked, but I understood nothing and I do not know if other users, half laypeople like me, would understand too. Have you seen that meme where the guy gets in doubt between two buttons to press ? That’s how I am now. Rs
– Gato de Schrödinger