-8

I have string:

Adding offert to database.

 number of agreement: 98393553553
 number of accounting: 1242422222222222224242
 Agreement in step: Accounting

How to extract text: 98393553553 (text has always the same number of characters - 11 characters) using Java. Text before: 98393553553 it can be different (sometimes).

abarisone
  • 3,707
  • 11
  • 35
  • 54
lamcpp
  • 69
  • 9
  • 4
    Your question doesn't show any effort of neither you trying to solve your problem on your own; nor you checking stack overflow for this question. Do you really assume that you are the very first person who is asking here how to parse strings in Java? – GhostCat Apr 22 '15 at 12:16
  • possible duplicate of [How to split a string in Java](http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java) – Wouter Apr 22 '15 at 12:22

3 Answers3

1

Try using String.split() with the delimiter :, and String.substring() on index 1 of the String[] obtained from the earlier String.split().

M. Shaw
  • 1,742
  • 11
  • 15
1

You may use a regular expression matcher for this:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

  String line = .........; //your input line
  String pattern = "number of agreement:\\s*(\\d+)";

  // Create a Pattern object
  Pattern r = Pattern.compile(pattern);

  // Now create matcher object.
  Matcher m = r.matcher(line);
  if (m.find( )) {
     System.out.println("Number of agreement: " + m.group(0) );
  } else {
     System.out.println("Number of agreement not found");
  }
AndreyS Scherbakov
  • 2,674
  • 2
  • 20
  • 27
0

You can use String.split() and store it into a String Array. Then finally extract whatever you want from the array.

Rajeev
  • 120
  • 1
  • 15