0

I have this string 4-1111

How to extract the 4 only?. I mean any character/s before the ( - ).

2
  • have you tried using a regex? Commented Feb 25, 2021 at 17:04
  • Could you clarify why did you tag the question with TypeScript - do you want to create a type to which only strings starting with digits before a dash are assignable? Commented Feb 25, 2021 at 17:07

2 Answers 2

1

Using split following should work :-

let string = "44234-1111"
console.log(string.split('-')[0]);

.split returns ['44234','1111'] and you can select the index 0 entry.

Sign up to request clarification or add additional context in comments.

Comments

1

Split would work

'4-1111'.split('-')[0];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.