【问题标题】:How to get substrings from a string in android?如何从android中的字符串中获取子字符串?
【发布时间】:2015-04-30 10:59:06
【问题描述】:

你好,我有一个长字符串,它有像“
”和“”和这样的html标签,我想从这个字符串中获取值,谁能给我解决方案如何获取值从中? 我的字符串是:

<strong>1 king bed</strong><br /> <b>Entertainment</b> - Wired Internet access and cable channels <br /><b>Food & Drink</b> - Refrigerator, minibar, and coffee/tea maker<br /><b>Bathroom</b> - Shower/tub combination, bathrobes, and slippers<br /><b>Practical</b> - Sofa bed, dining area, and sitting area<br />

我的尝试

int start = description_long.indexOf("Food");
                    int end = description_long.indexOf("<br />");
                    String subString = description_long.substring(start,
                            end);

                    System.out
                            .println("===============MY SUB STRING FROM STRING============="
                                    + start
                                    + ""
                                    + "============end======="
                                    + end + "");

我想获取 Food & DrinkBathroom 的值,谁能告诉我如何以编程方式在 android 中以单独的字符串获取这些值。 p>

【问题讨论】:

  • 您尝试过什么?也许你应该看看对字符串可能的操作。
  • 查看我的编辑以查看我的试用伙伴

标签: android regex string xpath substring


【解决方案1】:

最好使用正则表达式或尝试解析。

&lt;a[^&gt;]*&gt;([^&lt;]*)&lt;[^&gt;]*&gt;(.*)

http://www.vogella.com/tutorials/JavaRegularExpressions/article.html

【讨论】:

  • 嗨,先生,谢谢,请帮帮我,我已经尝试过但没有得到正确的值..:(请。
【解决方案2】:

试试这个,但值必须以 &lt;br /&gt; 结尾

    String key = "<b>Bathroom</b>";  //<b>Food & Drink</b>
    int start = htmlInput.lastIndexOf(key);
    String value = htmlInput.substring(start + key.length(), htmlInput.indexOf("<br />", start));
    System.out.println(value);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 2014-05-26
    • 1970-01-01
    • 1970-01-01
    • 2019-03-16
    相关资源
    最近更新 更多