M
M
MadHowradBot2021-12-16 22:38:27
Node.js
MadHowradBot, 2021-12-16 22:38:27

How to check if the first character in a string is equal to minus or not equal?

How to check if the first character in a string is equal to minus or not equal?
Like if
const str starts with a minus const str = "-2214324141324"
or not

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey delphinpro, 2021-12-16
@MadHowradBot

Global_Objects/String
At least six methods allow you to do this

  1. Global_Objects/String/charAt
  2. Global_Objects/String/search
  3. Global_Objects/String/indexOf
  4. Global_Objects/String/startsWith
  5. Global_Objects/String/substring
  6. Global_Objects/String/match

K
Kirill Gorelov, 2021-12-16
@Kirill-Gorelov

Why not convert to a number and check for less than zero?

K
kapp1, 2021-12-18
@kapp1

You can check the first character.
str[0] == "-"
BUT this is a bad way, since it is not clear in what form the strings come to you, whether there are other characters in the first position.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question