Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在一个字符串中列出所有提到的内容_Java_Android_Kotlin - Fatal编程技术网

Java 如何在一个字符串中列出所有提到的内容

Java 如何在一个字符串中列出所有提到的内容,java,android,kotlin,Java,Android,Kotlin,我正在创建一个EditText,在用你的昵称写@时可以提到其他用户,比如在Facebook、Instagram、Twitter等网站上。例如,在文本“Today@john and@jane吃了一块蛋糕”中,我想创建一个带有值的ArrayList​​“约翰”和“简”。但它不起作用。如何才能做到这一点?使用此正则表达式。它查找@后面的一组单词字符(字母、数字、下划线) val atMentions: List<String> = "(?<=@)\\w+".toRegex().fin

我正在创建一个
EditText
,在用你的昵称写@时可以提到其他用户,比如在Facebook、Instagram、Twitter等网站上。例如,在文本“Today@john and@jane吃了一块蛋糕”中,我想创建一个带有值的
ArrayList
​​“约翰”和“简”。但它不起作用。如何才能做到这一点?

使用此正则表达式。它查找@后面的一组单词字符(字母、数字、下划线)

val atMentions: List<String> = "(?<=@)\\w+".toRegex().findAll(editText.text).map { it.value }
val:List=“(?你可以

val list = mutableListOf<String>()
if(text.contains("@"))
   text.split("@").forEach { list.add(it.substringBefore(" ")) }
val list=mutableListOf()
if(text.contains(“@”))
text.split(“@”).forEach{list.add(it.substringBefore(“”)}

我认为有两个很好的选择:

  • 使用regex并解析片段。创建找到的UserTags的组
  • 使用ApacheUIMARUTA查找文本中的结构并注释用户标记
  • publicstaticarraylistgetNitenedNames(字符串文本){
    字符串[]tmp=text.split(“@”);
    ArrayList ArrayList=新的ArrayList();
    
    对于(int i=1;i请向我们展示您的尝试。此外,用户名的合法字符是什么?这将贪婪地包括名称后面的任何标点符号,并且它将包括字符串中的第一个单词,而不管是否存在@。
    public static ArrayList<String> getMentionedNames(String text){
            String[] tmp=text.split("@");
            ArrayList<String> arrayList=new ArrayList<>();
            for (int i=1;i<tmp.length;i++){
                String first=tmp[i].split(" ")[0];
                arrayList.add(first);
            }
            return arrayList;
        }