Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply backspace within a string

Tags:

string

matlab

I have a string which includes backspace. Displaying it to the commandline will 'apply' the backspaces such that each backspace and the non-backspace character which immediately precedes it cannot be seen:

>> tempStr = ['aaab', char(8)]
tempStr =
aaa

Yet the deletion operation operation only happens when displaying the string. It still has the backspace character, and the 'b', inside it:

>> length(tempStr)
ans =
 5

I'm looking for a minimal (ideally some sort of string processing built in) function which applies the backspace operation:

>>f(tempStr)
ans = 
 'aaa'

It may also help to know that I have an enumerations class over the alphabet 'a' to 'z' plus ' ' and backspace (to store my own personal indexing of the letters, images associated with each etc.). It'd be real spiffy to have this backspace removal operation be a method of the superclass that acts on a vector of its objects.

like image 913
eretmochelys Avatar asked Aug 06 '15 12:08

eretmochelys


People also ask

How do you backspace in a string?

The "#" key is a backspace character, which means it deletes the previous character in the string.

How do you backspace a string in C++?

The '#' represents a backspace. The task is to print a new string without '#'. Example: Input S=“Codee#SS#peee#dd#yy#“.


2 Answers

You can do it with a simple function using a while loop:

function s = printb(s)

while true

    % Find backspaces
    I = strfind(s, char(8));

    % Break condition
    if isempty(I), break; end

    % Remove elements
    if I(1)==1
        s = s(2:end);
    else
        s(I(1)-1:I(1)) = [];
    end

end

and the test gives:

s = [char(8) 'hahaha' char(8) char(8) '!'];

numel(s)                 % returns 10

z = printb(s)            % returns 'haha!'

numel(z)                 % returns 5

This is not really "minimal", but as far as my knowlegde goes I don't think this is feasible with regular expressions in Matlab.

Best,

like image 174
Ratbert Avatar answered Oct 16 '22 07:10

Ratbert


Your problem can be solved very elegantly using regular expressions:

function newStr = applyBackspaces(tempStr)
    newStr = tempStr;
    while (sum(newStr==char(8))>0)   % while there is at least one char(8) in newStr do:
        tmp    = newStr;             % work on previous result
        if (tmp(1) == char(8))       % if first character is char(8)
            newStr = tmp(2:end);     % then suppress first character
        else                         % else delete all characters just before a char(8)
            newStr = regexprep(tmp,[ '.' char(8)],'');  % as well as char(8) itself.
        end
    end
end

In essence, what my function does is delete the character just before the backspace until there are no more backspaces in your input string tempStr.

To test if it works, we check the output and the length of the string:

>> tempStr = ['abc', char(8), 'def', char(8), char(8), 'ghi']

tempStr =

abdghi

>> length(tempStr)

ans =

    12

>> applyBackspaces(tempStr)

ans =

abdghi

>> length(applyBackspaces(tempStr))

ans =

     6

Hence, tempStr and applyBackspaces(tempStr) show the same string, but applyBackspaces(tempStr) is the same length as the number of characters displayed.

like image 20
barceloco Avatar answered Oct 16 '22 07:10

barceloco