how to divide a string by every 8 chars?

str = 'sdkidkfl dkfke dkdke dka dkela32566 dsa321434 -6=0df3 302kd903kdl'
then divide it , How to make it ?

 Accepted Answer

str = 'sdkidkfl dkfke dkdke dka dkela32566 dsa321434 -6=0df3 302kd903kdl'
allwords(str)
In the command window:
str =
sdkidkfl dkfke dkdke dka dkela32566 dsa321434 -6=0df3 302kd903kdl
ans =
'sdkidkfl' 'dkfke' 'dkdke' 'dka' 'dkela32566' 'dsa321434' '-6=0df3' '302kd903kdl'
If you want every 8 (or partial if there are not enough), then try this:
counter = 1;
for index = 1 : 8 : length(str)
lastIndex = min(index+7, length(str));
ca{counter} = str(index:lastIndex);
counter = counter + 1;
end
celldisp(ca)

5 Comments

thanks , i have an idea,reshape(str,8[])',how do u think?
Cedric
Cedric on 9 Jun 2014
Edited: Cedric on 9 Jun 2014
It depends what you mean by "divide". RESHAPE doesn't divide, and you still have to index the relevant row when you want to access a sub-string, e.g. strReshaped(k,:) for sub-string k. Image Analyst's solution divides the input string in chunks (of 8 chars), and stores each chunk in a cell array.
thank u ,it did help me, i have got what i wanted;
your string was not a multiple of 8 so reshape won't work.
The way I understood the question was that spaces were not separators, and that the OP really needed to extract segments of 8 chars.

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

on 9 Jun 2014

Commented:

on 9 Jun 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!