473,799 Members | 3,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5147
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.co m

"thunderbol t" <aj********@gma il.comwrote in message
news:11******** **************@ o61g2000hsh.goo glegroups.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
"thunderbol t" <aj********@gma il.comwrote in message
news:11******** **************@ o61g2000hsh.goo glegroups.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
"thunderbol t" <aj********@gma il.comwrote in message
news:11******** **************@ o61g2000hsh.goo glegroups.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.ToDate Time(strParts[1] + '/' +
strParts[2])).ToString("yy yy MMM");

Jun 20 '07 #4
On Jun 20, 2:29 pm, thunderbolt <ajai.me...@gma il.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...@gma il.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(mon thString.IndexO f(")"));
monthString = monthString.Sub string(0, monthString.Ind exOf(")"));
DateTime firstOfMonth = new DateTime(DateTi me.Now.Year,
Convert.ToInt32 (monthString), 1);
string monthString = firstOfMonth.To String("MMM");
string result = prefix + monthString + suffix;

Jun 20 '07 #6
On 20 Jun, 23:37, Bruce Wood <brucew...@cana da.comwrote:
On Jun 20, 11:29 am, thunderbolt <ajai.me...@gma il.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(mon thString.IndexO f(")"));
monthString = monthString.Sub string(0, monthString.Ind exOf(")"));
DateTime firstOfMonth = new DateTime(DateTi me.Now.Year,
Convert.ToInt32 (monthString), 1);
string monthString = firstOfMonth.To String("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*******@cana da.comwrote in message
news:11******** *************@a 26g2000pre.goog legroups.com...
On Jun 20, 11:29 am, thunderbolt <ajai.me...@gma il.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(mon thString.IndexO f(")"));
monthString = monthString.Sub string(0, monthString.Ind exOf(")"));
DateTime firstOfMonth = new DateTime(DateTi me.Now.Year,
Convert.ToInt32 (monthString), 1);
string monthString = firstOfMonth.To String("MMM");
string result = prefix + monthString + suffix;
I was going to suggest
for(int i = 1; i <= 12; i++)
s = s.Replace(Strin g.Format("({0}) ", i), String.Format(" ({0})", new
DateTime(2007, i, 1).ToString("MM M")));

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.comwrot e:
"Bruce Wood" <brucew...@cana da.comwrote in message

news:11******** *************@a 26g2000pre.goog legroups.com...


On Jun 20, 11:29 am, thunderbolt <ajai.me...@gma il.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(mon thString.IndexO f(")"));
monthString = monthString.Sub string(0, monthString.Ind exOf(")"));
DateTime firstOfMonth = new DateTime(DateTi me.Now.Year,
Convert.ToInt32 (monthString), 1);
string monthString = firstOfMonth.To String("MMM");
string result = prefix + monthString + suffix;

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

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(mont hNumber) >= 0)
{
s = s.Replace(month Number, "(" + new DateTime(2000, i,
1).ToString("MM M") + ")");
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
1031
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 that does this? Thanks, Chris
2
11361
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 is input and variables are "output" which i need to make graphs (jgraph) -------OUTDOOR.txt------- Date; Time; Temp øC 21.11.2003; 18:21:39; -2.69
8
9449
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 $ Last-Modified: $Date: 2003/10/28 19:48:44 $ Author: A.M. Kuchling <amk@amk.ca> Status: Draft Type: Standards Track
9
2776
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 into a datetime python-object. Does anybody have any pointers on this? Besides the actual parsing, my main concern is the different locale date formats and how to be able to parse those strange us-like "month/day/year" compared to the clever...
6
1955
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 what most people use, but unfortunately I only get an empty file when I try to download it. I also tried the standard modules and ended up with this import datetime from time import strptime
0
1142
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 set to date. My program is being used in many different countries, and many different local settings. Some time ago a user from Turkey contacted me saying my program crashed on his system with the following error code: System.ArgumentException:...
10
430
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 before the date can vary. Thanks in advance for that respond
7
13642
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
1882
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 and the book that I have on it really kind of sucks. I'm having an issue parsing lines of 'last' output that I have stored in a /tmp file. The first time it does a .readline() I get the full line of output, which I'm then able to split() and...
0
9687
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
9541
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
10485
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
10252
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9073
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
6805
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();...
1
4141
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
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2938
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.