假设您有以下字符串:
String s = "The cold hand reaches for the %1$s %2$s Ellesse's";
String old = "old";
String tan = "tan";
String formatted = String.format(s,old,tan); //"The cold hand reaches for the old tan Ellesse's"
假设您想以该字符串结束,但也有一个特定的Span https://developer.android.com/reference/android/text/Spannable.html为任何替换为的单词设置String.format
.
例如,我们还想执行以下操作:
Spannable spannable = new SpannableString(formatted);
spannable.setSpan(new StrikethroughSpan(), oldStart, oldStart+old.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new ForegroundColorSpan(Color.BLUE), tanStart, tanStart+tan.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
有没有一种可靠的方法来了解开始索引old
and tan
?
请注意,仅搜索“old”会返回“cold”中的“old”,因此这是行不通的。
What will我想,工作就是寻找%[0-9]$s
事先,并计算偏移量以考虑替换String.format
。不过,这似乎很令人头疼,我怀疑可能有类似的方法String.format
这是关于其格式细节的更多信息。嗯,有吗?