user3340270
user3340270

Reputation: 187

Split word when meet new line and space

How do I split the paragraph in word by words which store in each array? I could only able to split the words when meeting the space between each word, but could not split it when meet the new line. The words will combine together when meeting new line (e.g.: the last word of 1st line combine together with the 1st word of second line)

word_newLine = regexp(CharData, '\n', 'split')

word = regexp(word_newLine, ' ', 'split')

the "CharData"

enter image description here

Upvotes: 0

Views: 664

Answers (1)

Bentoy13
Bentoy13

Reputation: 4966

With the function strsplit, you can split a string with multiple delimiters.

For example, you can split your paragraph in this manner:

words = strsplit(CharData, {' ','\n'});

EDIT

As stated in a comment, strsplit came with Matlab 2013. Before that version, one possibility is indeed to use regexp, in the following manner:

words = regexp(CharData,'\s+','split');

It splits the char array from white spaces (spaces, tabs, carriage return).

Upvotes: 2

Related Questions