Get 2 Strings

Discussion in 'ASP .Net' started by shapper, Oct 19, 2007.

  1. shapper

    shapper Guest

    Hello,

    I have a string which has the following format (I give 2 examples):

    "FirstWord468x60SecondWord"
    "FirstWord5x172SecondWord"

    I need to get the numbers 468 and 60, or 5 and 172 into strings
    FirstNumber and SecondNumber.

    How can I do this?

    Can I use a Regex?

    Thanks,
    Miguel
     
    shapper, Oct 19, 2007
    #1
    1. Advertising

  2. On Oct 19, 8:09 pm, shapper <> wrote:
    > Hello,
    >
    > I have a string which has the following format (I give 2 examples):
    >
    > "FirstWord468x60SecondWord"
    > "FirstWord5x172SecondWord"
    >
    > I need to get the numbers 468 and 60, or 5 and 172 into strings
    > FirstNumber and SecondNumber.
    >
    > How can I do this?
    >
    > Can I use a Regex?
    >
    > Thanks,
    > Miguel


    yes, you can use a regex.

    For example:

    using System.Text.RegularExpressions;

    Regex regex = new Regex(
    @"(\d*)",
    RegexOptions.Compiled
    );
     
    Alexey Smirnov, Oct 19, 2007
    #2
    1. Advertising

  3. shapper

    shapper Guest

    On Oct 19, 7:20 pm, Alexey Smirnov <> wrote:
    > On Oct 19, 8:09 pm, shapper <> wrote:
    >
    >
    >
    > > Hello,

    >
    > > I have a string which has the following format (I give 2 examples):

    >
    > > "FirstWord468x60SecondWord"
    > > "FirstWord5x172SecondWord"

    >
    > > I need to get the numbers 468 and 60, or 5 and 172 into strings
    > > FirstNumber and SecondNumber.

    >
    > > How can I do this?

    >
    > > Can I use a Regex?

    >
    > > Thanks,
    > > Miguel

    >
    > yes, you can use a regex.
    >
    > For example:
    >
    > using System.Text.RegularExpressions;
    >
    > Regex regex = new Regex(
    > @"(\d*)",
    > RegexOptions.Compiled
    > );


    Hello,

    I tried the following:

    Dim a As String = "something128x98word"

    Dim b() As String = Regex.Split(a, "(\d*)")

    For Each s As String In b
    Response.Write(s)
    Next

    This is not working.
    Could you tell me what am I doing wrong?

    Thanks,
    Miguel
     
    shapper, Oct 19, 2007
    #3
  4. On Oct 19, 8:58 pm, shapper <> wrote:
    > On Oct 19, 7:20 pm, Alexey Smirnov <> wrote:
    >
    >
    >
    >
    >
    > > On Oct 19, 8:09 pm, shapper <> wrote:

    >
    > > > Hello,

    >
    > > > I have a string which has the following format (I give 2 examples):

    >
    > > > "FirstWord468x60SecondWord"
    > > > "FirstWord5x172SecondWord"

    >
    > > > I need to get the numbers 468 and 60, or 5 and 172 into strings
    > > > FirstNumber and SecondNumber.

    >
    > > > How can I do this?

    >
    > > > Can I use a Regex?

    >
    > > > Thanks,
    > > > Miguel

    >
    > > yes, you can use a regex.

    >
    > > For example:

    >
    > > using System.Text.RegularExpressions;

    >
    > > Regex regex = new Regex(
    > > @"(\d*)",
    > > RegexOptions.Compiled
    > > );

    >
    > Hello,
    >
    > I tried the following:
    >
    > Dim a As String = "something128x98word"
    >
    > Dim b() As String = Regex.Split(a, "(\d*)")
    >
    > For Each s As String In b
    > Response.Write(s)
    > Next
    >
    > This is not working.
    > Could you tell me what am I doing wrong?
    >
    > Thanks,
    > Miguel- Hide quoted text -
    >
    > - Show quoted text -


    The Regex.Split method splits the string at a delimiter, for example,
    if you change a regular expression to match into "x"

    Dim b() As String = Regex.Split(a, "x")

    you will get "something128" and "98word"

    If you need to get a numbers, use following

    Dim a As String = "something128x98word"

    Dim r As Regex = New Regex("(\d+)") ' I changed it to {+}

    For Each m As Match In r.Matches(a)
    Response.Write(m.Groups.Item(1).ToString())
    Next
     
    Alexey Smirnov, Oct 19, 2007
    #4
  5. On Oct 19, 8:58 pm, shapper <> wrote:
    > On Oct 19, 7:20 pm, Alexey Smirnov <> wrote:
    >
    >
    >
    >
    >
    > > On Oct 19, 8:09 pm, shapper <> wrote:

    >
    > > > Hello,

    >
    > > > I have a string which has the following format (I give 2 examples):

    >
    > > > "FirstWord468x60SecondWord"
    > > > "FirstWord5x172SecondWord"

    >
    > > > I need to get the numbers 468 and 60, or 5 and 172 into strings
    > > > FirstNumber and SecondNumber.

    >
    > > > How can I do this?

    >
    > > > Can I use a Regex?

    >
    > > > Thanks,
    > > > Miguel

    >
    > > yes, you can use a regex.

    >
    > > For example:

    >
    > > using System.Text.RegularExpressions;

    >
    > > Regex regex = new Regex(
    > > @"(\d*)",
    > > RegexOptions.Compiled
    > > );

    >
    > Hello,
    >
    > I tried the following:
    >
    > Dim a As String = "something128x98word"
    >
    > Dim b() As String = Regex.Split(a, "(\d*)")
    >
    > For Each s As String In b
    > Response.Write(s)
    > Next
    >
    > This is not working.
    > Could you tell me what am I doing wrong?
    >
    > Thanks,
    > Miguel- Hide quoted text -
    >
    > - Show quoted text -


    I think, you can use the Split method too:

    Change the regular expression to

    Regex.Split(a, "[A-Za-z]+")

    and check if string is not empty

    For Each s As String In b
    if s.Length()>0 then
    Response.Write(s)
    end if
    Next
     
    Alexey Smirnov, Oct 19, 2007
    #5
  6. On Fri, 19 Oct 2007 11:58:54 -0700, shapper <> wrote:

    >Hello,
    >
    >I tried the following:
    >
    > Dim a As String = "something128x98word"
    >
    > Dim b() As String = Regex.Split(a, "(\d*)")
    >
    > For Each s As String In b
    > Response.Write(s)
    > Next
    >
    >This is not working.
    >Could you tell me what am I doing wrong?
    >
    >Thanks,
    >Miguel


    Try this:

    Regex r = new Regex(@"(?<number>\d+)");
    MatchCollection m =
    r.Matches("FirstWord468x60SecondWord");


    The match collection will give you the matches found in the string.

    So for the string, m.Count should return 2.

    To access the individual values use their indexes

    m[0].Value should give you 468
    m[1].Value should give you 60

    --
    http://bytes.thinkersroom.com
     
    Rad [Visual C# MVP], Oct 19, 2007
    #6
    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. Kurt Krueckeberg
    Replies:
    2
    Views:
    716
    =?ISO-8859-1?Q?Ney_Andr=E9_de_Mello_Zunino?=
    Nov 17, 2004
  2. Rick

    Comparing strings from within strings

    Rick, Oct 21, 2003, in forum: C Programming
    Replies:
    3
    Views:
    386
    Irrwahn Grausewitz
    Oct 21, 2003
  3. Klaus Neuner
    Replies:
    7
    Views:
    498
    Klaus Neuner
    Jul 26, 2004
  4. Girish Sahani
    Replies:
    17
    Views:
    574
    Boris Borcic
    Jun 9, 2006
  5. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    767
    Malcolm
    Jun 24, 2006
Loading...

Share This Page