1

I have a string, say

var Str = 'My name is 123 and my name is 234'.

Now I split this as

var arrStr = Str.split(' ');

I iterate through the array and have different logic depending upon whether the word is a string or number. How do i check that? I tried typeof which didn't work for me.

EDIT:

After Seeing multiple answers. Now, I am in despair, which is the most efficient way?

4 Answers 4

6

If you care only about the numbers, then instead of using split you can use a regular expression like this:

var input = "My name is 123 and my name is 234";
var results = input.match(/\d+/g)

If you care about all pieces, then you can use another expression to find all non-space characters like this:

var input = "My name is 123 and my name is 234";
var results = input.match(/\S+/g)

Then iterate them one by one, and check if a given string is a number or not using the famous isNumeric() function posted by @CMS in this famous question.

function isNumber(n) {
    return !isNaN(parseFloat(n)) && isFinite(n);
}

NOTE: Thanks to @Pointy and if you want them as numbers, input.match(/\d+/g).map(Number).

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

7 Comments

... and if you want them as numbers, input.match(/\d+/g).map(Number)
Gotta love how 4 people upvoted this before you corrected it. Prior to your edit the result of this was simpy ["d"]. Good job on catching that yourself though.
@JamesDonnelly Yeah I noticed that, sometimes they just like the sound of it :)
@Sniffer : Some suggest unary + is faster than parseFloat() ?
@cipher Have you seen a benchmark about this, and by how much ? How much do you care for speed here. Anyway the isNumberic() method is taken from a famous question with a lot of discussions and opinions and the guys seem to be happy with it :).
|
2

You need to attempt to convert your array values to an integer.

To iterate them you can use a for loop:

for(i=0;i<arrStr.length;i++) {
    var result = !isNaN(+arrStr[i]) ? 'number' : 'string';
    console.log(result);
}

Here I'm using a unary + to attempt to convert the value of each array value to a number. If this fails, the result will be NaN. I'm then using JavaScript's isNaN() method to test if this value is NaN. If it isn't, then it's a number, otherwise it's a string.

The result of this using the string you've provided is:

string
string
string
number
string
string
string
string
number

To use this in an if statement, we can simply:

for(i=0;i<arrStr.length;i++) {
    if(isNaN(+arrStr[i])) {
        /* Process as a string... */
    }
    else {
        /* Process as a number... */
    }
}

JSFiddle demo.

4 Comments

I think the unary + does evil? typeof +"string" returns number
@cipher I fixed my answer, sorry about that!
I'm seeing you basically replaced Number(arrStr[i]) to +arrStr[i] , Am I Correct?
1

To expound on Sniffer's answer...

var input = "My name is 123 and my name is 234";
var numberArray = input.match(/\d+/g);
var wordArray = input.match(/[A-Za-z]+/g);

for (var number in numberArray)
{
    //do something
}
for (var word in wordArray)
{
    //do something
}

Comments

0

While researching, I found out about the Number() object. This is generally used to work with manipulation of numbers. MDN has a good documentation .

I found out that Number() returns NaN (Not a Number) when not passed a number. Since no number returns NaN, It could be a good way to check whether the passed object is string or a number literal.

So my code would be:

if (Number(arrStr[i]) == NaN){
    //string
} else  {
    //number
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.