473,326 Members | 1,972 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,326 software developers and data experts.

Parsing a string to get the month

Hi,

How do i parse a string "ABC 2005 (12)" and convert it to "ABC 2005
(Dec)" in C#?
The number within the brackets represent month..
am new to c# and breaking head for couple of hours over this seemingly
simple thing, would be glad if anybody could help.

Thanks in Advance,
Ajai

Jun 20 '07 #1
8 5125
Ajai,

Well, if you can extract the 12, then you can create a DateTime instance
(the year and the day and the time don't matter, as long as the month is
12). Once you have that, you can call the ToString method on the DateTime
instance, and pass the custom format string of "MMM" to get the abbreviated
month.

Then, you would insert the value back into the string. Regular
expressions might help you here, unless the record is positional (meaning
that values are always at the same position) in which case I would suggest
using regular string manipulation methods.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"thunderbolt" <aj********@gmail.comwrote in message
news:11**********************@o61g2000hsh.googlegr oups.com...
Hi,

How do i parse a string "ABC 2005 (12)" and convert it to "ABC 2005
(Dec)" in C#?
The number within the brackets represent month..
am new to c# and breaking head for couple of hours over this seemingly
simple thing, would be glad if anybody could help.

Thanks in Advance,
Ajai

Jun 20 '07 #2
what is wrong with the "substring" and "indexof" ?

--
cheers,
RL
"thunderbolt" <aj********@gmail.comwrote in message
news:11**********************@o61g2000hsh.googlegr oups.com...
Hi,

How do i parse a string "ABC 2005 (12)" and convert it to "ABC 2005
(Dec)" in C#?
The number within the brackets represent month..
am new to c# and breaking head for couple of hours over this seemingly
simple thing, would be glad if anybody could help.

Thanks in Advance,
Ajai

Jun 20 '07 #3
"thunderbolt" <aj********@gmail.comwrote in message
news:11**********************@o61g2000hsh.googlegr oups.com...
Hi,

How do i parse a string "ABC 2005 (12)" and convert it to "ABC 2005
(Dec)" in C#?
The number within the brackets represent month..
am new to c# and breaking head for couple of hours over this seemingly
simple thing, would be glad if anybody could help.
As suggested by the EggHead, use substring and indexof.

Alternately the following two lines will generate "2005 Dec" in a textbox
string[] strParts = "ABC 2005(12)".Split(new Char[] { ' ', '(', ')' });

textBox1.Text = (Convert.ToDateTime(strParts[1] + '/' +
strParts[2])).ToString("yyyy MMM");

Jun 20 '07 #4
On Jun 20, 2:29 pm, thunderbolt <ajai.me...@gmail.comwrote:
Hi,

How do i parse a string "ABC 2005 (12)" and convert it to "ABC 2005
(Dec)" in C#?
The number within the brackets represent month..
am new to c# and breaking head for couple of hours over this seemingly
simple thing, would be glad if anybody could help.

Thanks in Advance,
Ajai
The Regex is your friend.

Jun 20 '07 #5
On Jun 20, 11:29 am, thunderbolt <ajai.me...@gmail.comwrote:
Hi,

How do i parse a string "ABC 2005 (12)" and convert it to "ABC 2005
(Dec)" in C#?
The number within the brackets represent month..
am new to c# and breaking head for couple of hours over this seemingly
simple thing, would be glad if anybody could help.

Thanks in Advance,
Ajai
Try this.

string whole = "ABC 2005 (12)";
int leftParenIndex = whole.IndexOf("(");
string prefix = whole.Substring(0, leftParenIndex + 1);
string monthString = whole.Substring(leftParenIndex + 1);
string suffix = monthString(monthString.IndexOf(")"));
monthString = monthString.Substring(0, monthString.IndexOf(")"));
DateTime firstOfMonth = new DateTime(DateTime.Now.Year,
Convert.ToInt32(monthString), 1);
string monthString = firstOfMonth.ToString("MMM");
string result = prefix + monthString + suffix;

Jun 20 '07 #6
On 20 Jun, 23:37, Bruce Wood <brucew...@canada.comwrote:
On Jun 20, 11:29 am, thunderbolt <ajai.me...@gmail.comwrote:
Hi,
How do i parse a string "ABC 2005 (12)" and convert it to "ABC 2005
(Dec)" in C#?
The number within the brackets represent month..
am new to c# and breaking head for couple of hours over this seemingly
simple thing, would be glad if anybody could help.
Thanks in Advance,
Ajai

Try this.

string whole = "ABC 2005 (12)";
int leftParenIndex = whole.IndexOf("(");
string prefix = whole.Substring(0, leftParenIndex + 1);
string monthString = whole.Substring(leftParenIndex + 1);
string suffix = monthString(monthString.IndexOf(")"));
monthString = monthString.Substring(0, monthString.IndexOf(")"));
DateTime firstOfMonth = new DateTime(DateTime.Now.Year,
Convert.ToInt32(monthString), 1);
string monthString = firstOfMonth.ToString("MMM");
string result = prefix + monthString + suffix;
Thank you all for your ideas, shall try them out.

Regards,
Ajai

Jun 21 '07 #7
PS

"Bruce Wood" <br*******@canada.comwrote in message
news:11*********************@a26g2000pre.googlegro ups.com...
On Jun 20, 11:29 am, thunderbolt <ajai.me...@gmail.comwrote:
>Hi,

How do i parse a string "ABC 2005 (12)" and convert it to "ABC 2005
(Dec)" in C#?
The number within the brackets represent month..
am new to c# and breaking head for couple of hours over this seemingly
simple thing, would be glad if anybody could help.

Thanks in Advance,
Ajai

Try this.

string whole = "ABC 2005 (12)";
int leftParenIndex = whole.IndexOf("(");
string prefix = whole.Substring(0, leftParenIndex + 1);
string monthString = whole.Substring(leftParenIndex + 1);
string suffix = monthString(monthString.IndexOf(")"));
monthString = monthString.Substring(0, monthString.IndexOf(")"));
DateTime firstOfMonth = new DateTime(DateTime.Now.Year,
Convert.ToInt32(monthString), 1);
string monthString = firstOfMonth.ToString("MMM");
string result = prefix + monthString + suffix;
I was going to suggest
for(int i = 1; i <= 12; i++)
s = s.Replace(String.Format("({0})", i), String.Format("({0})", new
DateTime(2007, i, 1).ToString("MMM")));

but thought that the unnecessary looping was not such a good idea however
after seeing the amount of code that is required to slice and dice then
maybe it is not so bad.

PS
Jun 22 '07 #8
On Jun 21, 7:52 pm, "PS" <ecneserpeg...@hotmail.comwrote:
"Bruce Wood" <brucew...@canada.comwrote in message

news:11*********************@a26g2000pre.googlegro ups.com...


On Jun 20, 11:29 am, thunderbolt <ajai.me...@gmail.comwrote:
Hi,
How do i parse a string "ABC 2005 (12)" and convert it to "ABC 2005
(Dec)" in C#?
The number within the brackets represent month..
am new to c# and breaking head for couple of hours over this seemingly
simple thing, would be glad if anybody could help.
Thanks in Advance,
Ajai
Try this.
string whole = "ABC 2005 (12)";
int leftParenIndex = whole.IndexOf("(");
string prefix = whole.Substring(0, leftParenIndex + 1);
string monthString = whole.Substring(leftParenIndex + 1);
string suffix = monthString(monthString.IndexOf(")"));
monthString = monthString.Substring(0, monthString.IndexOf(")"));
DateTime firstOfMonth = new DateTime(DateTime.Now.Year,
Convert.ToInt32(monthString), 1);
string monthString = firstOfMonth.ToString("MMM");
string result = prefix + monthString + suffix;

I was going to suggest
for(int i = 1; i <= 12; i++)
s = s.Replace(String.Format("({0})", i), String.Format("({0})", new
DateTime(2007, i, 1).ToString("MMM")));

but thought that the unnecessary looping was not such a good idea however
after seeing the amount of code that is required to slice and dice then
maybe it is not so bad.
Hey... I think you're on to something there. How about this minor
adjustment:

for (int i = 1; i <= 12; i++)
{
string monthNumber = "(" + i.ToString() + ")";
if (s.IndexOf(monthNumber) >= 0)
{
s = s.Replace(monthNumber, "(" + new DateTime(2000, i,
1).ToString("MMM") + ")");
break;
}
}

....which will replace the first one it finds and then stop.

Jun 22 '07 #9

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

Similar topics

1
by: AttilaTheChris | last post by:
I'm trying to figure out how to parse the MySQL Date/Time group '20031001155704' into meaningful data (i.e. - year, month, day, hour, minute, and second). Is there a built-in PHP string function...
2
by: Jari | last post by:
I would need a "script" that parses the following information in 'outdoor.txt'. What i actually need are those variables (for example $yTemp). I'd do it myself but my knowhow lacks ;D outdoor.txt...
8
by: Gerrit Holl | last post by:
Posted with permission from the author. I have some comments on this PEP, see the (coming) followup to this message. PEP: 321 Title: Date/Time Parsing and Formatting Version: $Revision: 1.3 $...
9
by: Thomas W | last post by:
I'm developing a web-application where the user sometimes has to enter dates in plain text, allthough a format may be provided to give clues. On the server side this piece of text has to be parsed...
6
by: Kalle Anke | last post by:
I want to parse a date string, for example '2005-09-23', and since I haven't done this before I would like to ask what is the best way to do it. I've looked around and the dateutil seems to be...
0
by: Uncle Leo | last post by:
I created an OleDbDataAdapter with the wizard in Visual Studio 2003. It created a dataset, connectionstring etc. for me to work with. It also created a .xsd file where one of the columns type is...
10
by: Stu | last post by:
Can somebody please tell me the most effient away to parse the date YYYYMMDD from the following string. char *date_path = "/dira/dirb/dirc/dird/2006/12/04" Note: the number of directories...
7
by: Grey Alien | last post by:
Does *ANYONE* in here know how I may parse the various date/time 'elements' from a string?. The input string has the ff format: 'YYYY-MM-DD HH:MM:SS AM'
3
by: Damon Getsman | last post by:
Okay so I'm writing a script in python right now as a dirty fix for a problem we're having at work.. Unfortunately this is the first really non-trivial script that I've had to work with in python...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.