Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Get 2 Strings

Reply
Thread Tools

Get 2 Strings

 
 
shapper
Guest
Posts: n/a
 
      10-19-2007
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

 
Reply With Quote
 
 
 
 
Alexey Smirnov
Guest
Posts: n/a
 
      10-19-2007
On Oct 19, 8:09 pm, shapper <(E-Mail Removed)> 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
);

 
Reply With Quote
 
 
 
 
shapper
Guest
Posts: n/a
 
      10-19-2007
On Oct 19, 7:20 pm, Alexey Smirnov <(E-Mail Removed)> wrote:
> On Oct 19, 8:09 pm, shapper <(E-Mail Removed)> 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

 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      10-19-2007
On Oct 19, 8:58 pm, shapper <(E-Mail Removed)> wrote:
> On Oct 19, 7:20 pm, Alexey Smirnov <(E-Mail Removed)> wrote:
>
>
>
>
>
> > On Oct 19, 8:09 pm, shapper <(E-Mail Removed)> 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

 
Reply With Quote
 
Alexey Smirnov
Guest
Posts: n/a
 
      10-19-2007
On Oct 19, 8:58 pm, shapper <(E-Mail Removed)> wrote:
> On Oct 19, 7:20 pm, Alexey Smirnov <(E-Mail Removed)> wrote:
>
>
>
>
>
> > On Oct 19, 8:09 pm, shapper <(E-Mail Removed)> 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


 
Reply With Quote
 
Rad [Visual C# MVP]
Guest
Posts: n/a
 
      10-19-2007
On Fri, 19 Oct 2007 11:58:54 -0700, shapper <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Catching std::strings and c-style strings at once Kurt Krueckeberg C++ 2 11-17-2004 03:53 AM
convert list of strings to set of regexes; convert list of strings to trie Klaus Neuner Python 7 07-26-2004 07:25 AM
Comparing strings from within strings Rick C Programming 3 10-21-2003 09:10 AM



Advertisments