Java查找以字母开头的所有单词

时间:2014-03-14 07:52:28

标签: java regex string

我试图获取所有以长字符串中的字母开头的单词。你会怎么做这是java?我不想遍历每个字母或低效的东西。

编辑:我也不能在构建的数据结构中使用任何数据结构(当然除了数组) - 它用于cs类。然而,我可以创建自己的数据结构(我已经创建了几个)。

7 个答案:

答案 0 :(得分:2)

您可以尝试从String中获取数组集合,然后迭代它:

String s = "my very long string to test";

for(String st : s.split(" ")){
    if(st.startsWith("t")){
        System.out.println(st);
    }
}

答案 1 :(得分:2)

你需要清楚一些事情。什么是“字”?你只想找到以字母开头的“单词”,所以我假设单词也可以有其他字符。但允许哪些字符?是什么定义了这个词的开头?空白,任何非字母,任何非字母/非数字,......?

e.g:

String TestInput = "test séntènce îwhere I'm want,to üfind 1words starting $with le11ers.";
String regex = "(?<=^|\\s)\\pL\\w*";

Pattern p = Pattern.compile(regex, Pattern.UNICODE_CHARACTER_CLASS);

Matcher matcher = p.matcher(TestInput);
while (matcher.find()) {
    System.out.println(matcher.group());
}

正则表达式(?<=^|\s)\pL\w*会找到以字母开头的序列(\pL对于字母为Unicode property),后跟0或更多"word" characters(Unicode字母和数字) ,因为修饰语Pattern.UNICODE_CHARACTER_CLASSlookbehind assertion (?<=^|\s)确保在序列之前有字符串的开头或空格。

所以我的代码会打印出来:

test
séntènce ==> contains non ASCII letters
îwhere   ==> starts with a non ASCII letter
I        ==> 'm is missing, because `'` is not in `\w`
want
üfind    ==> starts with a non ASCII letter
starting
le11ers  ==> contains digits

遗漏的话:

,to     ==> starting with a ","
1words  ==> starting with a digit
$with   ==> starting with a "$"

答案 2 :(得分:0)

您可以构建HashMap -

HashMap<String,String> map = new HashMap<String,String>();

示例 -

ant, bat, art, cat

Hashmap
a -> ant,art
b -> bat
c -> cat

找到以“a”开头的所有单词,只需执行

map.get("a")

答案 3 :(得分:0)

Scanner scan = new Scanner(text); // text being the string you are looking in
char test = 'x'; //whatever letter you are looking for
while(scan.hasNext()){
   String wordFound = scan.next();
   if(wordFound.charAt(0)==test){
       //do something with the wordFound
   }
}

这将做你想要的,在if语句中用

这个词做你想要的

答案 4 :(得分:0)

您可以使用split()方法。这是一个例子:

String string = "your string";
String[] parts = string.split(" C");

 for(int i=0; i<parts.length; i++) {
   String[] word = parts[i].split(" ");

    if( i > 0 ) {
          // ignore the rest words because don't starting with C
      System.out.println("C" + word[0]); 
    }
else {    // Check 1st excplicitly
          for(int j=0; j<word.length; j++) {

        if ( word[j].startsWith("c") || word[j].startsWith("C"))
              System.out.println(word[j]); 
            }   
        }

     }

其中“C”是你的来信。然后在数组周围循环。对于零件[0],您必须检查它是否以“C”开头。从i = 1开始循环是我的错。正确的是从0开始。

答案 5 :(得分:0)

您可以获取字符串的第一个字母,并使用API​​方法检查它是否是字母。

String input = "jkk ds 32";
String[] array = input.split(" ");
for (String word : array) {
    char[] arr = word.toCharArray();
    char c = arr[0];
    if (Character.isLetter(c)) {
        System.out.println( word + "\t isLetter");
    } else {
        System.out.println(word + "\t not Letter");
    }
}

以下是一些示例输出:

jkk  isLetter
ds   isLetter
32   not Letter

答案 6 :(得分:0)

Regexp方式:

public static void main(String[] args) {
    String text = "my very long string to test";
    Matcher m = Pattern.compile("(^|\\W)(\\w*)").matcher(text);
    while (m.find()) {
      System.out.println("Found: "+m.group(2));
    }
 }
相关问题