Java 从给定字符串中提取字符串部分的最佳方法是什么?

Java 从给定字符串中提取字符串部分的最佳方法是什么?,java,android,string,wordpress,Java,Android,String,Wordpress,好的,我有两个字符串,两个都有我想要的字符串(一个人的名字)。所以我可以处理其中的任何一个,从字符串中获取该名称。问题是哪一个更有效,以及使用它的最佳方式。这是两个字符串:字符串1- <p><a href=\"http://abinet.org/wp-content/uploads/2014/02/molestation1.jpg \" ><img class=\"aligncenter size-full wp-image-714\" src=\"http://

好的,我有两个字符串,两个都有我想要的字符串(一个人的名字)。所以我可以处理其中的任何一个,从字符串中获取该名称。问题是哪一个更有效,以及使用它的最佳方式。这是两个字符串:字符串1-

<p><a href=\"http://abinet.org/wp-content/uploads/2014/02/molestation1.jpg
 \" ><img class=\"aligncenter size-full wp-image-714\" src=\"http://abine
t.org/wp-content/uploads/2014/02/molestation1.jpg\" alt=\"molestation\" 
width=\"540\" height=\"393\" /></a></p>\n<p><strong>Krishna Pujari</str
ong></p>\n<p>A 25-year-old man was Sunday arrested in Kerala on charges
of raping his mother, media reports.</p>\n<p>“This was happening for s
ome time and there used to be ruckus in their home over this. The nei.....
\n克里希纳·普贾里

\n一名25岁男子周日在喀拉拉邦因被指控而被捕 媒体报道说,他强奸了母亲。

\n“这件事发生在s 有一段时间,他们的家里曾经因为这件事吵闹过。。。。。
字符串2:

 <p>Krishna Pujari A 25-year-old man was Sunday arrested in Kerala on charges
 of raping his mother, media reports. “This was happening for some time and there 
 used to be ruckus in their home over this. The neighbours were unhappy over this 
 and filed a complaint and we arrested the man,” said a police official at …</p>\n
 ghbours were unhappy over this and filed a complaint and we arrested 
 the man,” said a police official at the Pala police station.</p>\n<p>
周日,一名25岁男子Krishna Pujari在喀拉拉邦因被指控而被捕 媒体报道说:“这已经发生了一段时间了 这件事过去常在他们家引起骚动,邻居们对此很不高兴 并提出申诉,我们逮捕了这名男子,”一名警察说,…

\n ghbours对此表示不满,提出申诉,我们逮捕了他 “那个人,”帕拉警察局的一名警官说。

\n 在这两个字符串中,我想要的是“Krishna Pujari”。在第一个字符串中,它在这一行中:介于strong(在wordpress中表示粗体)

Krishna Pujari
在第二个字符串中,它是后面的第一个单词:

<p>

这些数据来自使用json api的网站。有时,字符串可能没有该名称。因此,第一个字符串将没有

<strong>Krishna Pujari</strong>
Krishna Pujari
并且字符串2在以下内容之后将没有名称:

<p>

它将直接从“一个25岁的男人是星期天……”开始。发生这种情况时,我根本不想提取任何字符串。 这是我用来提取该名称的代码:

int startIndex = content.indexOf("<strong>")+8;
        String substring = content.substring(startIndex, startIndex+500);
        int subendIndex = substring.indexOf("</strong></p>");
        int endIndex = startIndex + subendIndex;
        String short_content = content.substring(startIndex, endIndex);
intstartindex=content.indexOf(“”)+8;
String substring=content.substring(startIndex,startIndex+500);
int subendIndex=substring.indexOf(“

”); int-endIndex=startIndex+subendIndex; String short_content=content.substring(startIndex,endIndex);
这是有效的。但是我觉得这不是正确的方法,因为我不能依赖于这段代码。因为如果找不到/strong>(当名称不存在时),它将崩溃。或者当名字不在那里,而字符串中的其他单词是粗体的(它将出现在/strong>中),那么它会给我一个我不想要的单词

请告诉我提取那个名字的最佳方法。唯一的可能性是只有几个名字(4-5个)会出现,或者根本不会有名字


我的问题可能需要一点时间来理解,但可能很容易回答。我刚开始编程。请帮忙

您可以使用
regex
提取
strong
标记内的名称,而不是计算
子字符串
,这不是一个好主意

示例:

    try {
        String s = "<strong>Krishna Pujari</strong>";
        Pattern p = Pattern.compile("<strong>(.+?)</strong>");
        Matcher m = p.matcher(s);
        m.find();
        System.out.println(m.group(1));
    } catch (IllegalStateException e) {
        // this wont close your app
    }
试试看{
字符串s=“克里希纳·普贾里”;
Pattern p=Pattern.compile(“(.+?)”;
匹配器m=匹配器p;
m、 查找();
系统输出println(m.group(1));
}捕获(非法状态){
//这不会关闭你的应用程序
}

您可以使用
regex
提取
strong
标记内的名称,而不是计算
子字符串
,这不是一个好主意

示例:

    try {
        String s = "<strong>Krishna Pujari</strong>";
        Pattern p = Pattern.compile("<strong>(.+?)</strong>");
        Matcher m = p.matcher(s);
        m.find();
        System.out.println(m.group(1));
    } catch (IllegalStateException e) {
        // this wont close your app
    }
试试看{
字符串s=“克里希纳·普贾里”;
Pattern p=Pattern.compile(“(.+?)”;
匹配器m=匹配器p;
m、 查找();
系统输出println(m.group(1));
}捕获(非法状态){
//这不会关闭你的应用程序
}

您可以使用
regex
提取
strong
标记内的名称,而不是计算
子字符串
,这不是一个好主意

示例:

    try {
        String s = "<strong>Krishna Pujari</strong>";
        Pattern p = Pattern.compile("<strong>(.+?)</strong>");
        Matcher m = p.matcher(s);
        m.find();
        System.out.println(m.group(1));
    } catch (IllegalStateException e) {
        // this wont close your app
    }
试试看{
字符串s=“克里希纳·普贾里”;
Pattern p=Pattern.compile(“(.+?)”;
匹配器m=匹配器p;
m、 查找();
系统输出println(m.group(1));
}捕获(非法状态){
//这不会关闭你的应用程序
}

您可以使用
regex
提取
strong
标记内的名称,而不是计算
子字符串
,这不是一个好主意

示例:

    try {
        String s = "<strong>Krishna Pujari</strong>";
        Pattern p = Pattern.compile("<strong>(.+?)</strong>");
        Matcher m = p.matcher(s);
        m.find();
        System.out.println(m.group(1));
    } catch (IllegalStateException e) {
        // this wont close your app
    }
试试看{
字符串s=“克里希纳·普贾里”;
Pattern p=Pattern.compile(“(.+?)”;
匹配器m=匹配器p;
m、 查找();
系统输出println(m.group(1));
}捕获(非法状态){
//这不会关闭你的应用程序
}


ok。。但是,当该行不存在时(字符串s)会怎么样呢。然后它就没有什么可编译的东西了,它会崩溃。@AbhinavRaja,如果它不在那里的话??那么字符串是什么样子,名字在哪里?这就是问题所在。然后该字符串将为空,并且该名称不应显示在应用程序中。它是一个显示新闻的新闻应用程序。那个名字是编辑的名字。在一些新闻中,没有给出编辑的名字。那一次我将不会显示任何名称。@AbhinavRaja您可以捕获错误,这样您的应用程序就不会关闭。@AbhinavRaja欢迎您使用sapceok更改上述查找字符串的代码。。但是,当该行不存在时(字符串s)会怎么样呢。然后它就没有什么可编译的东西了,它会崩溃。@AbhinavRaja,如果它不在那里的话??那么字符串是什么样子,名字在哪里?这就是问题所在。然后该字符串将为空,并且该名称不应显示在应用程序中。它是一个显示新闻的新闻应用程序。那个名字是编辑的名字。在一些新闻中,没有给出编辑的名字。那一次我将不会显示任何名称。@Ab