How do i delete the spaces that are in the middle of a string array?
34 次查看(过去 30 天)
显示 更早的评论
str=' 123 456 ';
strtrim(str)
strfind('123 456',' ')
so far i know that using strtrim will remove the leading and trailing blanks in the string vector but how do i remove the space that is between 123 and 456? i can use strfind to help me know which element of the string vector is a blank space but how would i be able to delete the elements themselves?
0 个评论
回答(4 个)
the cyclist
2022-7-25
Here is one way:
str=' 123 456 ';
str(str==' ') = []
This method relies on the fact that the white space is a space character (and not some of the other whitespace characters).
0 个评论
Voss
2022-7-25
To delete elements from an array, set them to [] (or '').
% to removes spaces (' '):
str=' 123 456 ';
str(str == ' ') = []
% this also works:
str=' 123 456 ';
str(str == ' ') = ''
% to remove any whitespace:
str=' 123 456 ';
str(isstrprop(str,'wspace')) = []
0 个评论
Githin George
2022-7-26
Hi Sameer,
My understanding is that you would like to remove any and all multiple spaces from your string to single space and trim the start and end. You could to use the following code to get it done.
mystr = ' Some string one ';
% \s+ means multiple spaces
mystr = regexprep(mystr,'\s+',' ');
strtrim(mystr)
I hope it helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Characters and Strings 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!