String Manipulation
Ältere Kommentare anzeigen
Hi All,
For example, I have filename called abcdefhijklm.xlsx and this file is displayed on the GUI. I wanted to remove the extension in the file name 'xlsx' and display only 'abcdefhijklm'. How can i manipulate this string.
Thanks in Advance Yaseen.
Akzeptierte Antwort
Weitere Antworten (1)
filename_with_extension = "abcdefhijklm.xlsx";
filename_without_extension = extractBefore(filename_with_extension, ".")
2 Kommentare
This is risky advice. The fileparts function is designed to separate the path, file name, and extension. I don't know what edge cases there are, but let me try the first one that comes to mind:
filename_with_extension = "filename_with_a.dot_in_it.xlsx";
filename_without_extension = extractBefore(filename_with_extension, ".")
Exceptions like this is why it is safer to stick to functions that have been debugged for two decades by Mathworks.
I totally agree with you, to be sure of the result regardless of the filename content, fileparts is the way to go.
I just wanted to mention this option if we know the filename content won't cause any issue, and because we can use directly the filename without extension, which in some cases can save us an intermediate line of code (I am not saying this is the thing to do, I just wanted to mention that this possibility exists).
filename_with_extension = "abcdefhijklm.xlsx";
% Display filename in one line
disp(extractBefore(filename_with_extension, "."))
% Display filename in two lines
[pathstr, name, ext] = fileparts(filename_with_extension);
disp(name)
Kategorien
Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!