delete special characters (\ / : * " < > |) in char

86 vues (au cours des 30 derniers jours)
Alberto Acri
Alberto Acri le 25 Juin 2023
Commenté : Alberto Acri le 26 Juin 2023
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: \ / : * " < > |

Réponse acceptée

Matt J
Matt J le 25 Juin 2023
One way:
name_1 = '<Hello World>';
pat=num2cell('\/:*"<>|');
name_1=erase(name_1,pat)
name_1 = 'Hello World'

Plus de réponses (3)

Matt J
Matt J le 25 Juin 2023
Modifié(e) : Matt J le 25 Juin 2023
One way:
name_1 = '<Hello World>';
tf=ismember(name_1, '\/:*"<>|');
name_1(tf)=''
name_1 = 'Hello World'
  1 commentaire
Alberto Acri
Alberto Acri le 26 Juin 2023
Thank you for your reply. It is as good as the others' answer.

Connectez-vous pour commenter.


Jan
Jan le 25 Juin 2023
name_1 = '<Hello World>';
name_1 = erase(name_1, ["<", ">", "\", "/", ":", "*", """", "|"])
name_1 = 'Hello World'
  1 commentaire
Alberto Acri
Alberto Acri le 26 Juin 2023
Thank you for your reply. It is as good as the others' answer.

Connectez-vous pour commenter.


DGM
DGM le 25 Juin 2023
Modifié(e) : DGM le 25 Juin 2023
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,'[\\/:*"<>|]*','')
names = 3×1 cell array
{'Hello World'} {'File numb 5'} {'123456789' }
  1 commentaire
Alberto Acri
Alberto Acri le 26 Juin 2023
Thank you for your reply. It is as good as the others' answer.

Connectez-vous pour commenter.

Catégories

En savoir plus sur MATLAB dans Help Center et File Exchange

Produits


Version

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by