473,811 Members | 4,039 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I parse this string into int fragments?

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;
Nov 15 '05 #1
4 5182
On Wed, 4 Feb 2004 13:25:37 -0800, Top Gun wrote:
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;


You could use the Split method to get two strings (as long as you had a
consistent seperator such as the '-' in your excample) and then convert
those to ints.

Tim
--
Tim Smelser - MVP Visual C#
To email me, make the snot hot.
Nov 15 '05 #2
L#
On Wed, 4 Feb 2004 13:25:37 -0800, "Top Gun" <nf*@nospam.com > wrote:
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;


string str = "0154321-001";
string[]arr = str.Split('-');
if (arr.Length==2)
{
int contractid = int.Parse(arr[0]);
int contractseq = int.Parse(arr[1]);
}
--
Ludwig
mailto:ludwig_( nospamplease)st uyck@pandora(no spamplease).be
Nov 15 '05 #3
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.WriteLi ne (string.Format ("{0}: {1}", "int1", mt.Groups ["int1"].Value));
Console.WriteLi ne (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.****** *************** @no-sp-am-eh-mail.com
remove all "no-sp-am-eh"s to reply

"Top Gun" <nf*@nospam.com > wrote in message news:Oe******** ******@TK2MSFTN GP12.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;

Nov 15 '05 #4
Cezary Nolewajka <c.************ *********@no-sp-am-eh-mail.com> wrote:
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).


I don't agree with that. Using String.Split, you would only need to
change the check to make sure that it had returned an array with the
appropriate new size, and add the call to parse the final part.

Using a regular expression, you end up with an expression which is
(IMO) harder to read than just a straight call to String.Split, and you
have to change that expression to add a part.

Regular expressions certainly have their place, but to use them when
String.Split works perfectly well is overkill, IMO.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
1738
by: serpent17 | last post by:
Hello all, I have this line of numbers: 04242005 18:20:42-0.000002, 271.1748608, , , repeated several times in a text file and I would like each element to be part of a vector. how do I do this ? I am not very capable in using
8
41272
by: Developwebsites | last post by:
I want to enter sentences with blanks between words, names, cities, new york, pam anderson, etc. as in: Input"enter your name";name$ print"your name is: ";name$ C++ seems to make it rather difficult to do a simple thing as that. #include<string> using namespace std;
3
8844
by: Austin | last post by:
Hello I am wondering if anyone knows if there is a way to store string literals within an XML tag. For instance I would like to store HTML formatting data for an attribute but it keeps getting picked up as a tag by the XML parser. eg... <name>John</name>
0
1474
by: Hugh Sparks | last post by:
If I configure and use two different fragment extractors on the same XML document, how can I write xslt template match patterns that distinguish which elements these fragments replaced? Details: My xml document contains, among other elements, two distinct elements that need external java processing by a custom-made Cocoon serializer that converts the contents of these elements to "other stuff".
8
3775
by: Francesco Moi | last post by:
Hi. I must parse this XML document: -------------- <doc> <item> <name>Jerry</name> <message>Hi<br>My name is Jerry</message> </item> </doc>
0
1105
by: Steve Jorgensen | last post by:
I'm wondering if there's an approach to writing consistent code to read/write XML data in arbitrary order that I'm simply missing. It seems to be easy getting stuff -out- of a DOM via XPath, but it's much tougher building a DOM document in arbitrary order. Yes - I can get the parent context element first, using XPath, but then I build custom wrappers and helpers to simplify the building and adding fragments in the correct namespace,...
1
2787
by: Clemens Hoffmann | last post by:
Hello, i use resources to store code fragments. This fragments contain sequences with line feed and tabs (i.e: "\r\n\t\t"). When reading the text from the resource it is a literal string (in the debugger it is stated with a @ character in from of it.
1
1028
by: Bahman | last post by:
Hello! I have two questions. -I don't seem to be able to set string length in c#. It seems to be a variable. -when I am composing the body of an email in code, there is a "!" in the string. I am assuming it has to do with the string length, hence I did this:
2
1510
by: Belize | last post by:
Hi. Essence of problem in the following: Here is lines in utf8 of this form "BZ?ツーリTV%ツキDVD" Is it possible to split them into the fragments that contain only latin printable symbols (aplhabet + "?#" etc) and fragments with the hieroglyphs, so it could be like this ? Then, after translate of hieroglyphs, necessary to join line, so it could be like this "BZ? navigation TV% display DVD"
0
9724
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9604
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10644
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9201
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6882
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4336
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3863
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.