Hello, I have a text file and I want to see how much 'Hi there!' word inside of it. How can I do it? Thank you.

2 Kommentare

Walter Roberson
Walter Roberson am 3 Jun. 2022
What do you want to have happen if one of the occurrences is "PHi there!"? or "hi there!"?
tinkyminky93
tinkyminky93 am 3 Jun. 2022
Phi there is not meaningful and it is impossible to found in my text file. But it is a good point.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Image Analyst
Image Analyst am 3 Jun. 2022
Bearbeitet: Image Analyst am 3 Jun. 2022

0 Stimmen

Did you try
fileChars = fileread(fileName);
locations = strfind(fileChars, 'Hi there!')
numLocations = numel(locations)

1 Kommentar

Walter Roberson
Walter Roberson am 3 Jun. 2022
If you have "hi there" with lower case H, and you are searching for "Hi there!' with upper case H and with ! then the count should be 0.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (2)

VINAYAK LUHA
VINAYAK LUHA am 3 Jun. 2022
Bearbeitet: VINAYAK LUHA am 3 Jun. 2022

0 Stimmen

data = fileread("content.txt")
pattern="hi there!"
substr_freq =count(data,pattern)
%substr_freq has your answer

1 Kommentar

tinkyminky93
tinkyminky93 am 3 Jun. 2022
Bearbeitet: tinkyminky93 am 3 Jun. 2022
Search term must be a text or pattern array, error using count

Melden Sie sich an, um zu kommentieren.

tinkyminky93
tinkyminky93 am 3 Jun. 2022
Bearbeitet: tinkyminky93 am 3 Jun. 2022

0 Stimmen

you are right sir @Walter Roberson, I got the point.

Kategorien

Mehr zu Characters and Strings finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by