0

i want to get a string between some given string

like this..

String wholetext = "SomePlace Pin- 110065 Name and address Alias 1 ID of customer-123 SomePlace "
String namerequired =  "";
String textafter = "110065 Name and address";
String textbefore = "ID of customer";

//Constant part-110065 Name and address and ID of customer

googled and get some thing like this doindexing and this doindexing1

i am trying to get Alias 1 and not getting idea :( ? Help please

Community
  • 1
  • 1
dev_android
  • 493
  • 1
  • 11
  • 29

4 Answers4

1

You can use Regexes(Regular Expressions). there are a lot of regex learning sites over internet. i hope this helps.

mrkalan
  • 11
  • 1
1

If waht you want is after the last occurence of **, you can try

String substring = wholetext.substring(wholetext.lastIndexOf("**"));
System.out.println(substring);
Barbe Rouge
  • 394
  • 1
  • 6
  • 18
1

This should solve your problem:

    String wholetext = "Place Then Pin- **110065 Name and address** Alias 1 ID of customer-123 Some Place ";
    String textafter = "110065 Name and address";
    String textbefore = "ID of customer";

    int index1 = wholetext.indexOf(textbefore);
    int index2 = wholetext.indexOf(textafter) + textafter.length();

    String namerequired = wholetext.substring(index2, index1);
    System.out.println(namerequired);

Output:

    Alias 1

But using a regex or String matcher would probably be better.

DeiAndrei
  • 947
  • 6
  • 16
0

You can definitely use Java's Pattern matching to solve your problem. This will help you solve your current problem and any future Regex problems

See: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html

Patrick W
  • 1,485
  • 4
  • 19
  • 27