-1

I have a string

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

What i am trying to do::

  • i want to extract Bangalore from this string
  • But i don't want to extract bangalore from the start of the string traversing step-by-step instead i want to extract bangalore from the end of the string by traversing
  • How can i achieve this ?
Devrath
  • 42,072
  • 54
  • 195
  • 297
  • why the downvotes ? .... I did a search on stackoverflow on similar question ...but couldnt find one ? ..... – Devrath May 21 '14 at 12:42
  • do you mean, you want to get the last instance of banglore in your string if there are multiples? – Sanjeev May 21 '14 at 12:46
  • @Sanjeev .... I dont understand what u mean by instance .... but .. i want the Bangalore name traversing from the end !.... ps:: i know it can be done from the start .... my use case requires this specific use case ...hope i am clear – Devrath May 21 '14 at 12:50
  • Let me ask you what do you mean "it can be done from start step by step" ? – Sanjeev May 21 '14 at 12:53
  • reverse what u know then. – Top Cat May 21 '14 at 12:53

1 Answers1

0

Check this solution, Here, i am first eliminating wrappers([, ]) around the actual data. Then searching for keyword from the last is done

Code

   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);

            }
        }
    }

Output

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
Gaurav Gupta
  • 4,586
  • 4
  • 39
  • 72
  • Thanks, i used your solution with http://stackoverflow.com/a/18782701/1083093 ... to achieve my goal – Devrath May 21 '14 at 13:26