Thread Subject: easy string manipolation

Subject: easy string manipolation

From: gigio

Date: 16 Jun, 2005 17:20:34

Message: 1 of 3

Sorry for the easy question...

I have some strings like these
dummy='a, b'
dummy='a ,b'
dummy='a b'
dummy='a,b'

first and second terms are separated by space OR/AND comma
I wish to separate first adn second term of the string

This is my solution
----
[first,second]=strtok(dummy,' ,')
idx=find (second==',' |isspace(second) );
second(idx)=[];

first
second
----
It works, but it is not easy to generalize for string with 'n' terms
Any hint ??

Thank you very much!!

Subject: easy string manipolation

From: quo

Date: 16 Jun, 2005 16:05:10

Message: 2 of 3

gigio wrote:
>
>
> Sorry for the easy question...
>
> I have some strings like these
> dummy='a, b'
> dummy='a ,b'
> dummy='a b'
> dummy='a,b'
>
> first and second terms are separated by space OR/AND comma
> I wish to separate first adn second term of the string
>
> This is my solution
> ----
> [first,second]=strtok(dummy,' ,')
> idx=find (second==',' |isspace(second) );
> second(idx)=[];
>
> first
> second
> ----
> It works, but it is not easy to generalize for string with 'n'
> terms
> Any hint ??
>
> Thank you very much!!
>

well, this is messy and regexps would probably work well here, but...

rema = strrep(dummy,',',' ');
cnt = 0;
while ~isempty(rema)
cnt = cnt+1;
[element{cnt}, rema] = strtok(rema,' ');
end

hth
-quo

Subject: easy string manipolation

From: gigio

Date: 16 Jun, 2005 20:13:47

Message: 3 of 3

On Thu, 16 Jun 2005 16:05:10 -0400, quo <none@email.com> wrote:

>gigio wrote:
>>
>
>rema = strrep(dummy,',',' ');
>cnt = 0;
>while ~isempty(rema)
>cnt = cnt+1;
>[element{cnt}, rema] = strtok(rema,' ');
>end
>
>hth
>-quo

OK! Thanks!

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com