Find and Replace in C#.Net - c#

I have one string such as Date which has the value of Dates. Below is a sample value:
string Date = 05;
When Date is between 01 and 09, Date value should ignore "0". For example it should be "5".
If it is "20" means then it should not ignore "0".
How to do this in C#?

Seems like you're trying to convert a string to an integer ?
Try Convert.toInt32(date); which will return an int... then calling ToString() will give you the string representation if necessary.

You can use String.Replace() or Regex.Replace(). But it's hard to understand how to solve your problem effectively without more details/code.

you know you could use an int for this...
int month = 5; //is 5 and not 05 as an example
why do you have a string?
Also, in c# Dates have a special variable, the DateTime. You can store months, days, years, hours etc. in them. This might be related to what you are looking for.

Related

SQLite intermittently querying successfully between two dates [duplicate]

This question already has answers here:
SQLite DateTime comparison
(14 answers)
Closed 9 years ago.
Im storing my dates as string in format 'dd-mm-yyyy'. Now I want to do a comparison like this:
SELECT strftime('%d/%m/%Y', myDate) as myDate
FROM myTable
WHERE myDate>='01/07/2013' and myDate<='24/07/2013'
But I get nothing. Whats wrong with this query?.
THIS IS NOT A DUPLICATE QUESTION. I was storing everything as String, no as a DATE or DATETIME. I was trying to do a comparison between strings with date format. So is not fair the down vote.
There is no way you can get your query to work like that with your current setup:
I'm storing the date as a String so I guess with the using of strftime I can get what I want. Or am I wrong?
Basically: You're wrong!
The problem here is that a string formatted like that will not be sortable in the way you want.
Specifically:
myDate>='01/07/2013' and myDate<='24/07/2013'
Here, any date that is between the first and the 24th of any month in any year will match this. Ie. this will match: "02/01/1900", as will this: "02/12/2099".
The reason for this is that string comparisons are alphanumerical, not numerical (or datewise). "02" is greater than "01" and less than "24", and the rest is just redundant.
The "logical" way to format a date as a string is to start with the most significant value and work your way downwards, ie. this format: "yyyy/mm/dd", and not the other way around, but the real logical way to store a date, is to store it as a date, and not as a string.
The only way to work with the strings is to convert each string to a date and work with that, but the best way is to fix your schema.
If you absolutely cannot change your current schema, here is a way to make it work, but I do not advice this:
where (substr(myDate, 7, 4) || '-' || substr(myDate, 4, 2) || '-' || substr(myDate, 1, 2)) between '2013-07-01' and '2013-07-24'
This will pick apart the strings, put them together again in the right order, before doing the comparison. Also note the format of the last two dates there.
Did you try this :
SELECT strftime('%d/%m/%Y', myDate) as myDate
FROM myTable
WHERE myDate between date('01/07/2013') and date('24/07/2013');

Short Time with DateTime.ParseExact

I’m trying to parse a time. I’ve seen this question asked/answered here many times but not for this specific scenario. Here’s my code:
var time1 = DateTime.ParseExact("919", "Hmm", CultureInfo.InvariantCulture);
also
var time2 = DateTime.ParseExact("919", "Hmm", null);
both of these throw the same
"String was not recognized as a valid DateTime"
What I want is 9:19 AM.
For further info I also need to parse “1305” as 1:05 PM, this is working fine.
It seems to me I’m using the correct format. What am I overlooking?
I'm not sure there is any format that can handle this. The problem is that "H" can be either one digit or two, so if there are two digits available, it will grab both - in this case parsing it as hour 91, which is clearly invalid.
Ideally, you'd change the format to HHmm - zero-padding the value where appropriate - so "0919" would parse fine. Alternatively, use a colon in the format, to distinguish between the hours and the minutes. I don't believe there's any way of making DateTime parse a value of "919" as you want it to... so you'll need to adjust the string somehow before parsing it. (We don't have enough context to recommend a particular way of doing that.)
Yes, your format is right but since H specifier might be 2 character, ParseExact method try to parse 91 as an hour, which is an invalid hour, that's why you get FormatException in both case.
I connected to microsoft team about this situation 4 months ago. Take a look;
DateTime conversion from string C#
They suggest to use 2 digit form in your string or insert a date separator between them.
var time1 = DateTime.ParseExact("0919", "Hmm", CultureInfo.InvariantCulture);
or
var time1 = DateTime.ParseExact("9:19", "H:mm", CultureInfo.InvariantCulture);
You cant exclude the 0 prefix to the hour. This works
var time1 = DateTime.ParseExact("0919", "Hmm", CultureInfo.InvariantCulture);
Perhaps you want to just prefix 3-character times with a leading zero before parsing.
Much appreciated for all the answers. I don’t have control of the text being created so the simplest solution for me seemed to be prefixing a zero as opposed to adding a colon in the middle.
var text = "919";
var time = DateTime.ParseExact(text.PadLeft(4, '0'), "Hmm", null);

Add 0's to dates where it is missing in c#?

I have a string with a value in the format of
dd/mm/yyyy
Now I want to compare that to another string and check if they are equal, the other string can have the value of
dd/mm/yyyy
or sometimes when the day is between 1 and 9:
d/mm/yyyy
Sometimes when the month is between 1 and 9:
dd/m/yyyy
So there are a couple of instances where the string isn't equal but the actual date is.
First I only saw that in some cases when the day is between 1-9 it doesn't start with a zero so I did:
createdDate = dateField.value.ToString().Substring(0, 10);
createdDate = createdDate.Trim();
if (createdDate.Length == 9)
createdDate = "0" + createdDate;
but then I saw that it can be the same for month so there must be a better way to check this?
Why not just parse both, one with the format string "dd/MM/yyyy" and one with the format string "d/M/yyyy" and compare the returned DateTime values? After all, the date being represented is the important part, not the string representation, presumably.
You should use DateTime.Parse to convert both values to dates and then compare the dates.
DateTime.Parse("01/01/2001") == DateTime.Parse("1/1/2001")
When it are strings, that represent dates, why don't you try to parse them to a date, and compare the date instances that you become ?
you just have to make sure that you use the correct pattern when parsing.
Check out the DateTime.TryParse method.
I'd parse both dates into DateTimes and then compare them, that way you don't have to worry about different formats in the compare.
First, parse the strings into values of type DateTime (DateTime.ParseExact), then use the standard equality comparer (==) to compare the dates.

How do I keep the 0's in a Date

I am trying to figure out how it is that I can keep the 0's or add them when I grab a date.
What Im getting is this:
6/15/2010
What I'm tring to get is:
06/15/2010
I have added it so that it checks the length to and if its less than 6 (im stripping the "/") it pads the left side. That solves the issue when the month is a single digit, but what about when the date is a single digit.
My ultimate goal is to have a date such as:
1/1/2010
read out like:
01/01/2010
Any suggestions would be greatly appreciated.
Use a custom format : dd/MM/yyyy, or in your case MM/dd/yyyy. Note the capital M, the small m gets you the minutes.
string s = DateTime.Now.ToString("MM/dd/yyyy");
You need to use a custom DateTime format string:
string str = someDate.ToString("dd/MM/yyyy");
It depends on the format of date you are using.
For instance, dd/MM/yyyy will produce 01/05/2009 and d/M/yyyy would produce 1/5/2009
A complete reference can be found there : http://msdn.microsoft.com/en-us/library/8kb3ddd4.aspx
You want something like this:
string myDate = "1/1/2010";
DateTime date = DateTime.Parse(myDate);
string formattedDate = date.ToString("MM/dd/yyyy");
If the starting date is some other unrecognized format you could use DateTime.ParseExact();
Use DateTime.ParseExact() to parse the string into a valid datetime object and then use DateTime.ToString("dd/MM/yyyy") to get result in desired format.

problem with conversion

This code doesn't return data from table:
var pom = from k in dataContext.student_gods
where k.skgod == System.Convert.ToString(2002/03)
select k.id_stud;
This code does return data from table:
var pom = from k in dataContext.student_gods
where k.skgod== "2002/03"
select k;
How to convert a string variable without quotes???
Taking a stab at what the OP might be running into, I suspect you have a DateTime object that you'd like to use in a query to compare against a date stored as a string. If that's the case, you can modify your query to look like:
DateTime t = ...
var pom = from k in dataContext.student_gods
where k.skgod == t.ToString("yyyy/MM")
select k;
Here, you're formatting the date to match what you're expecting to see in your database. The ToString method is formatting the date to return just the year and month components. Look to the MSDN article on Custom date and Time Format Strings for more.
To extend the example, it's currently about 3pm on Sunday, November 22nd. If I run the following code:
DateTime t = DateTime.Now();
string s = t.ToString("yyyy/MM");
Console.WriteLine(s);
...I will see 2009/11 printed.
Unlike "2002/03", 2002/03 is not a string but the integer division of 2002 by 03 (= 667).
Are you looking how to convert a DateTime to a string?
new DateTime(2002, 3, 1).ToString("yyyy/MM", CultureInfo.InvariantCulture)
This returns "2002/03".
Your problem is 2002/03 is not what you mean. What are you trying to convert here?
2002/03 is two integers and a division, and it's value is 2002 / 03 = 667. If you want the string "2002/03" you need to enter that string, "2002/03".
I hope this made sense :)
How to convert a string variable without quotes???
That doesn't make sense. String literals must be surrounded with quotes, that is what makes it a string. You cannot just try to convert undeclared variables into strings by their name, it doesn't work that way. You just need to compare against an actual string, like you do in your second example.
The string "2002/03" and 2002/03 are very different things. In C# there are no such things as string literals without quotes. C# is not PHP :-)
2002/03 is simply an integer division, namely 2002/3 = 667 (note that there are no decimal places, since this is an integer division).
So if you want to compare something with a string, then by all means use a string and not an arbitrary calculation result. Keep in mind though, that the == operator behaves somewhat erratically when applied to operands of object and string (since it might be not immediately obvious whether you are doing value or reference equality).

Categories