从String中提取哈希标记

从String中提取哈希标记

本文介绍了从String中提取哈希标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在字符串中的字符后立即提取任何字词,并将它们存储在 String [] array。

I want to extract any words immediately after the # character in a String, and store them in a String[] array.

例如,如果这是我的 String ...

For example, if this is my String...

"Array is the most #important thing in any programming #language"

然后我想将以下单词提取到 String [] 数组中...

Then I want to extract the following words into a String[] array...

"important"
"language"

有人可以提供实现此目的的建议。

Could someone please offer suggestions for achieving this.

推荐答案

试试这个 -

String str="#important thing in #any programming #7 #& ";
Pattern MY_PATTERN = Pattern.compile("#(\\S+)");
Matcher mat = MY_PATTERN.matcher(str);
List<String> strs=new ArrayList<String>();
while (mat.find()) {
  //System.out.println(mat.group(1));
  strs.add(mat.group(1));
}

输出 -

important
any
7
&

这篇关于从String中提取哈希标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 13:12