0

i have a large String which includes numbers and text. Now i want exactly a number from that string. The number starts every time with a '6' and three '0' but then the number can be different digits. For example here is a try:

String text1 = "ID 6 IDENTIFICATION NUMBER 600026821 NAME: BECK POSTCODE 60025";

if(text1.contains("6000"))
    {
      System.out.println(text1.indexOf("6000"));

    }

So as you can see the String can also contains postcode digits and ids. But the number i want has always the same length of 9 digits and starts with '6000...'.

So how can i extract that number?

Thanks

EDIT

Ok now i try this one:

String index = "6000";
            String text1 = "ID 6 IDENTIFICATION NUMBER 600026821 NAME BECK POSTCODE 60025";

            System.out.println(text1.indexOf(index));



            String number = text1.substring(text1.indexOf(index), text1.lastIndexOf(text1.indexOf(index) + 5));
            System.out.println(number);

It starts but ends not correctly

  • You may look at regular expressions: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html – Manuel Selva May 07 '15 at 10:27
  • You're almost there. Once you have the index of 6000 in the string, use substring to get the 9 characters after this index. Note that calling contains() is useless, since indexOf() gives you the same information. – JB Nizet May 07 '15 at 10:28
  • 1
    Look at regular expressions. I'll try and mock up an example now but you should be able to say extract any number that starts 6000 and ends in either a space or end of the string. I won't submit an answer unless I figure out the exact regex you'll need. – Daniel Tung May 07 '15 at 10:29
  • http://stackoverflow.com/questions/1903252/extract-integer-part-in-string – Massimiliano Peluso May 07 '15 at 10:29
  • @Manuel Selva thanks for fast answere. Ok 'match' i can search for numbers in a string. But can i also say that the number must always have a length of 9 digits? – legalizeSINCE88 May 07 '15 at 10:29
  • You shouldn't identify your number only on that `6000` start, because the `ID` could also fulfill that requirement. Use the String *"IDENTIFICATION NUMBER"* to help you, because your searched number is right next to it. – Tom May 07 '15 at 10:33

5 Answers5

5

Regex can be used like this :

public static void main(String args[]) {
    String text1 = "ID 6 IDENTIFICATION NUMBER 600026821 NAME: BECK POSTCODE 60025";
    System.out.println(text1.replaceAll(".*?\\b(6000\\d+)\\b.*", "$1")); // replace everything except a number that starts with 6 and is followed by 000 with "".

}

O/P :

600026821

Note : You can use (6000\\d{5}) instead of (6000\\d+) if you are certain that the number of digits will be 9.

TheLostMind
  • 34,842
  • 11
  • 64
  • 97
  • 1
    @thanks - this, and all other examples, works fine;) – legalizeSINCE88 May 07 '15 at 10:42
  • Let's hope that his ID won't reach `600000001`, because this would cause trouble :D. Can/should this regex edited to start looking after *"IDENTIFICATION NUMBER"*? – Tom May 07 '15 at 11:04
3
for (String word : s.split(" ")) {
    int number = 0;
    if (word.startsWith("6000"))
        number = Integer.parseInt(word);
    }
}

EDIT

I hadn't read that the number you wanted is always of length 9. In that case, check its length in the if condition:

if (word.startsWith("6000") && word.length() == 9)
Balduz
  • 3,450
  • 15
  • 32
1

Like that:

System.out.println(text1.substring(text1.indexOf("6000"),text1 .indexOf("6000")+9));

int value=Integer.parseInt(text1.substring(text1.indexOf("6000"),text1 .indexOf("6000")+9));
Veselin Davidov
  • 6,886
  • 1
  • 13
  • 21
0

Once you have the index of the "6000" just continue, you already said that the length is always 9

int start = text1.indexOf("6000");
String yourNumber = text1.substring(start, start+10);
Anton
  • 3,522
  • 3
  • 29
  • 44
0

You can do it like this to get all numbers in that string that follow your rules. Note that I added some more numbers to text1 for testing purposes.

package so;

public class NumberExtractor {

    public static void main(String[] args) {

        String text1 = "ID 6 IDENTIFICATION NUMBER 600026821 NAME: BECK 600026822600026823 POSTCODE 60002682460025";

        boolean notAtTheEnd = true;
        int currentIndex = 0;
        while (notAtTheEnd) {
            int start = text1.indexOf("6000", currentIndex);

            if (start > -1) {
                String number = text1.substring(start, start + 9);
                currentIndex = start + 1;
                System.out.println(number);
            } else {
                notAtTheEnd = false;
            }
        }
    }
}
Stephan
  • 1,749
  • 1
  • 22
  • 43