String Parsing

B

Bob Davis

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
 
M

Mattias Sjögren

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
 
S

Shiv Kumar

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
:)
 
B

Bob Davis

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
 
B

Bob Davis

Shiv,

Split works if there is delimiter between the various
components of the string.

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
 
P

phoenix

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.ToString());

Yves
 
J

Jon Skeet [C# MVP]

Bob Davis said:
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.
 
B

Bob Davis

Thanks everyone for your input.

Bob
-----Original Message-----
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 .ToString());

Yves

"Bob Davis" <[email protected]> schreef in bericht
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


.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top