Sanitize string for comparison in Matlab

非 Y 不嫁゛ 提交于 2019-12-12 00:56:35

问题


This is a follow-up question from this that considered evalc, instead of figgling with file-descriptors manually. You can see below an example about poor sanitization. I want to remove things such as trailing characters, all whitespaces, all newlines etc -- that usually cause unexpected things -- is there a ready sanitization command to do this?

EDU>> a

a =

1 +1*{x} -1*{y}*{z}


EDU>> b

b =

1 +1*{x} -1*{y}*{z}

EDU>> isequal(a,b)

ans =

     0

回答1:


I don't know whether there exist any ready robust implementation but this works pretty well

xx=@(x)regexprep(x,'\s',''); isequal(xx(a),xx(b))

where I use anonymous function and remove some oddities such as trailing whitespaces/newlines often hard to see on the window.

Also, the commands such as strtrim() and deblank() can be useful to you in removing trailing characters.



来源:https://stackoverflow.com/questions/15991945/sanitize-string-for-comparison-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!