我想在java字符串的段落中找到给定的模式?

时间:2015-04-22 09:06:18

标签: java string find

这是我的问题描述,

String str="We may use the name you provide for your Google Profile across all of the services we offer that require a Google Account. In addition, we may replace past names associated with your Google Account so that you are represented consistently across all our services. If other users already have your email, or other information";

现在,

String pat="we offer";

所以我需要找到模式' pat'在字符串' str'而且我需要单词以相同的模式开头...喜欢"我们可以","与您的",......等。

1 个答案:

答案 0 :(得分:1)

首先,你可以从字符串pat中分割para。然后比较'pat'附近的单词并找到你需要的模式。

  public class Test {

    public static void main(String[] args) {
        String a="We may use the name you provide for your Google Profile across all of the services we offer that require a Google Account. In addition, we may replace past names associated with your Google Account so that you are represented consistently across all our services. If other users already have your email, or other information";
        String b="we offer";
    String[] s= a.split(b);
    System.out.print("==============="+s[0]);
    System.out.print("==============="+s[1]);
        }
}

这只是一个想法,而不是一个正确的代码。请进行验证和异常处理。