Current Position:Home > Find first occurence of an integer in a string

Find first occurence of an integer in a string

Update:11-30Source: network consolidation
Advertisement
Hi all,
I have a number of strings which go something like "MBNA Flow 12a" and I want to separate the integer from the rest of the string so I can parse it, sort the strings into some sort of order according to their integer, and then put it all back together again. The problem I'm having is finding the position of the integer within the string, so that I can take a substring and/or split the string. I've looked it up and I gather that using regex I should be able to find the beginning and end of the integer, using something along the lines of           
Pattern pattern = Pattern.compile("\\d+");
           Matcher matcher = pattern.matcher(temp.getString(1));which I found on another site where someone had the same problem. I've looked at the regex tutorials and according to them this is what I want, but whenever I try it no match is found, and I don't know why. Some sites have claimed that the regular expression has to apply to the entire string, so I would need to also take into account the MBNA Flow etc, something I'm loathe to do as the string is quite unpredictable, but others say that it should work exactly as is.
Any help would be greatly appreciated. :)
Thanks.

The Best Answer

Advertisement
silversurfer20 wrote:
Its throwing the exception cause its not finding a match in the string I know there should be a match in. What I'm wondering is why .Call find() before using either group() or start():
import java.util.regex.*;
public class Main {
    public static void main(String[]args) {
        Pattern p = Pattern.compile("\\d+");
        Matcher m = p.matcher("foo 12 bar 34 baz");
        if(m.find()) {
            System.out.println("first number: "+m.group()+", starts at index: "+m.start());
        } else {
            System.out.println("nothing found");
}Output:
first number: 12, starts at index: 4
  • Find first occurence of an integer in a string Update:11-30

    Hi all, I have a number of strings which go something like "MBNA Flow 12a" and I want to separate the integer from the rest of the string so I can parse it, sort the strings into some sort of order according to their integer, and then put it all

  • How to count number of occurences of a character  in a string Update:10-11

    I want to get the count of occurences of a character within a string. Is there any function for this or should i go for a PLSQL. For example is the sting is "occurences" & if i search for character "c", the answer should be 3. Rega

  • Oracle query to get all occurences of a text in a string Update:11-30

    Hi Does anybody know how to write a query to get all occurences of a text in a string in different rows of the table For eg: I have a string <aa>bb</aa><aa>cc</aa><aa>ddd</aa> I have to find every occurence of <aa>

  • I need to WAP to count the number of occurences of an alphabet in a string. Update:11-30

    I need to WAP to count the number of occurences of an alphabet in a string.I tried a lot and have surfed a lot regarding this problem. I m not the most proficient with java.but this is all i could come up with,and would appreciate some help here.I ho

  • 2 parts: 1) integer array to string 2) string out to a jTextField Update:11-30

    I am completely new to Java and Netbeans, but I'm writing my 1st application that takes a "hex" character input from a jTextField, then converts it to an "binary" integer array. I then do a lot of bit manipulation, and generate a new &

  • Finding the position of an integer in a string Update:10-11

    I haven't done much Java programming for the past 3 months, but I'm trying to get back into it now, but I'm having some trouble. I'm trying to break up a string of integers, to store each individual integer in an array. So, if I had something like 13

  • Change integer value to string? Update:11-30

    Hi all, I was wondering if it is possible to change an integer value to a string. I saw in this forum that a string can be changed to an integer using parseInt and I was wondering if there is a similar method for going the other way. Regards.try this

  • Count the occurence of a character in a string Update:11-30

    Post Author: halfpat CA Forum: Formula Hello to everyone, I use CR 8,0. Is anyone can tell me if this is possible: I have a string "12345-C-1 23456-C-2 34567-C-4", how can I count the occurence of the "C" character in this string ?Is t

  • Are Vector Integer and Vector String different types? Update:11-30

    Maybe I am missing something. Are Vector <String> and Vector <Integer> different types? Can I have to methods of the same name with arguments of these types? That would be the point of method overloading. Appearently not. The following code do

  • How do we convert an integer to a string? Update:10-11

    thanks!It might be somewhat less efificient because it uses a StringBuffer (and its append method, then a toString), that is, it constructs one object more. public class a { public String tos(int i) { return ""+i; }javap -c -private a Compiled f