0
GEN000 AMA000 GaT000   

I only need to count the number of text without the spaces

1
  • 2
    What you tried so far? Are you attempting to solve this in any particular way? Commented Dec 27, 2013 at 16:56

3 Answers 3

5

Just as an alternative approach:

'GEN000 AMA000 GaT000'.match(/\S/g).length;  // 18

However, the fastest solution should always be a single for loop:

var str = 'GEN000 AMA000 GaT000',
    count = 0;

for (var i = 0, len = str.length; i < len; i++) {
    if (str[i] !== ' ')
        count++;
}
Sign up to request clarification or add additional context in comments.

Comments

4

var text = "GEN000 AMA000 GaT000";
var length = text.split(" ").join("").length;

console.log(length);

3 Comments

@katspaugh This answer is not exactly the same as yours.
@katspaugh My answer is in different place :)
@VisioN, I need more coffee :)
2

Try this simple solution,

alert(str.replace(/\s/g, "").length);

Example

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.