25

I have below String.

ABC Results for draw no 2888

I would like to extract 2888 from here. That means, I need to extract characters after no in above string.

I'm always extract the number after the word no. The String contain no other no letter combinations elsewhere within it. String may contain other numbers and I don't need to extract them. Always there will be a space before the number and the number I wish to extract always be at the end of the String.

How could I achieve this ?

Bishan
  • 15,211
  • 52
  • 164
  • 258
  • Maybe it's me, but your question and in particular your requirements seem incomplete. Will you always extract a number after the word "no"? Will the String contain no other "no" letter combinations elsewhere within it? Will the String contain no other numbers that you don't or do wish to extract? Will there always be a space before the number? Will the number always be at the end of the String? Please help clear up my muddled cloudy mind by clarifying your requirements. Also show what you've tried and tell us how it's not working. – Hovercraft Full Of Eels Aug 10 '14 at 04:27
  • @HovercraftFullOfEels Thanks for your informative comment. Yes i'm always extract a number after the word `no`. The String contain no other "no" letter combinations elsewhere within it. String may contain other numbers and i don't need to extract them. There always be a space before the number and Yes, the number i wish to extract always be at the end of the String. – Bishan Aug 10 '14 at 04:43
  • Thank you for the clarification. I'm sure that you can understand why this information should have been in your question from the start. – Hovercraft Full Of Eels Aug 10 '14 at 05:14

5 Answers5

43
yourString.substring(yourString.indexOf("no") + 3 , yourString.length());
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • 5
    You should probably add `trim` to the end or replace `2` with `3` to get solely the number... and add `()` after `length`. – TNT Aug 10 '14 at 04:19
  • also make the .length a function call: .length(), or it barfs – vwvan Jul 16 '15 at 21:13
9

You may try this

String example = "ABC Results for draw no 2888";
System.out.println(example.substring(example.lastIndexOf(" ") + 1));
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
6

You always want to strive something that is easy to configure and modify. That is why I always recommend to choose Regex Pattern matching over other searches.

Example, consider this for your example:

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


public class Play {
  public static void main(String args[]) { 
    Pattern p = Pattern.compile("^(.*) Results for draw no (\\d+)$");
    Matcher m = p.matcher("ABC Results for draw no 2888");
    m.find();
    String groupName = m.group(1);
    String drawNumber = m.group(2);
    System.out.println("Group: "+groupName);
    System.out.println("Draw #: "+drawNumber);
  }
}

Now from the provided pattern, I can easily identify the useful parts. It helps me to identify problems, and I can identify additional parts in the pattern that is useful to me (I have added the group-name).

Another clear benefit is that I can store easily this pattern externally in a configuration file.

YoYo
  • 9,157
  • 8
  • 57
  • 74
6

For anyone willing to achieve this with the ubiquitous Apache Commons StringUtils:

String parsed = StringUtils.substringAfter(yourString, "no ");
Brujua
  • 71
  • 1
  • 6
0

you can also do something like this

String demo="ABC Results for draw no 2888";

demo.substring(demo.indexOf("no") + 3)
Boken
  • 4,825
  • 10
  • 32
  • 42