1

I need to find a way to take a sentence and remove all its words besides the first.

If the sentence is "Hi my name is dingo"
I need to get only the word "Hi"

hippietrail
  • 15,848
  • 18
  • 99
  • 158
Dan Naim
  • 161
  • 1
  • 3
  • 14

1 Answers1

3
var sentence : String = "Hi my name is dingo"
var words : Array = sentence.split( " " );
var firstWord : String = words[ 0 ];
trace( firstWord ) // outputs "Hi"

Obviously this only works for simple sentences without punctuation. If you need more complex word parsing you can use regexp:

var pattern : RegExp = new RegExp( "\\b.(\\w*).\\b",'gi' );
var words : Array = sentence.match( pattern ); 
Creynders
  • 4,573
  • 20
  • 21