识别字符串中单词的第一个字符

时间:2016-11-09 07:53:07

标签: java

我想确定我的索引是否在单词边界。

E.g。在字符串"National Aeronautics Space Research is an amazing organization"

我想检查我的索引是否在R research。我可以使用charAt(index),但我希望将其设为通用,因为这将是识别首字母缩略词longforms的更大算法的一部分。所以基本上我想识别单词边界。

非常感谢帮助。

2 个答案:

答案 0 :(得分:0)

I have not got your second part of your question,"but I want to make this generic....,

public class IndexVal {
        public static void main(String args[]){

            String goodText="National Aeronautics Space Research is an amazing organization";
            String[] parts = goodText.split(" ");
            for (String part : parts) {
               System.out.println("Index is at a word boundary " +part.charAt(0)+" ");

            }
        }
    }

答案 1 :(得分:0)

据我了解,您希望获得字边界的索引。那么这段代码可能就是您所需要的。它使用正则表达式

    Pattern pattern = Pattern.compile("\\s\\w");
    String s = "National Aeronautics Space Research is an amazing organization";
    Matcher matcher = pattern.matcher(s);
    while (matcher.find()) {
        System.out.println(matcher.start());
    }