1

Say I have a string variable, " Fruit".

Usually, I use strtrim() to remove the leading whitespace. Therefore, " Fruit" becomes "Fruit".

However, if the leading whitespace is non-breaking, i.e. char(160), then strtrim, as well as its cousins - deblank and strtok - cannot remove this leading whitespace.

I also tried to use a for loop to replace " Fruit" with "Fruit" but the for loop doesn't seem to recognize " Fruit", which indicates I'm identifying it incorrectly.

Here is my for loop

for i=1:height(T)
    if T.Foods(i) == " Fruit"
        T.Foods(i) = "Fruit"
    end
end

What is one way I can remove this leading, non-breaking whitespace, or at least, replace it with a variable without whitespaces?

Wolfie
  • 27,562
  • 7
  • 28
  • 55
Andrew
  • 23
  • 1
  • 4
  • To compare strings use Matlab's `strcmp` function instead of `someString == otherString`. But it is better to use Wolfie's answer below. – JAC Jul 30 '21 at 15:12
  • 1
    @JAC for `'character arrays'` it's important to use `strcmp`, for `"strings"` using `==` is fine, although that is a tangent from this question – Wolfie Jul 30 '21 at 15:19
  • @Wolfie. You are right. I thought of `strcmp` because the question that starts "I have a string variable". and used double quotes, which Matlab takes as strings. However, at least in Matlab 2018b, the `==` does work with both strings and char arrays, and so does `strcmpi`. Thanks for the clarification. – JAC Jul 30 '21 at 15:30

1 Answers1

1

You can use regexprep to match the regular expression \s (any whitespace character) and replace with ''

>> str = [char(160), 'fru', char(160), 'it', char(160), char(160)]
str =
    ' fru it  '

>> regexprep( str, '\s', '' )
ans =
    'fruit'

As noted in the comments, to target only leading whitespace you would use

regexprep( str, '^\s+', '' )
Wolfie
  • 27,562
  • 7
  • 28
  • 55
  • 3
    To remove only leading space you need to anchor the space as `regexprep(str, '^\s', '')` where the caret `^` means the beginning of the string. If you want to remove multiple leading blanks, use the quantifier `+` as in `regexprep(str, '^\s+', '')`. The `+` means one or more of the preceding character. Note also that `\s` applies not only to a `char(160)` but also to tabs, returns, and new lines. – JAC Jul 30 '21 at 15:18