ommiting blanks in a string

how would i write a code to print each line of the string array G omitting any trailing blanks and blanks in-between letters

回答 (4 件)

Thorsten
Thorsten 2013 年 2 月 11 日

0 投票

s = [' hallo a b c'; 'asdsdf v vvvv '];
for i = 1:size(s, 1)
disp(s(i, setdiff(1:length(s(i,:)), findstr(' ', s(i,:)))))
end
José-Luis
José-Luis 2013 年 2 月 11 日
編集済み: José-Luis 2013 年 2 月 11 日

0 投票

your_array = char(randi([32 127],100,10));
for ii = your_array'
sub_set = ii(ii ~= char(32))'
end
Image Analyst
Image Analyst 2013 年 2 月 11 日
編集済み: Image Analyst 2013 年 2 月 11 日

0 投票

Use strtrim(). From the help:
S = strtrim(str) returns a copy of string str with all leading and trailing white-space characters removed. A white-space character is one for which the isspace function returns logical 1 (true).
Code:
for k = 1:size(yourString, 1)
fprintf('%s', strtrim(yourString(k,:)));
end
Jan
Jan 2013 年 2 月 11 日

0 投票

This means, that you want to conserve the leading spaces, correctly?
Deleting all spaces would be:
s = strrep(s, ' ', ''); % Works for cell strings also
Trailing spaces:
s = deblank(s);
Conserve leading spaces - assuming that you have a cell string:
for iC = 1:numel(C)
S = C{iC};
first = find(isletter(S), 1);
C{iC} = [S(1:first - 1), strrep(S, ' ', '')];
end
And an equivalent approach work for a string also.

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

タグ

質問済み:

2013 年 2 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by