Check if it contains characters in the string

Asked

Viewed 414 times

-1

When I use teste.length it returns me the size, but how do I check if it contains no character?

For example:

var teste = (como se o usuário tivesse clicado **2x** no **enter/espaço**); 

Soon teste.length will return me 2, but in reality does not contain any character.

I wish that when I had nothing, I would return 0.

  • 2

    Your concept is wrong, if it contains no characters the size is 0. It has spaces so it has characters. If you don’t want to count certain types of characters you would have to define them very well. It is even possible for the user to paste something that has no visible characters and is not space. So what? for me or define the right problem and find a real solution or get gambiarra anyway.

  • I think the colleague just expressed himself badly, he meant that for his motivation, blank space would not be a valid character. But you’re right, space is a valid character.

  • 2

    @David just needs to express exactly to have an exact result. As a rule of thumb, there is ambiguity until we talk about space. In general people don’t understand what they don’t see, and non-printable characters are valid and not usually interesting, not just space. Anyway do there a say fault and the person does not know why. Conceptualize everything right is important to do right.

2 answers

3


Use the trim().

stringExemplo = stringExemplo.trim(); it removes whitespace. You can use the str.replace also.

  • 1

    the scolding of replace would it also alter the internal spaces of the string (I don’t know if that’s the case).

2

Use the trim(), so spaces will be disregarded at the beginning and end of the string.

var teste = string_usada.trim();

Browser other questions tagged

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