How can I find a character in a string?
26 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Karis Anoruo
am 7 Jul. 2021
Kommentiert: Karis Anoruo
am 12 Jul. 2021
I am searching a textfile line by line for a particular character. Each line is being saved into a string format and then gets searched for the character. Written below is a section of the function I am using. None of the inbuilt string compare functions seem to be able to do this one task: find a character in a string and release the logical '1' if the character is there.
Am I missing a function? If not, how then do I do this please?
oneline = fgets(fid);
while ischar(oneline)
strncmp(character,oneline,strlength(oneline));
oneline = fgets(fid);
end
2 Kommentare
Stephen23
am 7 Jul. 2021
"Am I missing a function?"
The simplest one of all:
s = 'hello world';
x = s=='l'
Akzeptierte Antwort
Weitere Antworten (1)
Sulaymon Eshkabilov
am 7 Jul. 2021
Hi,
Here is one of the many possible solutions to your exercise:
clearvars
fid = fopen('TEXT__R.txt', 'r'); % Your text file
Letter = 'a'; % Looking for letter "a"
ii=1;
while 1
tline = fgetl(fid);
if ischar(tline)
fprintf('Searched line: %d \n', ii)
IDX =strfind(tline, Letter) % Index of "a" character location
N = numel(IDX);
fprintf('%5d "a" character was found \n', N) % Display the found results
ii=ii+1;
end
end
fclose(fid);
0 Kommentare
Siehe auch
Kategorien
Mehr zu Characters and Strings finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!