[英]indexOf(“@”) method in Java
Why does this always return -1? 为什么总是返回-1? Does this have anything to do with '@' being the annotation symbol?
这与“ @”是注释符号有什么关系吗?
What can I do to search for the first occurrence of a substring starting with '@'? 如何搜索以'@'开头的子字符串的首次出现?
UserMentionEntity[] userMentionEntities = status.getUserMentionEntities();
for (UserMentionEntity ume : userMentionEntities) {
final String mention = ume.getText();
int mentionStart = text.indexOf("@" + mention);
int mentionEnd = mentionStart + mention.length();
ss.setSpan(new ForegroundColorSpan(Color.parseColor(COLOR)), mentionStart, mentionEnd + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
Apparently, mentionStart is -1. 显然,提及开始是-1。 I am using twitter4j library.
我正在使用twitter4j库。
Sample: 样品:
text: RT @Cornerstone_TMC Thank you @ukgdos at sa lahat ng nag-abang kanina kay @yengpluggedin! #SeenZoned @academyofrocksg… http://t.co/rsLqjlZJgg
mention: ukgdos
No. Your String doesn't contain what you think it does, 不。您的字符串不包含您认为的内容,
String mention = "not";
String text = "I do @not think so.";
int mentionStart = text.indexOf("@" + mention);
System.out.printf("%d%n", mentionStart);
Prints 版画
5
Edit 编辑
Based on your now provided text and mention 根据您现在提供的文字和提及
String text = "RT @Cornerstone_TMC Thank you @ukgdos "
+ "at sa lahat ng nag-abang kanina kay @yengpluggedin! "
+ "#SeenZoned @academyofrocksg… http://t.co/rsLqjlZJgg";
String mention = "ukgdos";
int mentionStart = text.indexOf("@" + mention);
System.out.printf("%d%n", mentionStart);
Prints 版画
30
I fixed the problem with my app. 我已解决我的应用程序的问题。
UserMentionEntity[] userMentionEntities = status.getUserMentionEntities();
for (UserMentionEntity ume : userMentionEntities) {
final String mention = ume.getText().toLowerCase();
int mentionStart = text.toLowerCase().indexOf("@" + mention);
int mentionEnd = mentionStart + mention.length();
ss.setSpan(new ForegroundColorSpan(Color.parseColor(COLOR)), mentionStart, mentionEnd + 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
I noticed that Twitter allows you to change cases in the mentions. 我注意到Twitter允许您更改提及内容。
@yengPLUGGEDin
can be shown as @yengpluggedin
so that ume.getText()
in the code there returns the proper screenname while text
contains the tweet text as it is. @yengPLUGGEDin
可以显示为@yengpluggedin
以便其中的代码中的ume.getText()
返回正确的屏幕名称,而text
按原样包含tweet文本。
I am not quite sure why it also failed on my sample input there. 我不太确定为什么我的示例输入也失败了。 Maybe it didn't but failed on the next before the print was executed.
也许没有,但是在执行打印之前的下一次失败。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.