I want to display data in this format yyyy/mm/dd
How I can do it? When I use
String.Format("{0:yyyy/MM/dd}", Model.RequiredDate)
I get yyyy.mm.dd
You can escape it like;
String.Format("{0:yyyy'/'MM'/'dd}", Model.RequiredDate)
I strongly suspect your CurrentCulture's DateSeparator is ., that's why / format specifier replace itself to it.
What about this option?
string DateToString(DateTime date, string seperator)
{
return date.ToString("yyyy") + seperator + date.ToString("MM") + seperator + date.ToString("dd");
}
But as Soner Gönül said, you can also escape it like this:
String.Format("{0:yyyy'/'MM'/'dd}", Model.RequiredDate)
EDIT:
What about this two options?
string DateToString(DateTime date, string seperator, string first, string second, string third)
{
return date.ToString(first) + seperator + date.ToString(second) + seperator + date.ToString(third);
}
string DateToString(DateTime date, string seperator, string[] format)
{
string result = "";
foreach (string s in format)
{
s += (date.ToString(s) + seperator);
}
return result;
}
Related
When I try this code:
string value = "220510"; // The `value` variable is always in this format
string key = "30";
string title;
switch (key)
{
case "30":
title = "Date: ";
Console.WriteLine($"{title} is {value}");
break;
}
the output looks like this:
My problem is that I don't know how to insert the '-' character to separate the month, day and year because I want it to display:
Date: is 22-05-10
Please show me how to parse it.
If you have a DateTime object:
oDate.toString("yy-MM-dd");
If you have a string you can either:
sDate = sDate.Insert(2,"-");
sDate = sDate.Insert(5,"-");
or go through DateTime again (for whatever reason):
string sDate = "220510";
CultureInfo provider = CultureInfo.InvariantCulture;
DateTime sDate = Convert.ParseExact(iDate, "yyMMdd", provider);
sDate.toString("yy-MM-dd");
Your question is: How do I parse the string 220510 date format so the value comes out as 22-05-10?
In this specific case, consider using the string.Substring method to pick out the digit pairs then use string interpolation to put them back together.
const string raw = "220510";
// To do a simple parse (not using a DateTime object)
var yearString = raw.Substring(0, 2);
var monthString = raw.Substring(2, 2);
var dayString = raw.Substring(4, 2);
var string_22_05_10 = $"{yearString}-{monthString}-{dayString}";
Console.WriteLine(string_22_05_10);
I am trying to pass quotes in string. I am having a hard time formulating the code.
path = path.Insert(0, #"\\ffusvintranet02\picfiles\temp\");
string format = "Set-UserPhoto ";
format += "" + user + "";
format += " -PictureData ([System.IO.File]::ReadAllBytes(";
format += "" + path + #"";
format += ")";
The user and path are variables that needs to be inside single quotes for the AD Command. command. What I have isn't working.
User \" for " symbol or \' for '
format += "\"" + user + "\"";
First of all , use string.format for such tasks. Second you have to escape quotes ( but you dont need to escape single quotes).
Double quotes can be escaped by double quote or by backslash based on type of string literal you are using:
var s = #"something "" somethin else "; // double double quote here
var s2 = "something \" somethin else ";
Now, using string.format, your code will turn into:
path = path.Insert(0, #"\\ffusvintranet02\picfiles\temp\");
string format = string.format("Set-UserPhoto {0} -PictureData ([System.IO.File]::ReadAllBytes(\"{1}\")", user, path);
or
path = path.Insert(0, #"\\ffusvintranet02\picfiles\temp\");
string format = string.format(#"Set-UserPhoto {0} -PictureData ([System.IO.File]::ReadAllBytes(""{1}"")", user, path);
string format = "Set-UserPhoto "; format += "'" + user + "'"; format += " -PictureData ([System.IO.File]::ReadAllBytes("; format += "'" + path + #"'"; format += ")";
I would suggest using string interpolation within a here-string as follows, this will prevent you from having to use string concatenation and escaping.
$format = #"
Set-UserPhoto " + user + " -PictureData ([System.IO.File]::ReadAllBytes(" + path + ")"
"#
I'm getting a string " abc df fd";
I want to split a space or null of string. As result is "abc df fd" That such I want;
private string _senselist;
public string senselist
{
get
{
return _senselist;
}
set
{
_senselist = value.Replace("\t", "").Replace(" "," ").Split(,1);
}
}
To remove spaces from the begining and the ending of a string, you can use Trim() method.
string data = " abc df fd";
string trimed = data.Trim(); // "abc df fd"
On your code add Trim at the end, instead of Split
_senselist = value.Replace("\t", "").Replace(" "," ").Trim();
As #andrei-rînea recommended you can also check TrimStart(' ') and TrimEnd(' ').
I'm trying to create a C# property as a string as part of an experiment, but I'm getting the error "Input string was not in the correct format".
Is this because of the quote?
How can I properly append the double quote character to the string?
this is the code:
string quote = "\"";
string propName = "MyPropName";
string propVal = "MyPropVal";
string csProperty = string.Format("public string {0} { get { return {1}; } }", propName, quote + propVal + quote);
Escape the braces (by doubling) that are not part of the format mask:
string csProperty = string.Format("public string {0} {{ get {{ return {1}; }} }}", propName, quote + propVal + quote);
Here is the source code for this test:
var tags = new List<string> {"Portland", "Code","StackExcahnge" };
const string separator = " ";
tagString = tags.Aggregate(t => , separator);
Console.WriteLine(tagString);
// Expecting to see "Portland Code StackExchange"
Console.ReadKey();
Update
Here is the solution I am now using:
var tagString = string.Join(separator, tags.ToArray());
Turns out string.Join does what I need.
For that you can just use string.Join.
string result = tags.Aggregate((acc, s) => acc + separator + s);
or simply
string result = string.Join(separator, tags);
String.Join Method may be?
This is what I use
public static string Join(this IEnumerable<string> strings, string seperator)
{
return string.Join(seperator, strings.ToArray());
}
And then it looks like this
tagString = tags.Join(" ")