Re: How do I parse this string into int fragments?
From: Cezary Nolewajka (c.nolewajka-no-sp-am-eh_at_no-sp-am-eh-mail.com)
Date: 02/04/04
- Next message: Roy Fine: "Re: 39 - 32.2 = 7.799999999999 - why?"
- Previous message: Adrian Vinca [MSFT]: "RE: TreeView - OnPaint override? - Boolean to display certain nodes"
- In reply to: Top Gun: "How do I parse this string into int fragments?"
- Next in thread: Jon Skeet [C# MVP]: "Re: How do I parse this string into int fragments?"
- Reply: Jon Skeet [C# MVP]: "Re: How do I parse this string into int fragments?"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 4 Feb 2004 23:05:59 +0100
A good idea to use regular expressions for such parsing. Then you don't need to fiddle with the code too much when the syntax of the expression changes (for instance you add another module with "-" and another integer).
The code to parse your syntax would be:
string strRxPattern = "(?<int1>\\d*)-(?<int2>\\d*)";
string strToCheck = "1287103871-87450";
Regex rx = new Regex (strRxPattern);
if (rx.IsMatch (strToCheck))
{
Match mt = rx.Match (strToCheck);
Console.WriteLine (string.Format ("{0}: {1}", "int1", mt.Groups ["int1"].Value));
Console.WriteLine (string.Format ("{0}: {1}", "int2", mt.Groups ["int2"].Value));
}
So, you end up with nicely split strings... :-) Have a good time with regexping! :-)
-- Cezary Nolewajka mailto:c.nolewajka-no-sp-am-eh@no-sp-am-eh-mail.com remove all "no-sp-am-eh"s to reply "Top Gun" <nfr@nospam.com> wrote in message news:OeucqW26DHA.1672@TK2MSFTNGP12.phx.gbl... > If I have a string that is in a constant format of, say 0154321-001, how can > I parse this into two fragments: > > int contractid = 0154321; > int contractseq = 001; > >
- Next message: Roy Fine: "Re: 39 - 32.2 = 7.799999999999 - why?"
- Previous message: Adrian Vinca [MSFT]: "RE: TreeView - OnPaint override? - Boolean to display certain nodes"
- In reply to: Top Gun: "How do I parse this string into int fragments?"
- Next in thread: Jon Skeet [C# MVP]: "Re: How do I parse this string into int fragments?"
- Reply: Jon Skeet [C# MVP]: "Re: How do I parse this string into int fragments?"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|