[英]Convert int variable value to char array value in c#?
I tried it my way, want to know where am i going wrong and to know how to replace int variable value to one of the index of char array? 我以自己的方式尝试过,想知道我在哪里出错,并且想知道如何将int变量值替换为char数组的索引之一?
string time = "07:05:45PM";
string result = string.Empty;
char[] time_Char = time.ToCharArray();
int first_Char = (int)Char.GetNumericValue(time_Char[0]) + 1;
int second_Char = (int)Char.GetNumericValue(time_Char[1]) + 2;
time_Char[0] = Convert.ToChar(first_Char);
time_Char[1] = Convert.ToChar(second_Char);
for (int i = 0; i < time_Char.Length; i++)
{
result += time_Char[i];
}
Console.WriteLine(result);
Output - :05:45PM 输出- :05:45PM
Expected Output - 19:05:45PM 预期输出 -19:05 : 45 PM
Are you just trying to turn a 12 hour time into a 24 hour time? 您是否只是想将12小时变成24小时? It is not super clear from your question because obviously
AM/PM
are irrelevant when you are dealing with 24 time... 您的问题还不是很清楚,因为当您处理24小时时,显然
AM/PM
是无关紧要的...
However if this is what you are trying to do there are way easier ways of doing this. 但是,如果这是您要尝试执行的操作,则可以使用更简单的方法来执行此操作。 You should just parse your input string into a new
DateTime
object, then you can output that date/time using custom formatting when calling .ToString()
: 您应该将输入字符串解析为新的
DateTime
对象,然后可以在调用.ToString()
时使用自定义格式输出该日期/时间:
string time = "07:05:45PM";
DateTime output;
if(DateTime.TryParse(time, out output))
{
Console.WriteLine(output.ToString("HH:mm:sstt"));
}
This would output: 19:05:45PM
这将输出:
19:05:45PM
: 19:05:45PM
: 19:05:45PM
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.