从字符串末尾遍历字符串中提取单词

时间:2014-05-21 12:38:53

标签: java android

我有一个字符串

String mystring="[
      "28, 4th Cross Road, HAL 3rd Stage, Hal, Puttappa layout, New Thippasandra, Bangalore, Karnataka 560008, Hindistan"
   ]"; 

我想做什么 ::

  • 我想从此字符串中提取Bangalore
  • 但我不想从字符串的开头提取bangalore 逐步遍历我想从中提取bangalore 通过遍历
  • 结束字符串
  • 我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

检查此解决方案, 在这里,我首先在实际数据周围消除包装器([])。 然后从最后一个搜索关键字

<强>代码

   public class SO12 {
        public static void main(String[] args) {
            String mystring = "[\"28, 4th Cross Road, HAL 3rd Stage, Hal, Puttappa layout, New Thippasandra, Bangalore, Karnataka 560008, Hindistan\"]";


            mystring = mystring.replace("[", "");
            mystring = mystring.replace("]", "");
            mystring = mystring.replace("\"", "");
            String[] arr = mystring.split(",");
            System.out.println(mystring + arr.length);
            for (int i = arr.length - 1; i > 0; i--) {
                String s = arr[i];
                String log = s;
                if (s.equalsIgnoreCase(" Bangalore")) {
                    log = log + "\t found";
                }else{
                    log = log + "\t not found";

                }
                System.out.println(log);

            }
        }
    }

<强>输出

28, 4th Cross Road, HAL 3rd Stage, Hal, Puttappa layout, New Thippasandra, Bangalore, Karnataka 560008, Hindistan9
 Hindistan   not found
 Karnataka 560008    not found
 Bangalore   found
 New Thippasandra    not found
 Puttappa layout     not found
 Hal     not found
 HAL 3rd Stage   not found
 4th Cross Road  not found