Month showing up twice in list of months - c#

I'm attempting to get my website to output a list with all the months in a year, but I've hit a bottleneck that I can't figure out. For some reason, October comes up twice in the list and April doesn't even come up, like this (the names are in Danish, but you can hopefully understand it):
Currently I'm getting all the months out through a foreach loop. Måneder is just a list I created earlier up on the page. It seems like for some reason when it hits the tenth, it does the addition in some weird fashion, instead of just overwriting, making it the 16th in the order instead of the 10th:
foreach (int l in Måneder)
{
DateTime bleh = DateTime.Now.AddMonths(l);
string blah = (bleh.ToString("MMMM"));
Literal4.Text += blah + l + "<br/>";
}

The problem is the loop that populates Måneder:
for (int t = 1; t <= 12; t++)
{
int months = int.Parse(DateTime.Now.Month.ToString(t.ToString())); // <-- BUG
Måneder.Add(months);
}
This loop gets the current month (6 for June if you run it today) and calls ToString on that number twelve times, passing the loop variable t as the format string:
6.ToString("1") ==> 1
6.ToString("2") ==> 2
6.ToString("3") ==> 3
6.ToString("4") ==> 4
6.ToString("5") ==> 5
6.ToString("6") ==> 6
6.ToString("7") ==> 7
6.ToString("8") ==> 8
6.ToString("9") ==> 9
6.ToString("10") ==> 16
6.ToString("11") ==> 11
6.ToString("12") ==> 12
Notice that 6.ToString("10") returns 16. As documented under Custom Numeric Format Strings, a 0 in a format string is actually a placeholder for a digit in the value being formatted — the value in this case being 6. The other digits 1 through 9 in a format string have no special meaning.
Why does this loop use DateTime.Now.Month at all? To build a list of numbers from 1 to 12, just call Måneder.Add(t):
for (int t = 1; t <= 12; t++)
{
Måneder.Add(t);
}
You can also eliminate the list entirely:
for (int t = 1; t <= 12; t++)
{
DateTime bleh = DateTime.Now.AddMonths(t);
string blah = (bleh.ToString("MMMM"));
Literal4.Text += blah + t + "<br/>";
}

The + operator does both integer addition and string concatenation. Your problem is probably that it is doing the wrong one.
Try this:
foreach (int l in Måneder)
{
DateTime bleh = DateTime.Now.AddMonths(l);
string blah = (bleh.ToString("MMMM"));
Literal4.Text += blah + l.ToString() + "<br/>";
}

Related

How to format a number such that first 6 digits and last 4 digits are not hidden

How to format a number such that first 6 digits and last 4 digits are not hidden
such that 111111111111111 looks like 111111****1111
You can also use LINQ, substituting chars with indexes more than 5 and less than number.Length - 4 with *:
string number = "111111111111111";
string res = new string(number.Select((c, index) => { return (index <= 5 || index >= number.Length - 4) ? c : '*'; }).ToArray());
Console.WriteLine(res); // 111111*****1111
One simple way to do this is to slit the input..
int number = 111111111111111;
string firstsix = number.ToString().Substring(0,6) //gets the first 6 digits
string middlefour = number.ToString().Substring(6,4) //gets the next 4
string rest = number.ToString().Substring(10, number.ToString().Lenght) //gets the rest
string combined = firstsix + "****" +rest;
You need to use \G anchor in-order to do a continuous string match.
string result = Regex.Replace(str, #"(?:^(.{6})|\G).(?=.{4})", "$1*");
DEMO
IDEONE

using concatenation on c#

What is the correct way to concatenate these elements?
Environment: C#-WinForm-Desktop.
In my app, I have a text file called config.ini. This file has values with this nomenclature:
csv_month_hour=value
The data are thus:
[csv]
csv_01_01=value // 24 hours a day in January
csv_01_02=value
csv_01_03=value
.
csv_02_01=value // 24 hours a day in February
csv_02_02=value
csv_02_03=value
.
// and so on
My app has this method called:
private void getMonthHour()
{
DateTime Xdt = DateTime.Now;
int Xmonth = Xdt.Month;
int Xhour = Xdt.Hour;
if (File.Exists("config.ini"))
{
IniFile ini = new IniFile("config.ini");
m3h = Convert.ToDouble(confg.csv["csv_" + Xmonth.ToString + "_" + Xhour.ToString]);
}
}
This method is called every seconds in timer_tick to check the month and time to use the corresponding value.
I need to know how to concatenate this:
m3h = Convert.ToDouble(confg.csv["csv_"+Xmonth.ToString+"_"+Xhour.ToString]);
An example of the code would be:
if (Xmonth==1&&Xhour==1){m3h = Convert.ToDouble(confg.csv.csv_01_01);}
else if (Xmonth==1&&Xhour==2){m3h = Convert.ToDouble(confg.csv.csv_01_02);}
else if (Xmonth==1&&Xhour==3){m3h = Convert.ToDouble(confg.csv.csv_01_03);}
// csv_month_hour in this case 01 is January.
else if (Xmonth==2&&Xhour==1){m3h = Convert.ToDouble(confg.csv.csv_02_01);}
// And so on, until December (12).
Putting aside the invalid syntax/compiler error from your method calls in your code, I suspect you're building the string but can't get the leading 0 in front of digits 0-9. So when Xmonth and Xhour are less than 10, your string is being built as "csv_1_1" instead of your intended "csv_01_01".
To instruct the ToString method to include a leading zero when needed, you can supply a custom numeric format string "00" (see the first entry in the link for "Zero placeholder").
Try using Xmonth.ToString("00") and Xhour.ToString("00"):
Monitor.malyt.m3h = Convert.ToDouble(confg.csv["csv_" +
Xmonth.ToString("00") + "_" +
Xhour.ToString("00")]);
This will output the values of Xmonth and Xhour with a single leading zero where needed.

Max Character Per line in a textbox in C#

I have a textbox and i want that it should have only 21 characters at max and they should be displayed in 3 lines(7 characters in each). As the user is typing when he types the 7th character i want the focus to be shifted to next line. I have already tried the following code
int textLength = a.Text.Length;
if (textLength % 8 == 0)
{
a.Text += "\n";
a.SelectionStart = a.Text.Length - 1;
}
It does not work .
Thanks for the help.

How Can I read From Line number() to line Starts with in C#

Let's say I have text file like this
<pre>----------------
hPa m C
---------------------
1004.0 28 13.6
1000.0 62 16.2
998.0 79 17.2
992.0 131 18.0
<pre>----------------
Sometext here
1000.0 10 10.6
1000.0 10 11.2
900.0 10 12.2
900.0 100 13.0
<aaa>----------------
How Can I Create Array in C# that reads text file from line number 5 (1004.0) to just before line that starts with string <pre>-
I used string[] lines = System.IO.File.ReadAllLines(Filepath);
To make each line in the array
The problem is I want only numbers of first section in the array in order to separate them later to another 3 arrays (hPa, m, C) .
Here's a possible solution. It's probably way more complicated than it should be, but that should give you an idea of possible mechanisms to further refine your data.
string[] lines = System.IO.File.ReadAllLines("test.txt");
List<double> results = new List<double>();
foreach (var line in lines.Skip(4))
{
if (line.StartsWith("<pre>"))
break;
Regex numberReg = new Regex(#"\d+(\.\d){0,1}"); //will find any number ending in ".X" - it's primitive, and won't work for something like 0.01, but no such data showed up in your example
var result = numberReg.Matches(line).Cast<Match>().FirstOrDefault(); //use only the first number from each line. You could use Cast<Match>().Skip(1).FirstOrDefault to get the second, and so on...
if (result != null)
results.Add(Convert.ToDouble(result.Value, System.Globalization.CultureInfo.InvariantCulture)); //Note the use of InvariantCulture, otherwise you may need to worry about , or . in your numbers
}
Do you mean this?
System.IO.StreamReader file = new System.IO.StreamReader(FILE_PATH);
int skipLines = 5;
for (int i = 0; i < skipLines; i++)
{
file.ReadLine();
}
// Do what you want here.

How to get value from number tagged file and compare it?

hi i'm trying to extract values from this tagged file
here is the file
0
LINE
5
1C1CBD
330
1C1ADB
100
AcDbEntity
8
0-FD
62
9
370
-2
100
AcDbLine
10
53740.73468153231
20
-190253.3098529756
30
0.0
11
53690.49919802765
21
-190166.2994431953
31
0.0
0
you can see that there is a hexadecimal number below "LINE", is there anyway i can extract it into some list?
like
VALUE = 1C1CBD
NOTE: the file consist more than one of this kind of pattern
and then how can i get the maximum value?
EDIT 1
i see that "5" can be found by using regex "\s\s5" can i use this?
Thank you for your feedback i have been able to complete what i want
here is the code
string[] handle = originalString.Split(new string[] { "\r\n" }, StringSplitOptions.None);
List<string> hexa = new List<string>();
for (var a = 1; a <= handle.Count() - 1; a++)
{
if (Regex.IsMatch(handle[a], #"^\s\s5"))
{
hexa.Add(handle[a + 1]);
}
}
List<int> HexaToInt = new List<int>();
foreach (string valueHexa in hexa)
{
int intHexaValue = int.Parse(valueHexa, System.Globalization.NumberStyles.HexNumber);
HexaToInt.Add(intHexaValue);
}
int maximumHexa = HexaToInt.Max();
string hexValue = maximumHexa.ToString("X");
it is possible afterall to extract the hexadecimal value from the handle group codes
It is totally impossible.
No way to catch characters and extract what you need.
You have to organize the structure of this file better.

Categories

Resources