Automatic Character deletion within string after detecting a special character

  • Follow


Suppose I have a string "Take me home, cas<<ountry rosd<<ad."
I want to write a script to detect each < as a cue for backspace, making the string read as follows: "Take me home, country road."
deleting the < as well as the preceding character. 
How can this be done? 
  
0
Reply Adrian 7/10/2010 2:15:08 AM

"Adrian " <bechaotik@gmail.com> wrote in message <i18l3c$k9o$1@fred.mathworks.com>...
> Suppose I have a string "Take me home, cas<<ountry rosd<<ad."
> I want to write a script to detect each < as a cue for backspace, making the string read as follows: "Take me home, country road."
> deleting the < as well as the preceding character. 
> How can this be done? 

One way:

function s = bsdel(s)
m = numel(s);
g = true(1,m);
n = 0;
for k=m:-1:1
    if( s(k) == '<' )
        n = n + 1;
        g(k) = false;
    elseif( n > 0 )
        g(k) = false;
        n = n - 1;
    end
end
s = s(g);
return
end


James Tursa
0
Reply James 7/10/2010 3:00:07 AM


"Adrian " <bechaotik@gmail.com> wrote in message <i18l3c$k9o$1@fred.mathworks.com>...
> Suppose I have a string "Take me home, cas<<ountry rosd<<ad."
> I want to write a script to detect each < as a cue for backspace, making the string read as follows: "Take me home, country road."
> deleting the < as well as the preceding character. 
> How can this be done? 
>   


http://www.mathworks.com/access/helpdesk/help/techdoc/ref/sscanf.html
   Not sure, but here's a start. I'm looking into it too...
                 Maxx
0
Reply Maxx 7/10/2010 3:01:03 AM

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <i18nnn$op7$1@fred.mathworks.com>...
> "Adrian " <bechaotik@gmail.com> wrote in message <i18l3c$k9o$1@fred.mathworks.com>...
> > Suppose I have a string "Take me home, cas<<ountry rosd<<ad."
> > I want to write a script to detect each < as a cue for backspace, making the string read as follows: "Take me home, country road."
> > deleting the < as well as the preceding character. 
> > How can this be done? 
> 
> One way:
> 
> function s = bsdel(s)
> m = numel(s);
> g = true(1,m);
> n = 0;
> for k=m:-1:1
>     if( s(k) == '<' )
>         n = n + 1;
>         g(k) = false;
>     elseif( n > 0 )
>         g(k) = false;
>         n = n - 1;
>     end
> end
> s = s(g);
> return
> end
> 
> 
> James Tursa

Hi James, I am new to Matlab. esp to Functions.
where do i insert the to be detected string in the script?
cheers
adrian
0
Reply Adrian 7/10/2010 3:51:04 AM

"Adrian " <bechaotik@gmail.com> wrote in message <i18qn8$k58$1@fred.mathworks.com>...
> "James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <i18nnn$op7$1@fred.mathworks.com>...
> > "Adrian " <bechaotik@gmail.com> wrote in message <i18l3c$k9o$1@fred.mathworks.com>...
> > > Suppose I have a string "Take me home, cas<<ountry rosd<<ad."
> > > I want to write a script to detect each < as a cue for backspace, making the string read as follows: "Take me home, country road."
> > > deleting the < as well as the preceding character. 
> > > How can this be done? 
> > 
> > One way:
> > 
> > function s = bsdel(s)
> > m = numel(s);
> > g = true(1,m);
> > n = 0;
> > for k=m:-1:1
> >     if( s(k) == '<' )
> >         n = n + 1;
> >         g(k) = false;
> >     elseif( n > 0 )
> >         g(k) = false;
> >         n = n - 1;
> >     end
> > end
> > s = s(g);
> > return
> > end
> > 
> > 
> > James Tursa
> 
> Hi James, I am new to Matlab. esp to Functions.
> where do i insert the to be detected string in the script?
> cheers
> adrian

The '<' is hard-coded per your request. If you want to specify the character to detect for backspace, just pass it in. e.g.,

function s = bsdel(s,c)
m = numel(s);
g = true(1,m);
n = 0;
for k=m:-1:1
    if( s(k) == c ) % c is the "backspace"
        n = n + 1;
        g(k) = false;
    elseif( n > 0 )
        g(k) = false;
        n = n - 1;
    end
end
s = s(g);
return
end


James Tursa
0
Reply James 7/10/2010 5:18:04 AM

"James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <i18vqc$mp2$1@fred.mathworks.com>...
> "Adrian " <bechaotik@gmail.com> wrote in message <i18qn8$k58$1@fred.mathworks.com>...
> > "James Tursa" <aclassyguy_with_a_k_not_a_c@hotmail.com> wrote in message <i18nnn$op7$1@fred.mathworks.com>...
> > > "Adrian " <bechaotik@gmail.com> wrote in message <i18l3c$k9o$1@fred.mathworks.com>...
> > > > Suppose I have a string "Take me home, cas<<ountry rosd<<ad."
> > > > I want to write a script to detect each < as a cue for backspace, making the string read as follows: "Take me home, country road."
> > > > deleting the < as well as the preceding character. 
> > > > How can this be done? 
> > > 
> > > One way:
> > > 
> > > function s = bsdel(s)
> > > m = numel(s);
> > > g = true(1,m);
> > > n = 0;
> > > for k=m:-1:1
> > >     if( s(k) == '<' )
> > >         n = n + 1;
> > >         g(k) = false;
> > >     elseif( n > 0 )
> > >         g(k) = false;
> > >         n = n - 1;
> > >     end
> > > end
> > > s = s(g);
> > > return
> > > end
> > > 
> > > 
> > > James Tursa
> > 
> > Hi James, I am new to Matlab. esp to Functions.
> > where do i insert the to be detected string in the script?
> > cheers
> > adrian
> 
> The '<' is hard-coded per your request. If you want to specify the character to detect for backspace, just pass it in. e.g.,
> 
> function s = bsdel(s,c)
> m = numel(s);
> g = true(1,m);
> n = 0;
> for k=m:-1:1
>     if( s(k) == c ) % c is the "backspace"
>         n = n + 1;
>         g(k) = false;
>     elseif( n > 0 )
>         g(k) = false;
>         n = n - 1;
>     end
> end
> s = s(g);
> return
> end
> 
> 
> James Tursa

If you have never used functions before, just put the above code in a file called bsdel.m and have that file somewhere on the MATLAB path. Then you can call it just like any other function. e.g.,

mystring = 'Take me home, cas<<ountry rosd<<ad.'
bscharacter ='<'
newstring = bsdel(mystring,bscharacter)

James Tursa
0
Reply James 7/10/2010 5:46:05 AM

Thanks, James. It works like a GEM!
0
Reply Adrian 7/10/2010 5:53:05 PM

"Adrian " <bechaotik@gmail.com> wrote in message <i18l3c$k9o$1@fred.mathworks.com>...
> Suppose I have a string "Take me home, cas<<ountry rosd<<ad."
> I want to write a script to detect each < as a cue for backspace, making the string read as follows: "Take me home, country road."
> deleting the < as well as the preceding character. 
> How can this be done? 
>   

one of the many solutions

     s='Take meX< home, cas<<ountry rosdXX<<<<ad.';
     ss='';
while   true
     ss=regexprep(s,'(\w{1,1}<)','');
if   isequal(ss,s)
     break;
end
     s=ss;
end
     disp(s);     % <- or SS...
%    Take me home, country road.

us
0
Reply us 7/10/2010 6:20:24 PM

Yet another:


% DATA
STR = 'Take me homeuuu<<<, cas<<ountry rosd<<ad.'

% ENGINE
[Y,J] = regexp(STR,'<+','start','end');
H = 2*(J - Y + 1);
Y = Y - H/2;
YJ = cumsum(H);
V = ones(1,YJ(end));
V(1) = Y(1);
V(1+YJ(1:end-1)) = Y(2:length(Y)) - J(1:length(Y)-1);
STR(cumsum(V)) = ''
0
Reply spamanon (2437) 7/10/2010 8:20:05 PM

Dear Adrian,

It is easy to pretend a solution:
  S = 'Take me home, cas<<ountry rosd<<ad.';
  S2 = strrep(S, '<', char(8));
  disp(S2)
>> Take me home, country road.

But the truth is revealed by inspecting S2:
  length(S2)
>> 35
So the backspaces (CHAR(8)) and the shadowed characters are still existing.

I didn't find a solution to read the cleaned printed string. Neither SPRINTF, nor EVALC(DISP) nor FPRINTF to a file or the command window worked - the CHAR8) are still there. I had success using the java.awt.robot simulating keyboard clicks, but this looks even more puzzling than Matt Figs solution ("as someone had rolled an angry armadillo over the keyboard").

Kind regards, Jan
0
Reply Jan 7/10/2010 9:20:21 PM

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <i1ao6l$msu$1@fred.mathworks.com>...
> Dear Adrian,
> 
> It is easy to pretend a solution:
>   S = 'Take me home, cas<<ountry rosd<<ad.';
>   S2 = strrep(S, '<', char(8));
>   disp(S2)
> >> Take me home, country road.
> 
> But the truth is revealed by inspecting S2:
>   length(S2)
> >> 35
> So the backspaces (CHAR(8)) and the shadowed characters are still existing.
> 
> I didn't find a solution to read the cleaned printed string. Neither SPRINTF, nor EVALC(DISP) nor FPRINTF to a file or the command window worked - the CHAR8) are still there. I had success using the java.awt.robot simulating keyboard clicks, but this looks even more puzzling than Matt Figs solution ("as someone had rolled an angry armadillo over the keyboard").
> 
> Kind regards, Jan

but... the above solutions (seems) to work...
% use BS char instead of '<'...

     s='Take meX< home, cas<<ountry rosdXX<<<<ad.';
     bs=char(8);
     s=strrep(s,'<',bs);
     ix1=strfind(s,bs);
     ss='';
while   true
     ss=regexprep(s,['(\w{1,1}',bs,')'],'');
if   isequal(ss,s)
     break;
end
     s=ss;
end
     ix2=strfind(s,bs);
     ix1
     ix2
%{
%    ix1 =
          9    20    21    35    36    37    38
%    ix2 =
          []
%}

us
0
Reply us 7/10/2010 10:03:04 PM

Dear us,

> but... the above solutions (seems) to work...
> while true
>      ss=regexprep(s,['(\w{1,1}',bs,')'],'');
> if   isequal(ss,s)
>      break;
> end
>      s=ss;
> end

Of course this works. And it is easy to understand, modify and debug (if it would have a bug).
I'm just disappointed, that the simple FPRINTF *can* perform the conversion when printing to the command line, but I cannot catch the result to a string again.

Jan
0
Reply Jan 7/10/2010 10:31:05 PM

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <i1asb9$h6$1@fred.mathworks.com>...
> Dear us,
> 
> > but... the above solutions (seems) to work...
> > while true
> >      ss=regexprep(s,['(\w{1,1}',bs,')'],'');
> > if   isequal(ss,s)
> >      break;
> > end
> >      s=ss;
> > end
> 
> Of course this works. And it is easy to understand, modify and debug (if it would have a bug).
> I'm just disappointed, that the simple FPRINTF *can* perform the conversion when printing to the command line, but I cannot catch the result to a string again.
> 
> Jan

but jan...
you simply cannot have if all:
- either germany wins a great(!) third...
- or ML provides a solution to this conundrum...

now, let's see what paul did: he/she/most likely it(!) voted for #1...

:-)
urs
0
Reply us 7/10/2010 10:44:06 PM

How about a drunk and angry armadillo with a hatred of multiple lines?



% DATA
STR = 'Take me homeuuu<<<, cas<<ountry rosd<<ad.'

% ENGINE
STR(cell2mat(cellfun(@(x)x(1)-diff(x)-1:x(2),regexp(STR,'(<)+','tokenExtents'),'Un',0))) = ''
0
Reply Matt 7/10/2010 10:56:05 PM

"Matt Fig" <spamanon@yahoo.com> wrote in message <i1atq4$r6k$1@fred.mathworks.com>...
> How about a drunk and angry armadillo with a hatred of multiple lines?

%{
> % DATA
> STR = 'Take me homeuuu<<<, cas<<ountry rosd<<ad.'
> % ENGINE
> STR(cell2mat(cellfun(@(x)x(1)-diff(x)-1:x(2),regexp(STR,'(<)+','tokenExtents'),'Un',0))) = ''
%}

clearly an armadillo about to loose its drivers license...

:-)
us
0
Reply us 7/11/2010 12:38:05 AM

Dear Matt, dear Us,

> How about a drunk and angry armadillo with a hatred of multiple lines?
>STR(cell2mat(cellfun(@(x)x(1)-diff(x)-1:x(2),regexp(STR,'(<)+','tokenExtents'),'Un',0))) = ''

I know this kind of programming:
Remove the keys from some old laptops, fill them from the wide side into a vuvuzela and shake it. Open the small hole and let one key after the other drop out. Repeat this until the line produces the wanted result (ask the COMBINATOR for the number of drinks you will need).

However, I like this solution, although it does not contain a CUMSUM, but at least CELLFUN and DIFF.
  'Un', 0 ))) = ''
What a finale.

Jan
0
Reply Jan 7/11/2010 12:42:05 AM

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <i1b40s$7tj$1@fred.mathworks.com>...
> Dear Matt, dear Us,
> 
> > How about a drunk and angry armadillo with a hatred of multiple lines?
> >STR(cell2mat(cellfun(@(x)x(1)-diff(x)-1:x(2),regexp(STR,'(<)+','tokenExtents'),'Un',0))) = ''
> 
> I know this kind of programming:
> Remove the keys from some old laptops, fill them from the wide side into a vuvuzela and shake it. Open the small hole and let one key after the other drop out. Repeat this until the line produces the wanted result (ask the COMBINATOR for the number of drinks you will need).
> 
> However, I like this solution, although it does not contain a CUMSUM, but at least CELLFUN and DIFF.
>   'Un', 0 ))) = ''
> What a finale.
> 
> Jan


And the best part is:   its self documenting ;-).  LOL
0
Reply Matt 7/11/2010 2:01:07 AM

16 Replies
223 Views

(page loaded in 0.428 seconds)

Similiar Articles:















7/29/2012 11:28:42 PM


Reply: