-1

I'm going to create an edit options in a flash input text fields: I need live word count. How count the words that user is typing live?

5
  • RegEx is your friend. Match spaces not at the beginning or end of the string, get the length of the array, and add 1 (for the first word) to it. Easy enough.
    – Josh
    Aug 8, 2013 at 17:06
  • Why add 1? The length of the array is how many words there are.
    – Ribs
    Aug 8, 2013 at 17:08
  • @Ribs You're thinking of split, which might be the better route here (and why I upvoted your answer). With RegEx, we will count the spaces. This sentence has 9 words but only 8 spaces. So you have to add one to account for that difference.
    – Josh
    Aug 8, 2013 at 17:12
  • ah, I see what you mean now. Thx for the upvote. :) I like the regex idea tho too.
    – Ribs
    Aug 8, 2013 at 17:13
  • What is the 'cut and paste' bit you want?
    – putvande
    Aug 8, 2013 at 19:16

2 Answers 2

1

Try something like this, where we count groups of non-whitespace characters:

function countWords(input:String):int
{
    // Match collections of non-whitespace characters.
    return input.match(/[^\s]+/g).length;
}

Some tests:

trace(countWords("")); // 0
trace(countWords("Simple test.")); // 2
trace(countWords("  This  is an  untrimmed string ")); // 5
5
  • I want count the words live. Each space key pressed update the word counts. I don't know how do this.
    – Amirhosein
    Aug 9, 2013 at 7:37
  • @Amir You have to attach an event listener to the TextField for when the content changes and use my function inside that.
    – Marty
    Aug 9, 2013 at 7:41
  • If you mean 'myTextfield.addEventListener(Event.CHANGE,countWords);' it does't work.
    – Amirhosein
    Aug 9, 2013 at 7:52
  • @Amir No you have to make a handler function and use countWords within that.
    – Marty
    Aug 9, 2013 at 8:27
  • myTextfield.addEventListener(Event.CHANGE,count); function count(e:Event):void{ trace(countWords(myTextfield.text)); function countWords(input:String):int { return input.match(/[^\s]+/g).length; } }
    – Amirhosein
    Aug 9, 2013 at 10:18
1

To get the number of words in a text field, split the string in the textfield by the spaces. This will return an array of all the words in the textfield. Get the length of the array to tell how many words were entered:

var words:Array = myTextFieldInput.split( ' ' ); 
var numberOfWords = words.length;

As for copying text from a textfield and pasting it into another, as long as the textfield is selectable, that behavior should be native to the operating system.

2
  • Be careful of spaces next to each other, line breaks, etc. Also your naming of myTextField is a little misleading because you can't split a TextField.
    – Marty
    Aug 9, 2013 at 7:06
  • Thanks, but this count the white spaces as words.
    – Amirhosein
    Aug 9, 2013 at 9:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.