How to extract only alphabets from mixed string
Afficher commentaires plus anciens
Hi,
I have a mixed string as below:
string=BA9K90L
And I only want to extract alphabets i.e., BAKL. Kindly some one help how can we do this.
Many thanks in advance,
Réponse acceptée
Plus de réponses (1)
Hi
You can use fancy functions like regexp or so, but this is my suggestion:
str = 'BA9K90L';
% pick out letters only (using ASCII value)
str = str(double(str) > 64)
Done! :)
Cheers
EDIT: This does not work if you have more than alphanumeric strings, see Stephen's answer for a complete check.
5 commentaires
This method actually returns any characters above 64:
>> str = '123ABC[]~D4';
>> str(double(str) > 64)
ans =
ABC[]~D
See my answer for the simple correct method that returns only alphabetic characters, as the OP requested.
"does the trick, is transparent"
Not in the industry that I work in. If the code should return the alphabetic characters, then this should be obvious from the code. The code double(str)>64 would not be considered to be "transparent" and reviewable by our external assessors (let alone that it would fail the requirements tests). Is "alphabetic" anywhere to be seen in this code? What is the magic number?
The intent of the code should be discernible by the code itself. Then that would be transparent.
"and faster on top of that"
We also save more time by writing correct code, than by using little "tricks" (and debugging them later). More time is spent writing and reviewing this code than will be spent running it, so the clearer its intent is the better (see above point). Our guidelines tell us to pick the solution that uses an existing, well tested function or method (see my answer) rather than improvising something new each time it is required. This also allows for bug fixing and efficiency improvements to occur in one location.
These are not my opinions, but are standards in many areas of industry.
Guillaume
le 26 Mar 2016
I totally agree with Stephen. Correct and obvious code trumps fast code. While comparing to 'A' (and that would have been clearer than comparing to 64) works in the toy example provided, it certainly is not robust.
There are tons of characters (>120,000 in the unicode standard) above 64 that may or may not be alphabetic. isstrprop at least attempts to do the right thing and takes into account the current locale.
@Guillaume:
Of course. I admit that I understood the question as "if I have a string with letters and numbers, how to I extract the letters", as this was given in the example. I have no problem admitting the correctness of his solution, and it was good to point out the limitation. That being said, I see this forum as a place where people share ideas and solutions, and not one of industrial competition. Even Dr. Siva's answer, while being unnecessarily long, is a contribution. It's up to the OP to decide which answer suits their need.
Anyway, that is irrelevant for the topic at hand.
Catégories
En savoir plus sur Characters and Strings dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!