473,398 Members | 2,393 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

String Parsing

In C# one can construct a string using the format method.

for example

string newstring = string.Format("CC%d-%s.%d-%s-%
d",123,"aaaa",456,"bbb",789);

The resulting newstring will be "CC123-aaa.456-bbb-789"

The question I have is, given the newstring and the
format how do I extract the parse out the different
elements.

Thanks,
Bob
Nov 15 '05 #1
8 8383
Bob,
string newstring = string.Format("CC%d-%s.%d-%s-%
d",123,"aaaa",456,"bbb",789);
That looks more like a C printf style fromat string than a .NET one.

The resulting newstring will be "CC123-aaa.456-bbb-789"

The question I have is, given the newstring and the
format how do I extract the parse out the different
elements.


That would be non-trivial to do (if not impossible). If it's doable
depends on what kind of input you support. You'd have to do the
parsing yourself one way or another.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 15 '05 #2
In this particular case you dhuld br able to use the String.Split method to
back back an array like

CC123
aaa
456
bbb
789

Of course I don't see the big picture so I can't give you a generic solution
:)
--
Shiv R. Kumar
http://www.matlus.com
Nov 15 '05 #3
Mattias,

You are correct it is a printf style format, string class
in .Net provides a static member called "Format" that
enables the developers to format the way I mentioned
below.

I have come to the same conclusion, but before I go and
code I wanted to double check.

Thanks

-----Original Message-----
Bob,
string newstring = string.Format("CC%d-%s.%d-%s-%
d",123,"aaaa",456,"bbb",789);
That looks more like a C printf style fromat string than

a .NET one.
The resulting newstring will be "CC123-aaa.456-bbb-789"

The question I have is, given the newstring and the
format how do I extract the parse out the different
elements.
That would be non-trivial to do (if not impossible). If

it's doabledepends on what kind of input you support. You'd have to do theparsing yourself one way or another.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.comPlease reply only to the newsgroup.
.

Nov 15 '05 #4
Shiv,

Split works if there is delimiter between the various
components of the string.
string newstring = string.Format("CC%d-%s.%d-%s-%
d",123,"aaaa",456,"bbb",789); The resulting newstring will be "CC123-aaa.456-bbb-789"

In this case there is no delimiter between CC and 123 and
so split fails.

I am looking for scanf type functionality, looks like I
will have the string myself.

Thanks and appreciate your help.
Bob
-----Original Message-----
In this particular case you dhuld br able to use the String.Split method toback back an array like

CC123
aaa
456
bbb
789

Of course I don't see the big picture so I can't give you a generic solution:)
--
Shiv R. Kumar
http://www.matlus.com
.

Nov 15 '05 #5
You could try using regex. If you have your input format you 'just' need to
convert it to accepted regex format.

string newstring = "CC123-aaa.456-bbb-789";
GroupCollection gc = Regex.Match(newstring,
@"CC(\d+)-(\w+).(\d+)-(\w+)-(\d+)").Groups;

for (int i = 1; i < gc.Count; i++)
Console.WriteLine("Group {0} : {1}", i, gc[i].ToString());

Yves

"Bob Davis" <an*******@discussions.microsoft.com> schreef in bericht
news:0e****************************@phx.gbl...
In C# one can construct a string using the format method.

for example

string newstring = string.Format("CC%d-%s.%d-%s-%
d",123,"aaaa",456,"bbb",789);

The resulting newstring will be "CC123-aaa.456-bbb-789"

The question I have is, given the newstring and the
format how do I extract the parse out the different
elements.

Thanks,
Bob

Nov 15 '05 #6
Bob Davis <an*******@discussions.microsoft.com> wrote:
You are correct it is a printf style format, string class
in .Net provides a static member called "Format" that
enables the developers to format the way I mentioned
below.


No it doesn't. It enables developers for format with

"CC{0}-{1}.{2}-{3}-{4}", 123, "aaaa", 456, "bbb", 789"

which I believe was Mattias' point.

As for the parsing - a regular expression may well do it for you.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
Thanks everyone for your input.

Bob
-----Original Message-----
You could try using regex. If you have your input format you 'just' need toconvert it to accepted regex format.

string newstring = "CC123-aaa.456-bbb-789";
GroupCollection gc = Regex.Match(newstring,
@"CC(\d+)-(\w+).(\d+)-(\w+)-(\d+)").Groups;

for (int i = 1; i < gc.Count; i++)
Console.WriteLine("Group {0} : {1}", i, gc [i].ToString());
Yves

"Bob Davis" <an*******@discussions.microsoft.com> schreef in berichtnews:0e****************************@phx.gbl...
In C# one can construct a string using the format method.
for example

string newstring = string.Format("CC%d-%s.%d-%s-%
d",123,"aaaa",456,"bbb",789);

The resulting newstring will be "CC123-aaa.456-bbb-789"

The question I have is, given the newstring and the
format how do I extract the parse out the different
elements.

Thanks,
Bob

.

Nov 15 '05 #8
Sorry, I misunderstood. I thought the "-" were the delimiters :)

--
Shiv R. Kumar
http://www.matlus.com
Nov 15 '05 #9

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

Similar topics

26
by: Kai Jaensch | last post by:
Hello, i am an newbie and i have to to solve this problem as fast as i can. But at this time i don´t have a lot of success. Can anybody help me (and understand my english :-))? I have a...
18
by: Steve Litvack | last post by:
Hello, I have built an XMLDocument object instance and I get the following string when I examine the InnerXml property: <?xml version=\"1.0\"?><ROOT><UserData UserID=\"2282\"><Tag1...
50
by: z. f. | last post by:
HI, i have string in format dd/mm/yyyyy hh:mm:ss and giving this as an input to DateTime.Parse gives a string was not recognized as a valid date time format string error. how do i make the parse...
9
by: Python.LeoJay | last post by:
Dear all, i need to parse billions of numbers from a file into float numbers for further calculation. i'm not satisfied with the speed of atof() function on my machine(i'm using visual c++ 6)....
4
by: Michael Meckelein | last post by:
Hello, Wondering, if C# (framework 2.0) does not support parsing DateTime timezones in three letter acronyms. I would like to parse date strings like "2005 Nov 01 11:58:47.490 CST -6:00" but...
3
by: dimasteg | last post by:
Hi all C. Nead some help with string "on the fly" parsing, how it can be realized ? Any ideas? I got some of my own, but it's interesting to get other points of view . Regards.
9
balabaster
by: balabaster | last post by:
I'm looking for some ideas regarding string parsing and brackets. Say I have the following string: 56*(73+23/(28+(7/14)-(3/2)) What would be the best way to parse the string for each opening...
6
by: James Arnold | last post by:
Hello, I am new to C and I am trying to write a few small applications to get some hands-on practise! I am trying to write a random string generator, based on a masked input. For example, given...
6
by: (2b|!2b)==? | last post by:
I am expecting a string of this format: "id1:param1,param2;id2:param1,param2,param3;id" The tokens are seperated by semicolon ";" However each token is really a struct of the following...
1
by: eyeore | last post by:
Hello everyone my String reverse code works but my professor wants me to use pop top push or Stack code and parsing code could you please teach me how to make this code work with pop top push or...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...

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.