Extract string...

Discussion in 'Java' started by Wandy Tang, Jul 16, 2004.

  1. Wandy Tang

    Wandy Tang Guest

    Hi,

    I would like to extract string from this situation,
    @phone:12345678@ or @phone:123456789@.
    How can I retrieve string from between [@phone:mad:]?
    I do not want to read character one by one,
    I am guessing I can use regular expression to achieve this situation...
    Appreciate any help, TIA.

    Regards,
    Wandy Tang
    Wandy Tang, Jul 16, 2004
    #1
    1. Advertising

  2. Wandy Tang

    Ike Guest

    String s[] = getArgs("@phone:12345678@ or @phone:123456789@.", "@");
    //this gives you an array of strings.....
    phone:12345678
    or
    phone:12345678
    //Then merely call it again with one of the strings like "phone:12345678",
    this time parsing on the colon:
    String ss[] = getArgs(s[1],":");
    //and now in ss[1] you should have something like "12345678" //Ike

    final public String[] getArgs(String parameter, String delimiter){
    String args[];
    int nextItem=0;
    StringTokenizer stoke=new StringTokenizer(parameter,delimiter);
    args=new String[stoke.countTokens()];
    while(stoke.hasMoreTokens()){
    args[nextItem]=stoke.nextToken();
    nextItem=(nextItem+1)%args.length;
    }
    return args;
    }

    "Wandy Tang" <> wrote in message
    news:cd8cj2$...
    > Hi,
    >
    > I would like to extract string from this situation,
    > @phone:12345678@ or @phone:123456789@.
    > How can I retrieve string from between [@phone:mad:]?
    > I do not want to read character one by one,
    > I am guessing I can use regular expression to achieve this situation...
    > Appreciate any help, TIA.
    >
    > Regards,
    > Wandy Tang
    >
    >
    Ike, Jul 16, 2004
    #2
    1. Advertising

  3. Wandy Tang

    Sudsy Guest

    Wandy Tang wrote:
    > Hi,
    >
    > I would like to extract string from this situation,
    > @phone:12345678@ or @phone:123456789@.
    > How can I retrieve string from between [@phone:mad:]?
    > I do not want to read character one by one,
    > I am guessing I can use regular expression to achieve this situation...
    > Appreciate any help, TIA.


    String s = "@phone:123456789@";
    System.out.println( s.substring( 1, s.length() - 1 ).split( ":" )[1] );

    Of course that's starting to look like APL. It also doesn't perform the
    kind of checking you require for robust, production-level code. But it
    works!
    Sudsy, Jul 16, 2004
    #3
  4. Wandy Tang

    Wandy Tang Guest

    Thanks all for your kindly response.

    Actually, I am trying to do this:

    my phone no: @phone:12345678@ or @phone:1234567890@

    to code like this use in wml application:

    my phone no: <a href="wtai://wp/mc;12345678">12345678</a> or <a
    href="wtai://wp/mc;1234567890">1234567890</a>

    Any help appreciated. Thanks all.


    "Sudsy" <> wrote in message
    news:...
    > Wandy Tang wrote:
    > > Hi,
    > >
    > > I would like to extract string from this situation,
    > > @phone:12345678@ or @phone:123456789@.
    > > How can I retrieve string from between [@phone:mad:]?
    > > I do not want to read character one by one,
    > > I am guessing I can use regular expression to achieve this situation...
    > > Appreciate any help, TIA.

    >
    > String s = "@phone:123456789@";
    > System.out.println( s.substring( 1, s.length() - 1 ).split( ":" )[1] );
    >
    > Of course that's starting to look like APL. It also doesn't perform the
    > kind of checking you require for robust, production-level code. But it
    > works!
    >
    Wandy Tang, Jul 16, 2004
    #4
  5. Wandy Tang

    Filip Larsen Guest

    Wandy Tang wrote

    > I would like to extract string from this situation,
    > @phone:12345678@ or @phone:123456789@.
    > How can I retrieve string from between [@phone:mad:]?
    > I do not want to read character one by one,
    > I am guessing I can use regular expression to achieve this

    situation...

    Indeed you can use regular expressions. For instance, the following
    class will return the phone numbers from 4 to 10 digits long, or null if
    there is no match.

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

    public class PhoneMatcher {

    private final Pattern pattern = Pattern.compile("@phone:(\\d{4,10})@");

    public String getPhone(String input) {
    Matcher matcher = pattern.matcher(input);
    return matcher.matches() ? matcher.group(1) : null;
    }

    }


    Read more about regex in the javadoc for Pattern and Matcher.


    Regards,
    --
    Filip Larsen
    Filip Larsen, Jul 16, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bogosian
    Replies:
    0
    Views:
    342
    Bogosian
    Jan 26, 2004
  2. Jay Douglas
    Replies:
    3
    Views:
    3,493
    Jay Douglas
    Aug 27, 2004
  3. Neo Geshel
    Replies:
    0
    Views:
    865
    Neo Geshel
    Feb 19, 2006
  4. Dwight Army of Champions
    Replies:
    0
    Views:
    392
    Dwight Army of Champions
    Apr 20, 2010
  5. Jochen Brenzlinger
    Replies:
    5
    Views:
    673
    Daniel Pitts
    Nov 21, 2011
Loading...

Share This Page