delete special characters (\ / : * " < > |) in char
117 次查看(过去 30 天)
显示 更早的评论
Hi. I need to transform these lines:
name_1 = '<Hello World>';
name_2 ='File numb: 5';
into these:
name_1 = 'Hello World';
name_2 ='File numb 5';
In general, I would like to delete, if they are present, within the name all 8 symbols: \ / : * " < > |
0 个评论
采纳的回答
Matt J
2023-6-25
One way:
name_1 = '<Hello World>';
pat=num2cell('\/:*"<>|');
name_1=erase(name_1,pat)
0 个评论
更多回答(3 个)
Jan
2023-6-25
name_1 = '<Hello World>';
name_1 = erase(name_1, ["<", ">", "\", "/", ":", "*", """", "|"])
DGM
2023-6-25
编辑:DGM
2023-6-25
You could also use regexprep(), though perhaps using erase() is more convenient.
% using numbered variables only makes processing more difficult
% instead of embedding implicit indices in the names, just use an array
names = {'<Hello World>';
'File numb: 5';
'1\|2/>3:<4*"5"*6<:7>/8|\9'};
% replace any instance of the listed characters with ''
names = regexprep(names,'[\\/:*"<>|]*','')
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!