Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a substring from a string after a particular word

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 ?

like image 881
Bishan Avatar asked Aug 10 '14 04:08

Bishan


People also ask

How do you get a substring that comes after a certain character?

To get the substring after a specific character, call the substring() method, passing it the index after the character's index as a parameter. The substring method will return the part of the string after the specified character. Copied! We used the String.

How do you get a string before a specific substring?

Use the substring() method to get the substring before a specific character, e.g. const before = str. substring(0, str. indexOf('_')); . The substring method will return a new string containing the part of the string before the specified character.

How do you take a specific part of a string?

The substr() method extracts a part of a string. The substr() method begins at a specified position, and returns a specified number of characters. The substr() method does not change the original string. To extract characters from the end of the string, use a negative start position.

How do I extract a particular substring from a string in Java?

You can extract a substring from a String using the substring() method of the String class to this method you need to pass the start and end indexes of the required substring.


4 Answers

yourString.substring(yourString.indexOf("no") + 3 , yourString.length());
like image 148
Juned Ahsan Avatar answered Sep 29 '22 22:09

Juned Ahsan


You may try this

String example = "ABC Results for draw no 2888";
System.out.println(example.substring(example.lastIndexOf(" ") + 1));
like image 42
Rahul Tripathi Avatar answered Sep 29 '22 23:09

Rahul Tripathi


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.

like image 22
YoYo Avatar answered Sep 29 '22 21:09

YoYo


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

String parsed = StringUtils.substringAfter(yourString, "no ");
like image 30
Brujua Avatar answered Sep 29 '22 21:09

Brujua