Parsing a string to get the month

T

thunderbolt

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
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
B

Beemer Biker

thunderbolt said:
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");
 
C

chornbe99

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.
 
B

Bruce Wood

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;
 
T

thunderbolt

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
 
P

PS

Bruce Wood said:
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
 
B

Bruce Wood

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.
 

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