delete an element in char array without deleting the duplicated elements before it
Ältere Kommentare anzeigen
if I have a char array ex: 'drinking tea' and I padded this string with 'ab' till its size becomes 16 but now when I want to delete the padded characters the 'a' at the end also deleted how can I delete the padded elements without affecting the original string
Antworten (2)
Star Strider
am 11 Nov. 2017
One approach:
str = 'drinking tea';
stra = [str 'ab' 'ab'];
idx = strfind(stra, 'ab');
recovered_str = stra(1:idx(1)-1)
5 Kommentare
mia saam
am 11 Nov. 2017
Star Strider
am 11 Nov. 2017
When I ran this code:
str = 'drinking tea';
stra = [str 'ab' 'ab' 'a'];
idx = strfind(stra, 'ab');
recovered_str = stra(1:idx(1)-1)
my code still worked correctly.
mia saam
am 11 Nov. 2017
Bearbeitet: per isakson
am 11 Nov. 2017
Star Strider
am 11 Nov. 2017
I am simply addressing the Question you posed. I cannot guarantee that my code is robust to all problems.
I will delete my Answer in a few hours.
mia saam
am 11 Nov. 2017
Walter Roberson
am 11 Nov. 2017
0 Stimmen
No, this is not possible to do without information about the intended size of each of the strings. As long as you pad with a pattern that is also valid potential input, you are going to run into problems if the valid input happens to end with that pattern. If you pad with 'ab' pairs then run into problems with (for example) 'nice grab' .
To get around this you either need to keep track of the length of each string, or you need to pad with something that is defined as not being valid input. For example you might be able to get away with padding with char(65535)
1 Kommentar
mia saam
am 11 Nov. 2017
Kategorien
Mehr zu Data Type Conversion finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!