問題
讀取excel表格中的2個(gè)值并比較這2個(gè)值是否相等。
使用RichTextString的equals()方法來比較,發(fā)現(xiàn)明明2個(gè)值相等,結(jié)果還是返回false;debug了一下源碼。
//OK lets do this in stages to return a quickly, first check the actual string
boolean eq = ((field_1_charCount == other.field_1_charCount)
&& (field_2_optionflags == other.field_2_optionflags)
&& field_3_string.equals(other.field_3_string));
if (!eq) return false;
發(fā)現(xiàn)在比較的時(shí)候,field_2_optionflags == other.field_2_optionflags這兩個(gè)不相等。
方法上面還有一行注釋(大概意思就是富文本很難進(jìn)行比較)
/**
* Our handling of equals is inconsistent with compareTo. The trouble is because we don't truely understand
* rich text fields yet it's difficult to make a sound comparison.
*
* @param o The object to compare.
* @return true if the object is actually equal.
*/
說明富文本不單單比較的是內(nèi)容還比較格式類型。之前開發(fā)的時(shí)候沒有注意到這點(diǎn),結(jié)果測試的時(shí)候,測出來了。
最后還是用toString()方法,轉(zhuǎn)成字符串來進(jìn)行比較。