0

I have this inputs:

var str1 = "21";
var str2 = "1.";
var str3 = "5. test"
var str4 = "- something";

I want this outputs:

// 21
// 1
// 5
// 0

  • First case doesn't need to convert.
  • Second case need to use Number();.
  • Third case using .replace(/(\d)/g, "$1");.
  • But how can I do fourth case? Actually I want to replace a string with 0 if it isn't containing number.

My question seems easy, but that's 30min which I'm thinking about it and still I couldn't solve it.

stack
  • 10,280
  • 19
  • 65
  • 117
  • check if first element is number ... if not than assign 0 to it – Amar Singh Feb 01 '16 at 16:07
  • @YoYo It returns `NaN` – stack Feb 01 '16 at 16:08
  • check for first element for str4 only because 1. will also return NaN ,,,right – Amar Singh Feb 01 '16 at 16:10
  • @YoYo No, [`Number("1.");`](https://jsfiddle.net/hunyLezf/) returns `1`. – stack Feb 01 '16 at 16:12
  • 1
    [Please try this one](http://stackoverflow.com/questions/5778020/check-whether-an-input-string-contains-number) [Or this one](http://stackoverflow.com/questions/22100243/how-to-check-if-a-string-contains-a-number-in-javascript) – Yingy Feb 01 '16 at 16:14
  • @Yingy Thank you ... I wonder, How did you able to write a comment under my question (because you have just 1 rep) – stack Feb 01 '16 at 16:17

4 Answers4

2

isNaN() and parseInt()

var str1 = "21";
var str2 = "1.";
var str3 = "5. test"
var str4 = "- something";

console.log(isNaN(parseInt(str1)) ? 0 : parseInt(str1));
console.log(isNaN(parseInt(str2)) ? 0 : parseInt(str2));
console.log(isNaN(parseInt(str3)) ? 0 : parseInt(str3));
console.log(isNaN(parseInt(str4)) ? 0 : parseInt(str4));
Andrew Brooke
  • 12,073
  • 8
  • 39
  • 55
2

you can say if the number(yourString) = NaN then print 0

Nicky Mirfallah
  • 1,004
  • 4
  • 16
  • 38
  • `Number("5. test")` will also result in NaN. – krisk Feb 01 '16 at 16:20
  • in this case I recommend using charAt(0) to check if it has an integer at the beginning or not. if it did, you can check the next char (charAt(1)) until it returns something rather than a number or it ends. you can check if the returned char is a number or not by checking it's aasci code – Nicky Mirfallah Feb 01 '16 at 18:14
1
   var patt = /([0-9]+)/,
       match = patt.exec(str);
   if (!match) 
        str = 0;
   else
       str = match[1];

This will match first number from your string.

krisk
  • 169
  • 11
  • I want if that condition is `true`, then returns such a number ...! – stack Feb 01 '16 at 16:21
  • This code will print 0 if there is no number in your string. If I get your question. – krisk Feb 01 '16 at 16:22
  • 1
    Ok I got the whole point. You want to test all the conditions. I am adding an edit. – krisk Feb 01 '16 at 16:27
  • now try the edit. If you have to match more than one numbers in your string, you can run a loop. This one will bring first matched number from your string or will store 0 if there is no number in string. – krisk Feb 01 '16 at 16:35
  • 1
    Good `:-)`. Now it is a complete answer. – stack Feb 01 '16 at 16:37
1
var str4 = "- something"; 
str4 = ( str4.match(/\d+/g)) ? str4 : 0;
UserBSS1
  • 2,091
  • 1
  • 28
  • 31