Alex Rivera | Logout

Remove everything after space in string

Asked 2012-02-16T21:20:29.527
31

I would like to remove everything after a space in a string.

For example:

"my string is sad"

should return

"my"

I've been trying to figure out how to do this using sub/gsub but have been unsuccessful so far.

Edit
Report

1 Answer

12

or, substitute everything behind the first space to nothing:

gsub(' [A-z ]*', '' , 'my string is sad')

And with numbers:

gsub('([0-9]+) .*', '\\1', c('c123123123 0320.1'))
answered 2012-02-16T22:06:32.073

Your Answer