How can I remove the extra blank spaces from a text?

조회 수: 10 (최근 30일)
Mark
Mark 2022년 5월 3일
댓글: Walter Roberson 2022년 5월 3일
For example, I have the sentence ' I will go to the cinema' . I need this sentence to be written as 'I will go to the cinema'. I need to delete the extras spaces.

답변 (1개)

Geoff Hayes
Geoff Hayes 2022년 5월 3일
@Tiziano dos Santos - you could perhaps use strsplit to split the string on the space character and will create a cell array of strings (the words from your sentence). You could then use join to combine the words with a space between each one (may also need to trim the string first).
There is also probably a clever (better way to do this using) regexprep command that will do the same thing. I tried it and it wasn't too difficult to do.
  댓글 수: 2
Mark
Mark 2022년 5월 3일
How did you do it with regexprep?
Walter Roberson
Walter Roberson 2022년 5월 3일
S = ' I will go to the cinema'
S = ' I will go to the cinema'
newS = regexprep(strtrim(S), '\s+', ' ')
newS = 'I will go to the cinema'

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by